#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;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ listen(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int bind_socket(int fd, struct addrinfo *aip) {
  int y = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(int))) {
    return 100;
  }
  int ret;
  if ((ret = fcntl(fd, F_SETFL, O_NONBLOCK)) == -1) {
    close(fd);
    return 101;
    ;
  }
  if (bind(fd, aip->ai_addr, aip->ai_addrlen) == -1) {
    return 111;
  }
  if (listen(fd, 5)) {
    return 122;
  }
  return 0;
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11a0 <bind_socket+0x50>
movl   $0x64,-0x4(%rbp)
jmp    122e <bind_socket+0xde>
mov    -0x8(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11da <bind_socket+0x8a>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x65,-0x4(%rbp)
jmp    122e <bind_socket+0xde>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1204 <bind_socket+0xb4>
movl   $0x6f,-0x4(%rbp)
jmp    122e <bind_socket+0xde>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     1227 <bind_socket+0xd7>
movl   $0x7a,-0x4(%rbp)
jmp    122e <bind_socket+0xde>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rax,%rcx
mov    $0x64,%eax
test   %rcx,%rcx
je     1198 <bind_socket+0x48>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11e4 <bind_socket+0x94>
mov    (%r14),%edx
mov    0x4(%r14),%esi
mov    %ebx,%edi
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     11f2 <bind_socket+0xa2>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
xor    %ecx,%ecx
test   %rax,%rax
mov    $0x7a,%eax
cmove  %ecx,%eax
jmp    1190 <bind_socket+0x40>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x65,%eax
jmp    1190 <bind_socket+0x40>
mov    $0x6f,%eax
jmp    1190 <bind_socket+0x40>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @bind_socket(i32 %0, %struct.addrinfo* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store %struct.addrinfo* %1, %struct.addrinfo** %5
store i32 1, i32* %6
%8 = load i32, i32* %4
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_REUSEADDR
%11 = call i64 @setsockopt(i32 %8, i32 %9, i32 %10, i32* %6, i32 4)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 100, i32* %3
br label %40
14: ; preds = %2
%15 = load i32, i32* %4
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* @O_NONBLOCK
%18 = call i32 @fcntl(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
store i32 101, i32* %3
br label %40
23: ; preds = %14
%24 = load i32, i32* %4
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load %struct.addrinfo*, %struct.addrinfo** %5
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @bind(i32 %24, i32 %27, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %23
store i32 111, i32* %3
br label %40
34: ; preds = %23
%35 = load i32, i32* %4
%36 = call i64 @listen(i32 %35, i32 5)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 122, i32* %3
br label %40
39: ; preds = %34
store i32 0, i32* %3
br label %40
40: ; preds = %39, %38, %33, %20, %13
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @bind_socket(i32 %0, %struct.addrinfo* nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%5 = load i32, i32* @SOL_SOCKET
%6 = load i32, i32* @SO_REUSEADDR
%7 = call i64 @setsockopt(i32 %0, i32 %5, i32 %6, i32* %3, i32 4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %2
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @O_NONBLOCK
%12 = call i32 @fcntl(i32 %0, i32 %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @close(i32 %0)
br label %27
16: ; preds = %9
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @bind(i32 %0, i32 %18, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = call i64 @listen(i32 %0, i32 5)
%25 = icmp eq i64 %24, 0
%26 = select i1 %25, i32 0, i32 122
br label %27
27: ; preds = %14, %16, %23, %2
%28 = phi i32 [ 100, %2 ], [ 101, %14 ], [ 111, %16 ], [ %26, %23 ]
ret i32 %28
}