Clothing retailer Gap Inc. is electing to purchase a San Francisco building that serves as the headquarters for its Old Navy brand for almost $343 million, scooping the building out from under Mountain View, California-based developer The Sobrato Organization, which had an earlier agreement to purchase the property.