#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 sockaddr_storage {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int __socket(struct addrinfo *, int, int);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int __socket_bind(const char *address, const char *port, struct addrinfo *hints,
                  struct sockaddr_storage *addr) {
  struct addrinfo *info;
  struct addrinfo *p;
  int status;
  int sock;
  if ((status = getaddrinfo(address, port, hints, &info)) != 0) {
    fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(status));
    return (-1);
  }
  for (p = info; p != NULL; p = p->ai_next) {
    if ((sock = __socket(p, 1, 1)) < 0) {
      perror("socket()");
      continue;
    }
    if (bind(sock, info->ai_addr, info->ai_addrlen) < 0) {
      perror("bind()");
      close(sock);
      sock = -1;
      continue;
    }
    if (addr != NULL)
      memcpy(addr, p->ai_addr, p->ai_addrlen);
    break;
  }
  freeaddrinfo(info);
  return (sock);
}


x86 assembly (clang, -O0):
<__socket_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     11fb <__socket_bind+0x6b>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe16(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <__socket_bind+0x130>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12b1 <__socket_bind+0x121>
mov    -0x38(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <__socket@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jge    123b <__socket_bind+0xab>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
jmp    12a0 <__socket_bind+0x110>
mov    -0x40(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    127a <__socket_bind+0xea>
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
mov    -0x40(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x40(%rbp)
jmp    12a0 <__socket_bind+0x110>
cmpq   $0x0,-0x28(%rbp)
je     129b <__socket_bind+0x10b>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
jmp    12b1 <__socket_bind+0x121>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    1203 <__socket_bind+0x73>
mov    -0x30(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__socket_bind>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
lea    0x8(%rsp),%rcx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     11d8 <__socket_bind+0x48>
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe3c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1265 <__socket_bind+0xd5>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     125b <__socket_bind+0xcb>
lea    0xe2a(%rip),%r15
lea    0xe2c(%rip),%r12
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1090 <__socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     123d <__socket_bind+0xad>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1070 <bind@plt>
test   %rax,%rax
jns    1247 <__socket_bind+0xb7>
mov    %r12,%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    11f0 <__socket_bind+0x60>
jmp    125b <__socket_bind+0xcb>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1232 <__socket_bind+0xa2>
test   %r14,%r14
je     125b <__socket_bind+0xcb>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1060 <memcpy@plt>
jmp    125b <__socket_bind+0xcb>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo* }
%struct.sockaddr_storage = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"getaddrinfo(): %s\0A\00"
@.str.1 = [9 x i8] c"socket()\00"
@.str.2 = [7 x i8] c"bind()\00"
define i32 @__socket_bind(i8* %0, i8* %1, %struct.addrinfo* %2, %struct.sockaddr_storage* %3) {
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo** %5 to i8*
%7 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %2, %struct.addrinfo** %5)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load %struct.addrinfo*, %struct.addrinfo** %5
%11 = icmp eq %struct.addrinfo* %10, null
br i1 %11, label %46, label %16
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = call i8* @gai_strerror(i32 %7)
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %50
16: ; preds = %9, %41
%17 = phi %struct.addrinfo* [ %44, %41 ], [ %10, %9 ]
%18 = call i32 @__socket(%struct.addrinfo* %17, i32 1, i32 1)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %41
22: ; preds = %16
%23 = load %struct.addrinfo*, %struct.addrinfo** %5
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @bind(i32 %18, i32 %25, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @close(i32 %18)
br label %41
33: ; preds = %22
%34 = icmp eq %struct.sockaddr_storage* %3, null
br i1 %34, label %46, label %35
35: ; preds = %33
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @memcpy(%struct.sockaddr_storage* %3, i32 %37, i32 %39)
br label %46
41: ; preds = %30, %20
%42 = phi i32 [ %18, %20 ], [ -1, %30 ]
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 2
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %16
46: ; preds = %41, %9, %33, %35
%47 = phi i32 [ %18, %35 ], [ %18, %33 ], [ undef, %9 ], [ %42, %41 ]
%48 = load %struct.addrinfo*, %struct.addrinfo** %5
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
br label %50
50: ; preds = %46, %12
%51 = phi i32 [ -1, %12 ], [ %47, %46 ]
ret i32 %51
}