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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int AI_ADDRCONFIG;
int AI_PASSIVE;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ stderr;
int bind_socket(const char *dgram_port) {
  struct addrinfo ai_hints;
  struct addrinfo *ai_results = 0, *ai_item = 0;
  int fd_socket = 0, error = 0;
  memset(&ai_hints, 0, sizeof(struct addrinfo));
  ai_hints.ai_family = AF_UNSPEC;
  ai_hints.ai_socktype = SOCK_DGRAM;
  ai_hints.ai_protocol = 0;
  ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  error = getaddrinfo(NULL, dgram_port, &ai_hints, &ai_results);
  if (error != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
    exit(EXIT_FAILURE);
  }
  for (ai_item = ai_results; ai_item != NULL; ai_item = ai_item->ai_next) {
    fd_socket =
        socket(ai_item->ai_family, ai_item->ai_socktype, ai_item->ai_protocol);
    if (fd_socket == -1) {
      continue;
    }
    if (bind(fd_socket, ai_item->ai_addr, ai_item->ai_addrlen) == 0) {
      break;
    }
    close(fd_socket);
  }
  if (ai_item == NULL) {
    fprintf(stderr, "Could not bind\n");
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(ai_results);
  return fd_socket;
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    0x2db6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     1257 <bind_socket+0xc7>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12d5 <bind_socket+0x145>
mov    -0x40(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rdx
call   10b0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1297 <bind_socket+0x107>
jmp    12c4 <bind_socket+0x134>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jne    12bc <bind_socket+0x12c>
jmp    12d5 <bind_socket+0x145>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    125f <bind_socket+0xcf>
cmpq   $0x0,-0x40(%rbp)
jne    1305 <bind_socket+0x175>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e07(%rip),%rax
mov    0x2dc8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1283 <bind_socket+0xf3>
mov    (%rsp),%rbx
jmp    1214 <bind_socket+0x84>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rbx),%rbx
test   %rbx,%rbx
je     125e <bind_socket+0xce>
mov    0x1c(%rbx),%edi
mov    0x18(%rbx),%esi
mov    0x10(%rbx),%rdx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <bind_socket+0x80>
mov    %eax,%ebp
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %eax,%edi
call   1070 <bind@plt>
test   %rax,%rax
je     124a <bind_socket+0xba>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1210 <bind_socket+0x80>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d3e(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd66(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1275 <bind_socket+0xe5>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i64, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"Could not bind\0A\00"
define i32 @bind_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*
store %struct.addrinfo* null, %struct.addrinfo** %3
%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 5
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i64 0, i64* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = load i32, i32* @AI_ADDRCONFIG
%14 = or i32 %13, %12
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
store i32 %14, i32* %15
%16 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %50, label %27
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = call i8* @gai_strerror(i32 %16)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %18, %46
%28 = phi %struct.addrinfo* [ %48, %46 ], [ %19, %18 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%34 = load i64, i64* %33
%35 = call i32 @socket(i32 %30, i32 %32, i64 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %46, label %37
37: ; preds = %27
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%41 = load i32, i32* %40
%42 = call i64 @bind(i32 %35, i32 %39, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %55, label %44
44: ; preds = %37
%45 = call i32 @close(i32 %35)
br label %46
46: ; preds = %27, %44
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 6
%48 = load %struct.addrinfo*, %struct.addrinfo** %47
%49 = icmp eq %struct.addrinfo* %48, null
br i1 %49, label %50, label %27
50: ; preds = %46, %18
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %37
%56 = load %struct.addrinfo*, %struct.addrinfo** %3
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
ret i32 %35
}