#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ name;
typedef int /*<<< orphan*/ hint;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ gethostname(char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ ltr_int_log_message(char *, ...);
int /*<<< orphan*/ ltr_int_my_perror(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int prepare_server_comm(int /*<<< orphan*/, struct sockaddr *,
                        int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int init_server(unsigned int port) {
  int sfd;
  char name[1024];
  struct addrinfo *ai;
  struct addrinfo hint;
  memset(&hint, 0, sizeof(hint));
  hint.ai_family = AF_INET;
  hint.ai_socktype = SOCK_STREAM;
  if (gethostname(name, sizeof(name)) != 0) {
    ltr_int_my_perror("gethostname");
  } else {
    strcpy(name, "localhost");
  }
  int status = getaddrinfo(name, NULL, &hint, &ai);
  if (status != 0) {
    ltr_int_log_message("getaddrinfo: %s\n", gai_strerror(status));
    return -1;
  }
  struct addrinfo *tmp_ai;
  for (tmp_ai = ai; tmp_ai != NULL; tmp_ai = tmp_ai->ai_next) {
    struct sockaddr_in *sinp = (struct sockaddr_in *)tmp_ai->ai_addr;
    sinp->sin_addr.s_addr = htonl(INADDR_ANY);
    sinp->sin_port = htons(port);
    sfd = prepare_server_comm(SOCK_STREAM, (struct sockaddr *)sinp,
                              tmp_ai->ai_addrlen, 0);
    if (sfd != -1) {
      break;
    }
  }
  if (tmp_ai == NULL) {
    ltr_int_log_message("Didn't work!\n");
    return -1;
  }
  freeaddrinfo(ai);
  return sfd;
}


x86 assembly (clang, -O0):
<init_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x438(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x41c(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10b0 <gethostname@plt>
cmp    $0x0,%rax
je     121b <init_server+0x6b>
lea    0xdef(%rip),%rdi
call   1090 <ltr_int_my_perror@plt>
jmp    122e <init_server+0x7e>
lea    -0x410(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x438(%rbp),%rdx
lea    -0x418(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0x0,-0x43c(%rbp)
je     1286 <init_server+0xd6>
mov    -0x43c(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    %eax,%esi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <ltr_int_log_message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1369 <init_server+0x1b9>
mov    -0x418(%rbp),%rax
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
je     132f <init_server+0x17f>
mov    -0x448(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x450(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,%ecx
mov    -0x450(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,%ecx
mov    -0x450(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
mov    -0x450(%rbp),%rsi
mov    -0x448(%rbp),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   10a0 <prepare_server_comm@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     1313 <init_server+0x163>
jmp    132f <init_server+0x17f>
jmp    1318 <init_server+0x168>
mov    -0x448(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x448(%rbp)
jmp    1294 <init_server+0xe4>
cmpq   $0x0,-0x448(%rbp)
jne    1357 <init_server+0x1a7>
lea    0xce3(%rip),%rdi
mov    $0x0,%al
call   1040 <ltr_int_log_message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1369 <init_server+0x1b9>
mov    -0x418(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x430,%rsp
mov    %edi,%r15d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2de9(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x28(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x400,%esi
call   10b0 <gethostname@plt>
test   %rax,%rax
je     1210 <init_server+0x60>
lea    0xdf7(%rip),%rdi
call   1090 <ltr_int_my_perror@plt>
jmp    1221 <init_server+0x71>
lea    0xdf5(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1030 <strcpy@plt>
lea    0x30(%rsp),%rdi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %esi,%esi
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
je     1259 <init_server+0xa9>
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xdcd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <ltr_int_log_message@plt>
mov    $0xffffffff,%ebp
jmp    12c9 <init_server+0x119>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12a8 <init_server+0xf8>
mov    0x2d76(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    0x8(%rbx),%rbp
mov    (%r12),%edi
call   1070 <htonl@plt>
mov    %eax,0x4(%rbp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x0(%rbp)
mov    (%r14),%edi
mov    (%rbx),%edx
mov    %rbp,%rsi
xor    %ecx,%ecx
call   10a0 <prepare_server_comm@plt>
cmp    $0xffffffff,%eax
jne    12bd <init_server+0x10d>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    1270 <init_server+0xc0>
lea    0xd78(%rip),%rdi
xor    %eax,%eax
call   1040 <ltr_int_log_message@plt>
mov    $0xffffffff,%ebp
jmp    12c9 <init_server+0x119>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64, %struct.addrinfo*, i32, i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"gethostname\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [17 x i8] c"getaddrinfo: %s\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [14 x i8] c"Didn't work!\0A\00"
define i32 @init_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
%8 = alloca i32
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.sockaddr_in*
store i32 %0, i32* %3
%11 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 32)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %14, i32* %15
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%17 = call i64 @gethostname(i8* %16, i32 1024)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %24
21: ; preds = %1
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %19
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%26 = call i32 @getaddrinfo(i8* %25, i32* null, %struct.addrinfo* %7, %struct.addrinfo** %6)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* %8
%31 = call i32 @gai_strerror(i32 %30)
%32 = call i32 (i8*, ...) @ltr_int_log_message(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %31)
store i32 -1, i32* %2
br label %76
33: ; preds = %24
%34 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %34, %struct.addrinfo** %9
br label %35
35: ; preds = %63, %33
%36 = load %struct.addrinfo*, %struct.addrinfo** %9
%37 = icmp ne %struct.addrinfo* %36, null
br i1 %37, label %38, label %67
38: ; preds = %35
%39 = load %struct.addrinfo*, %struct.addrinfo** %9
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 1
%41 = load i64, i64* %40
%42 = inttoptr i64 %41 to %struct.sockaddr_in*
store %struct.sockaddr_in* %42, %struct.sockaddr_in** %10
%43 = load i32, i32* @INADDR_ANY
%44 = call i32 @htonl(i32 %43)
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %45, i32 0, i32 1
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
store i32 %44, i32* %47
%48 = load i32, i32* %3
%49 = call i32 @htons(i32 %48)
%50 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load i32, i32* @SOCK_STREAM
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%54 = bitcast %struct.sockaddr_in* %53 to %struct.sockaddr*
%55 = load %struct.addrinfo*, %struct.addrinfo** %9
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @prepare_server_comm(i32 %52, %struct.sockaddr* %54, i32 %57, i32 0)
store i32 %58, i32* %4
%59 = load i32, i32* %4
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %38
br label %67
62: ; preds = %38
br label %63
63: ; preds = %62
%64 = load %struct.addrinfo*, %struct.addrinfo** %9
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 2
%66 = load %struct.addrinfo*, %struct.addrinfo** %65
store %struct.addrinfo* %66, %struct.addrinfo** %9
br label %35
67: ; preds = %61, %35
%68 = load %struct.addrinfo*, %struct.addrinfo** %9
%69 = icmp eq %struct.addrinfo* %68, null
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 (i8*, ...) @ltr_int_log_message(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %76
72: ; preds = %67
%73 = load %struct.addrinfo*, %struct.addrinfo** %6
%74 = call i32 @freeaddrinfo(%struct.addrinfo* %73)
%75 = load i32, i32* %4
store i32 %75, i32* %2
br label %76
76: ; preds = %72, %70, %29
%77 = load i32, i32* %2
ret i32 %77
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, %struct.addrinfo*, i32, i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"gethostname\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [17 x i8] c"getaddrinfo: %s\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [14 x i8] c"Didn't work!\0A\00"
define i32 @init_server(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i64 @gethostname(i8* %5, i32 1024)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %19
17: ; preds = %1
%18 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
%20 = call i32 @getaddrinfo(i8* %5, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %3)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load %struct.addrinfo*, %struct.addrinfo** %3
%24 = icmp eq %struct.addrinfo* %23, null
br i1 %24, label %48, label %32
25: ; preds = %19
%26 = call i32 @gai_strerror(i32 %20)
%27 = call i32 (i8*, ...) @ltr_int_log_message(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %53
28: ; preds = %32
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 2
%30 = load %struct.addrinfo*, %struct.addrinfo** %29
%31 = icmp eq %struct.addrinfo* %30, null
br i1 %31, label %48, label %32
32: ; preds = %22, %28
%33 = phi %struct.addrinfo* [ %30, %28 ], [ %23, %22 ]
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 1
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to %struct.sockaddr_in*
%37 = load i32, i32* @INADDR_ANY
%38 = call i32 @htonl(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i64 0, i32 1, i32 0
store i32 %38, i32* %39
%40 = call i32 @htons(i32 %0)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i64 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* @SOCK_STREAM
%43 = inttoptr i64 %35 to %struct.sockaddr*
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @prepare_server_comm(i32 %42, %struct.sockaddr* %43, i32 %45, i32 0)
%47 = icmp eq i32 %46, -1
br i1 %47, label %28, label %50
48: ; preds = %28, %22
%49 = call i32 (i8*, ...) @ltr_int_log_message(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %53
50: ; preds = %32
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %53
53: ; preds = %48, %50, %25
%54 = phi i32 [ -1, %25 ], [ -1, %48 ], [ %46, %50 ]
ret i32 %54
}