#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ free(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
char *strdup(char *);
struct sockaddr_in str2sa(char *str, int port) {
  static struct sockaddr_in sa;
  bzero(&sa, sizeof(sa));
  str = strdup(str);
  if (!inet_aton(str, &sa.sin_addr)) {
    struct hostent *he;
    if ((he = gethostbyname(str)) != NULL) {
      sa.sin_addr = *(struct in_addr *)*(he->h_addr_list);
    }
  }
  sa.sin_port = htons(port);
  sa.sin_family = AF_INET;
  if (str != NULL) {
    free(str);
  }
  return sa;
}


x86 assembly (clang, -O0):
<str2sa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
lea    0x2ede(%rip),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0x2ebc(%rip),%rsi
add    $0x8,%rsi
call   1060 <inet_aton@plt>
cmp    $0x0,%eax
jne    11d8 <str2sa+0x78>
mov    -0x18(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11d3 <str2sa+0x73>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x2e89(%rip)
jmp    11d8 <str2sa+0x78>
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,0x2e72(%rip)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2e5f(%rip)
cmpq   $0x0,-0x18(%rbp)
je     1209 <str2sa+0xa9>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e4d(%rip),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e3b(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%edx
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2sa>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x2ee9(%rip),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
lea    0x2ed5(%rip),%rsi
mov    %rax,%rdi
call   1060 <inet_aton@plt>
test   %eax,%eax
jne    11b2 <str2sa+0x52>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11b2 <str2sa+0x52>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x2eae(%rip)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x2e9d(%rip)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2e8a(%rip)
test   %rbx,%rbx
je     11db <str2sa+0x7b>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2e76(%rip),%rax
mov    0x2e78(%rip),%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
@str2sa.sa = internal global %struct.sockaddr_in zeroinitializer
@AF_INET = global i32 0
define { i64, i32 } @str2sa(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca { i64, i32 }
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i32 @bzero(%struct.sockaddr_in* @str2sa.sa, i32 12)
%9 = load i8*, i8** %4
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = call i32 @inet_aton(i8* %11, %struct.in_addr* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i32 0, i32 2))
%13 = icmp ne i32 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call %struct.hostent* @gethostbyname(i8* %15)
store %struct.hostent* %16, %struct.hostent** %6
%17 = icmp ne %struct.hostent* %16, null
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load %struct.hostent*, %struct.hostent** %6
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i64*, i64** %20
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to %struct.in_addr*
%24 = bitcast %struct.in_addr* %23 to i8*
br label %25
25: ; preds = %18, %14
br label %26
26: ; preds = %25, %2
%27 = load i32, i32* %5
%28 = call i32 @htons(i32 %27)
store i32 %28, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i32 0, i32 1)
%29 = load i32, i32* @AF_INET
store i32 %29, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i32 0, i32 0)
%30 = load i8*, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %32, %26
%36 = bitcast %struct.sockaddr_in* %3 to i8*
%37 = bitcast { i64, i32 }* %7 to i8*
%38 = bitcast %struct.sockaddr_in* %3 to i8*
%39 = load { i64, i32 }, { i64, i32 }* %7
ret { i64, i32 } %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
@str2sa.sa = internal global %struct.sockaddr_in zeroinitializer
@AF_INET = global i32 0
define { i64, i32 } @str2sa(i8* nocapture %0, i32 %1) {
%3 = tail call i32 @bzero(%struct.sockaddr_in* @str2sa.sa, i32 12)
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i32 @inet_aton(i8* %4, %struct.in_addr* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 2))
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = tail call %struct.hostent* @gethostbyname(i8* %4)
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%12 = load i64*, i64** %11
%13 = load i64, i64* %12
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* %14
store i32 %15, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 2, i32 0)
br label %16
16: ; preds = %7, %10, %2
%17 = tail call i32 @htons(i32 %1)
store i32 %17, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 1)
%18 = load i32, i32* @AF_INET
store i32 %18, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 0)
%19 = icmp eq i8* %4, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @free(i8* %4)
br label %22
22: ; preds = %20, %16
%23 = load i64, i64* bitcast (%struct.sockaddr_in* @str2sa.sa to i64*)
%24 = load i32, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 2, i32 0)
%25 = insertvalue { i64, i32 } poison, i64 %23, 0
%26 = insertvalue { i64, i32 } %25, i32 %24, 1
ret { i64, i32 } %26
}