#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ yes;
typedef scalar_t__ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ listen_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(scalar_t__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, unsigned int *,
               int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bind_socket(uint16_t listen_port, uint16_t blocking) {
  int fd;
  int result;
  unsigned int yes = 1;
  struct sockaddr_in listen_addr;
  fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (fd < 0) {
    perror("socket.c socket()");
    return fd;
  }
  result = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));
  if (result < 0) {
    perror("socket.c setsockopt(SO_BROADCAST)");
    return result;
  }
  result = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  if (result < 0) {
    perror("socket.c setsockopt(SO_REUSEADDR)");
    return result;
  }
  if (blocking == 0) {
    result = fcntl(fd, F_SETFL, O_NONBLOCK);
    if (result == -1) {
      perror("socket.c fcntl()");
      return result;
    }
  }
  memset(&listen_addr, 0, sizeof(listen_addr));
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_port = htons(listen_port);
  listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  result = bind(fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr));
  if (result < 0) {
    perror("socket.c bind()");
    return result;
  }
  return fd;
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11db <bind_socket+0x5b>
lea    0xe35(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <bind_socket+0x1ad>
mov    -0x1c(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1223 <bind_socket+0xa3>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <bind_socket+0x1ad>
mov    -0x1c(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    126b <bind_socket+0xeb>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <bind_socket+0x1ad>
cmpq   $0x0,-0x18(%rbp)
jne    12b9 <bind_socket+0x139>
mov    -0x1c(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12b4 <bind_socket+0x134>
lea    0xdb2(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <bind_socket+0x1ad>
jmp    12b9 <bind_socket+0x139>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1327 <bind_socket+0x1a7>
lea    0xd50(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <bind_socket+0x1ad>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
movl   $0x1,0x4(%rsp)
mov    0x2e3d(%rip),%r12
mov    (%r12),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12a2 <bind_socket+0x122>
mov    0x2e19(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     12b0 <bind_socket+0x130>
mov    0x0(%r13),%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     12bb <bind_socket+0x13b>
test   %r15,%r15
jne    1244 <bind_socket+0xc4>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12d8 <bind_socket+0x158>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %r14,%rdi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %eax,%eax
js     12c6 <bind_socket+0x146>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd57(%rip),%rdi
call   1090 <perror@plt>
jmp    1291 <bind_socket+0x111>
mov    %eax,%ebp
lea    0xd59(%rip),%rdi
jmp    12cf <bind_socket+0x14f>
mov    %eax,%ebp
lea    0xd70(%rip),%rdi
jmp    12cf <bind_socket+0x14f>
mov    %eax,%ebp
lea    0xd98(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%ebx
jmp    1291 <bind_socket+0x111>
lea    0xd77(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1291 <bind_socket+0x111>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [18 x i8] c"socket.c socket()\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.1 = [34 x i8] c"socket.c setsockopt(SO_BROADCAST)\00"
@SO_REUSEADDR = global i32 0
@.str.2 = [34 x i8] c"socket.c setsockopt(SO_REUSEADDR)\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [17 x i8] c"socket.c fcntl()\00"
@INADDR_ANY = global i32 0
@.str.4 = [16 x i8] c"socket.c bind()\00"
define i32 @bind_socket(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i64 %0, i64* %4
store i64 %1, i64* %5
store i32 1, i32* %8
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %74
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_BROADCAST
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %8, i32 4)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %7
store i32 %28, i32* %3
br label %74
29: ; preds = %19
%30 = load i32, i32* %6
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_REUSEADDR
%33 = call i32 @setsockopt(i32 %30, i32 %31, i32 %32, i32* %8, i32 4)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %74
39: ; preds = %29
%40 = load i64, i64* %5
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %53
42: ; preds = %39
%43 = load i32, i32* %6
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* @O_NONBLOCK
%46 = call i32 @fcntl(i32 %43, i32 %44, i32 %45)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* %7
store i32 %51, i32* %3
br label %74
52: ; preds = %42
br label %53
53: ; preds = %52, %39
%54 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%55 = load i32, i32* @AF_INET
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %55, i32* %56
%57 = load i64, i64* %4
%58 = call i32 @htons(i64 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %58, i32* %59
%60 = load i32, i32* @INADDR_ANY
%61 = call i32 @htonl(i32 %60)
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
store i32 %61, i32* %63
%64 = load i32, i32* %6
%65 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%66 = call i32 @bind(i32 %64, %struct.sockaddr* %65, i32 12)
store i32 %66, i32* %7
%67 = load i32, i32* %7
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %53
%70 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%71 = load i32, i32* %7
store i32 %71, i32* %3
br label %74
72: ; preds = %53
%73 = load i32, i32* %6
store i32 %73, i32* %3
br label %74
74: ; preds = %72, %69, %49, %36, %26, %16
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [18 x i8] c"socket.c socket()\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.1 = [34 x i8] c"socket.c setsockopt(SO_BROADCAST)\00"
@SO_REUSEADDR = global i32 0
@.str.2 = [34 x i8] c"socket.c setsockopt(SO_REUSEADDR)\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [17 x i8] c"socket.c fcntl()\00"
@INADDR_ANY = global i32 0
@.str.4 = [16 x i8] c"socket.c bind()\00"
define i32 @bind_socket(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = tail call i32 @socket(i32 %7, i32 %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %51
14: ; preds = %2
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_BROADCAST
%17 = call i32 @setsockopt(i32 %10, i32 %15, i32 %16, i32* %3, i32 4)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %51
21: ; preds = %14
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %10, i32 %22, i32 %23, i32* %3, i32 4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %51
28: ; preds = %21
%29 = icmp eq i64 %1, 0
br i1 %29, label %30, label %37
30: ; preds = %28
%31 = load i32, i32* @F_SETFL
%32 = load i32, i32* @O_NONBLOCK
%33 = call i32 @fcntl(i32 %10, i32 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %51
37: ; preds = %30, %28
%38 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %39, i32* %40
%41 = call i32 @htons(i64 %0)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %41, i32* %42
%43 = load i32, i32* @INADDR_ANY
%44 = call i32 @htonl(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %44, i32* %45
%46 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%47 = call i32 @bind(i32 %10, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %37
%50 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %37, %49, %35, %26, %19, %12
%52 = phi i32 [ %10, %12 ], [ %17, %19 ], [ %24, %26 ], [ -1, %35 ], [ %47, %49 ], [ %10, %37 ]
ret i32 %52
}