#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int initialize_socket(int *sockfd2) {
  int sockfd;
  struct addrinfo hints, *servinfo, *p;
  int yes = 1;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rv = getaddrinfo(NULL, "3490", &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("server: socket");
      continue;
    }
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
      perror("setsockopt");
      exit(1);
    }
    if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      perror("server: bind");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "server: failed to bind\n");
    return 2;
  }
  freeaddrinfo(servinfo);
  *sockfd2 = sockfd;
  return 0;
}


x86 assembly (clang, -O0):
<initialize_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x54(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdfe(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
je     124f <initialize_socket+0x9f>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x58(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1370 <initialize_socket+0x1c0>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1329 <initialize_socket+0x179>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1299 <initialize_socket+0xe9>
lea    0xd87(%rip),%rdi
call   1090 <perror@plt>
jmp    1318 <initialize_socket+0x168>
mov    -0x14(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12dc <initialize_socket+0x12c>
lea    0xd58(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1313 <initialize_socket+0x163>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xd27(%rip),%rdi
call   1090 <perror@plt>
jmp    1318 <initialize_socket+0x168>
jmp    1329 <initialize_socket+0x179>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1257 <initialize_socket+0xa7>
cmpq   $0x0,-0x50(%rbp)
jne    1357 <initialize_socket+0x1a7>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
lea    0xcf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1370 <initialize_socket+0x1c0>
mov    -0x48(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initialize_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r15
movl   $0x1,0x4(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0xdf5(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbp,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     124b <initialize_socket+0x9b>
mov    0x2da3(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdd0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    12fc <initialize_socket+0x14c>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12cf <initialize_socket+0x11f>
lea    0xdba(%rip),%r12
mov    0x2d55(%rip),%r13
lea    0x4(%rsp),%r14
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10d0 <socket@plt>
mov    %r12,%rdi
cmp    $0xffffffff,%eax
je     12c1 <initialize_socket+0x111>
mov    %eax,%ebp
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    0x0(%r13),%edx
mov    %ebp,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     130b <initialize_socket+0x15b>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ed <initialize_socket+0x13d>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1268 <initialize_socket+0xb8>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x2,%eax
jmp    12fc <initialize_socket+0x14c>
mov    0x8(%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebp,(%r15)
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd13(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [5 x i8] c"3490\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [15 x i8] c"server: socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
@.str.4 = [13 x i8] c"server: bind\00"
@.str.5 = [24 x i8] c"server: failed to bind\0A\00"
define i32 @initialize_socket(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i32 1, i32* %8
%10 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 40)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 6
store i32 %15, i32* %16
%17 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %5, %struct.addrinfo** %6)
store i32 %17, i32* %9
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = load i32, i32* %9
%22 = call i8* @gai_strerror(i32 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 1, i32* %2
br label %82
24: ; preds = %1
%25 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %25, %struct.addrinfo** %7
br label %26
26: ; preds = %67, %24
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = icmp ne %struct.addrinfo* %27, null
br i1 %28, label %29, label %71
29: ; preds = %26
%30 = load %struct.addrinfo*, %struct.addrinfo** %7
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 4
%32 = load i32, i32* %31
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 3
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %7
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = call i32 @socket(i32 %32, i32 %35, i32 %38)
store i32 %39, i32* %4
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %29
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %67
43: ; preds = %29
%44 = load i32, i32* %4
%45 = load i32, i32* @SOL_SOCKET
%46 = load i32, i32* @SO_REUSEADDR
%47 = call i32 @setsockopt(i32 %44, i32 %45, i32 %46, i32* %8, i32 4)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %43
%53 = load i32, i32* %4
%54 = load %struct.addrinfo*, %struct.addrinfo** %7
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load %struct.addrinfo*, %struct.addrinfo** %7
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @bind(i32 %53, i32 %56, i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %66
62: ; preds = %52
%63 = load i32, i32* %4
%64 = call i32 @close(i32 %63)
%65 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %67
66: ; preds = %52
br label %71
67: ; preds = %62, %41
%68 = load %struct.addrinfo*, %struct.addrinfo** %7
%69 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %68, i32 0, i32 5
%70 = load %struct.addrinfo*, %struct.addrinfo** %69
store %struct.addrinfo* %70, %struct.addrinfo** %7
br label %26
71: ; preds = %66, %26
%72 = load %struct.addrinfo*, %struct.addrinfo** %7
%73 = icmp eq %struct.addrinfo* %72, null
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %2
br label %82
77: ; preds = %71
%78 = load %struct.addrinfo*, %struct.addrinfo** %6
%79 = call i32 @freeaddrinfo(%struct.addrinfo* %78)
%80 = load i32, i32* %4
%81 = load i32*, i32** %3
store i32 %80, i32* %81
store i32 0, i32* %2
br label %82
82: ; preds = %77, %74, %19
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [5 x i8] c"3490\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [15 x i8] c"server: socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
@.str.4 = [13 x i8] c"server: bind\00"
@.str.5 = [24 x i8] c"server: failed to bind\0A\00"
define i32 @initialize_socket(i32* nocapture writeonly %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%8 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_PASSIVE
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %57, label %24
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = call i8* @gai_strerror(i32 %15)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %63
24: ; preds = %17, %51
%25 = phi %struct.addrinfo* [ %55, %51 ], [ %18, %17 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %51, label %34
34: ; preds = %24
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_REUSEADDR
%37 = call i32 @setsockopt(i32 %32, i32 %35, i32 %36, i32* %4, i32 4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %34
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @bind(i32 %32, i32 %44, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %60
49: ; preds = %42
%50 = call i32 @close(i32 %32)
br label %51
51: ; preds = %24, %49
%52 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %49 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %24 ]
%53 = call i32 @perror(i8* %52)
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%55 = load %struct.addrinfo*, %struct.addrinfo** %54
%56 = icmp eq %struct.addrinfo* %55, null
br i1 %56, label %57, label %24
57: ; preds = %51, %17
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %63
60: ; preds = %42
%61 = load %struct.addrinfo*, %struct.addrinfo** %3
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %61)
store i32 %32, i32* %0
br label %63
63: ; preds = %60, %57, %20
%64 = phi i32 [ 1, %20 ], [ 2, %57 ], [ 0, %60 ]
ret i32 %64
}