#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;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int __initialize_socket(const char *port) {
  int status;
  struct addrinfo hints, *addresses, *a;
  int sock;
  memset(&hints, 0x00, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  status = getaddrinfo(NULL, port, &hints, &addresses);
  if (status != 0) {
    fprintf(stderr, "Couldn't get address to bind to.\n");
    return -1;
  }
  for (a = addresses; a; a = a->ai_next) {
    sock = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
    if (sock == -1)
      continue;
    if (!bind(sock, a->ai_addr, a->ai_addrlen))
      break;
    status = close(sock);
    assert(!status);
  }
  freeaddrinfo(addresses);
  if (!a) {
    fprintf(stderr, "Couldn't bind socket\n");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<__initialize_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1203 <__initialize_socket+0x83>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <__initialize_socket+0x14f>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1294 <__initialize_socket+0x114>
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   10a0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1242 <__initialize_socket+0xc2>
jmp    1283 <__initialize_socket+0x103>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%eax
jne    1265 <__initialize_socket+0xe5>
jmp    1294 <__initialize_socket+0x114>
mov    -0x54(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    120b <__initialize_socket+0x8b>
mov    -0x48(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x50(%rbp)
jne    12c9 <__initialize_socket+0x149>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <__initialize_socket+0x14f>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__initialize_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11ec <__initialize_socket+0x6c>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
jmp    125d <__initialize_socket+0xdd>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1209 <__initialize_socket+0x89>
xor    %edi,%edi
jmp    1248 <__initialize_socket+0xc8>
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1244 <__initialize_socket+0xc4>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1200 <__initialize_socket+0x80>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <bind@plt>
test   %eax,%eax
je     1272 <__initialize_socket+0xf2>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
jmp    1200 <__initialize_socket+0x80>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    1267 <__initialize_socket+0xe7>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Couldn't get address to bind to.\0A\00"
@.str.1 = [22 x i8] c"Couldn't bind socket\0A\00"
define i32 @__initialize_socket(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %52, label %21
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %57
21: ; preds = %15, %43
%22 = phi %struct.addrinfo* [ %45, %43 ], [ %16, %15 ]
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 4
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %24, i32 %26, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %43, label %31
31: ; preds = %21
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @bind(i32 %29, i32 %33, i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %47, label %38
38: ; preds = %31
%39 = call i32 @close(i32 %29)
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
br label %43
43: ; preds = %21, %38
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 5
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %50, label %21
47: ; preds = %31
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
br label %57
50: ; preds = %43
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %52
52: ; preds = %50, %15
%53 = phi %struct.addrinfo* [ %51, %50 ], [ null, %15 ]
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %57
57: ; preds = %47, %52, %18
%58 = phi i32 [ -1, %18 ], [ -1, %52 ], [ %29, %47 ]
ret i32 %58
}