#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 atol(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char *strrchr(char *, char);
struct sockaddr_in str2sa(char *str) {
  static struct sockaddr_in sa;
  char *c;
  int port;
  bzero(&sa, sizeof(sa));
  str = strdup(str);
  if ((c = strrchr(str, ':')) != NULL) {
    *c++ = 0;
    port = atol(c);
  } else
    port = 0;
  if (!inet_aton(str, &sa.sin_addr)) {
    struct hostent *he;
    if ((he = gethostbyname(str)) == NULL)
      fprintf(stderr, "[NetTools] Invalid server name: %s\n", str);
    else
      sa.sin_addr = *(struct in_addr *)*(he->h_addr_list);
  }
  sa.sin_port = htons(port);
  sa.sin_family = AF_INET;
  free(str);
  return sa;
}


x86 assembly (clang, -O0):
<str2sa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x18(%rbp)
lea    0x2ec9(%rip),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f9 <str2sa+0x69>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1090 <atol@plt>
mov    %eax,-0x24(%rbp)
jmp    1200 <str2sa+0x70>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0x2e61(%rip),%rsi
add    $0x8,%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%eax
jne    1269 <str2sa+0xd9>
mov    -0x18(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1252 <str2sa+0xc2>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdb8(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1264 <str2sa+0xd4>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x2e10(%rip)
jmp    1269 <str2sa+0xd9>
mov    -0x24(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,0x2df9(%rip)
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2de6(%rip)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2ddf(%rip),%eax
mov    %eax,-0x8(%rbp)
mov    0x2dcd(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%edx
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2sa>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x2ed3(%rip),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    %rbx,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11d7 <str2sa+0x47>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
call   1090 <atol@plt>
mov    %eax,%ebp
jmp    11d9 <str2sa+0x49>
xor    %ebp,%ebp
lea    0x2e98(%rip),%rsi
mov    %rbx,%rdi
call   1080 <inet_aton@plt>
test   %eax,%eax
jne    1221 <str2sa+0x91>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1209 <str2sa+0x79>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x2e71(%rip)
jmp    1221 <str2sa+0x91>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x2e46(%rip)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2e33(%rip)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2e24(%rip),%rax
mov    0x2e26(%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
@stderr = global i32 0
@.str = [36 x i8] c"[NetTools] Invalid server name: %s\0A\00"
@AF_INET = global i32 0
define { i64, i32 } @str2sa(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca { i64, i32 }
store i8* %0, i8** %3
%8 = call i32 @bzero(%struct.sockaddr_in* @str2sa.sa, i32 12)
%9 = load i8*, i8** %3
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = call i8* @strrchr(i8* %11, i8 signext 58)
store i8* %12, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %4
store i8 0, i8* %15
%17 = load i8*, i8** %4
%18 = call i32 @atol(i8* %17)
store i32 %18, i32* %5
br label %20
19: ; preds = %1
store i32 0, i32* %5
br label %20
20: ; preds = %19, %14
%21 = load i8*, i8** %3
%22 = call i32 @inet_aton(i8* %21, %struct.in_addr* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i32 0, i32 2))
%23 = icmp ne i32 %22, 0
br i1 %23, label %40, label %24
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = call %struct.hostent* @gethostbyname(i8* %25)
store %struct.hostent* %26, %struct.hostent** %6
%27 = icmp eq %struct.hostent* %26, null
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %3
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %30)
br label %39
32: ; preds = %24
%33 = load %struct.hostent*, %struct.hostent** %6
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i64*, i64** %34
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to %struct.in_addr*
%38 = bitcast %struct.in_addr* %37 to i8*
br label %39
39: ; preds = %32, %28
br label %40
40: ; preds = %39, %20
%41 = load i32, i32* %5
%42 = call i32 @htons(i32 %41)
store i32 %42, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i32 0, i32 1)
%43 = load i32, i32* @AF_INET
store i32 %43, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i32 0, i32 0)
%44 = load i8*, i8** %3
%45 = call i32 @free(i8* %44)
%46 = bitcast %struct.sockaddr_in* %2 to i8*
%47 = bitcast { i64, i32 }* %7 to i8*
%48 = bitcast %struct.sockaddr_in* %2 to i8*
%49 = load { i64, i32 }, { i64, i32 }* %7
ret { i64, i32 } %49
}

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
@stderr = global i32 0
@.str = [36 x i8] c"[NetTools] Invalid server name: %s\0A\00"
@AF_INET = global i32 0
define { i64, i32 } @str2sa(i8* nocapture %0) {
%2 = tail call i32 @bzero(%struct.sockaddr_in* @str2sa.sa, i32 12)
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %3, i8 signext 58)
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = getelementptr inbounds i8, i8* %4, i64 1
store i8 0, i8* %4
%8 = tail call i32 @atol(i8* %7)
br label %9
9: ; preds = %1, %6
%10 = phi i32 [ %8, %6 ], [ 0, %1 ]
%11 = tail call i32 @inet_aton(i8* %3, %struct.in_addr* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 2))
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = tail call %struct.hostent* @gethostbyname(i8* %3)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %25
19: ; preds = %13
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%21 = load i64*, i64** %20
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i32*
%24 = load i32, i32* %23
store i32 %24, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 2, i32 0)
br label %25
25: ; preds = %16, %19, %9
%26 = tail call i32 @htons(i32 %10)
store i32 %26, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 1)
%27 = load i32, i32* @AF_INET
store i32 %27, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 0)
%28 = tail call i32 @free(i8* %3)
%29 = load i64, i64* bitcast (%struct.sockaddr_in* @str2sa.sa to i64*)
%30 = load i32, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @str2sa.sa, i64 0, i32 2, i32 0)
%31 = insertvalue { i64, i32 } poison, i64 %29, 0
%32 = insertvalue { i64, i32 } %31, i32 %30, 1
ret { i64, i32 } %32
}