#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 bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int close(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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int bind_socket(char *port) {
  struct addrinfo hints;
  struct addrinfo *results, *r;
  int x, sockfd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  x = getaddrinfo(NULL, port, &hints, &results);
  if (x != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(x));
    return -1;
  }
  for (r = results; r != NULL; r = r->ai_next) {
    sockfd = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
    if (sockfd == -1) {
      perror("ignoring: socket");
    } else {
      x = bind(sockfd, r->ai_addr, r->ai_addrlen);
      if (x != 0) {
        perror("ignoring: bind");
        x = close(sockfd);
        if (x != 0) {
          perror("ignoring: close");
        }
      } else {
        break;
      }
    }
  }
  freeaddrinfo(results);
  if (r == NULL) {
    fprintf(stderr, "bind_socket: Unable to bind\n");
    return -1;
  } else {
    return sockfd;
  }
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1226 <bind_socket+0x96>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <bind_socket+0x192>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12e5 <bind_socket+0x155>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1271 <bind_socket+0xe1>
lea    0xdaa(%rip),%rdi
call   1080 <perror@plt>
jmp    12cf <bind_socket+0x13f>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     12c5 <bind_socket+0x135>
lea    0xd88(%rip),%rdi
call   1080 <perror@plt>
mov    -0x50(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     12c0 <bind_socket+0x130>
lea    0xd76(%rip),%rdi
call   1080 <perror@plt>
jmp    12ca <bind_socket+0x13a>
jmp    12e5 <bind_socket+0x155>
jmp    12cf <bind_socket+0x13f>
jmp    12d4 <bind_socket+0x144>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    122e <bind_socket+0x9e>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x48(%rbp)
jne    131c <bind_socket+0x18c>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <bind_socket+0x192>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1218 <bind_socket+0x88>
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdfb(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    128f <bind_socket+0xff>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     129e <bind_socket+0x10e>
lea    0xde9(%rip),%r15
lea    0xdf3(%rip),%r14
lea    0xdfb(%rip),%r12
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
mov    %r15,%rdi
cmp    $0xffffffff,%eax
je     127f <bind_socket+0xef>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <bind@plt>
test   %eax,%eax
je     1286 <bind_socket+0xf6>
mov    %r14,%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12,%rdi
test   %eax,%eax
jne    127f <bind_socket+0xef>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1236 <bind_socket+0xa6>
jmp    12c1 <bind_socket+0x131>
call   1080 <perror@plt>
jmp    1274 <bind_socket+0xe4>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1211 <bind_socket+0x81>
mov    (%rsp),%rdi
jmp    12a0 <bind_socket+0x110>

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