#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*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/ const);
int /*<<< orphan*/ inet_addr(char const *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int httpserver_bindsocket(const char *listen_addr, const uint16_t port,
                          int backlog) {
  int r;
  int nfd;
  nfd = socket(AF_INET, SOCK_STREAM, 0);
  if (nfd < 0)
    return -1;
  int one = 1;
  r = setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(int));
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr(listen_addr);
  addr.sin_port = htons(port);
  r = bind(nfd, (struct sockaddr *)&addr, sizeof(addr));
  if (r < 0)
    return -1;
  r = listen(nfd, backlog);
  if (r < 0)
    return -1;
  int flags;
  if ((flags = fcntl(nfd, F_GETFL, 0)) < 0 ||
      fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
    return -1;
  }
  return (nfd);
}


x86 assembly (clang, -O0):
<httpserver_bindsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c4 <httpserver_bindsocket+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <httpserver_bindsocket+0x14a>
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e13(%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,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    124f <httpserver_bindsocket+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <httpserver_bindsocket+0x14a>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1273 <httpserver_bindsocket+0xf3>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <httpserver_bindsocket+0x14a>
mov    -0x20(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jl     12b8 <httpserver_bindsocket+0x138>
mov    -0x20(%rbp),%edi
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
mov    -0x34(%rbp),%edx
mov    0x2d30(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    12c4 <httpserver_bindsocket+0x144>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <httpserver_bindsocket+0x14a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<httpserver_bindsocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e42(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     127f <httpserver_bindsocket+0xff>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %rbp,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1284 <httpserver_bindsocket+0x104>
mov    %ebx,%edi
mov    %r14d,%esi
call   1080 <listen@plt>
test   %eax,%eax
js     1284 <httpserver_bindsocket+0x104>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1284 <httpserver_bindsocket+0x104>
mov    0x2d69(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d70(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
mov    $0xffffffff,%ebp
cmovns %ebx,%ebp
jmp    1284 <httpserver_bindsocket+0x104>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @httpserver_bindsocket(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %64
19: ; preds = %3
store i32 1, i32* %10
%20 = load i32, i32* %9
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = bitcast i32* %10 to i8*
%24 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i8* %23, i32 4)
store i32 %24, i32* %8
%25 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i8*, i8** %5
%29 = call i32 @inet_addr(i8* %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* %6
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %9
%36 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%37 = call i32 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %19
store i32 -1, i32* %4
br label %64
41: ; preds = %19
%42 = load i32, i32* %9
%43 = load i32, i32* %7
%44 = call i32 @listen(i32 %42, i32 %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 -1, i32* %4
br label %64
48: ; preds = %41
%49 = load i32, i32* %9
%50 = load i32, i32* @F_GETFL
%51 = call i32 @fcntl(i32 %49, i32 %50, i32 0)
store i32 %51, i32* %12
%52 = icmp slt i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %48
%54 = load i32, i32* %9
%55 = load i32, i32* @F_SETFL
%56 = load i32, i32* %12
%57 = load i32, i32* @O_NONBLOCK
%58 = or i32 %56, %57
%59 = call i32 @fcntl(i32 %54, i32 %55, i32 %58)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %53, %48
store i32 -1, i32* %4
br label %64
62: ; preds = %53
%63 = load i32, i32* %9
store i32 %63, i32* %4
br label %64
64: ; preds = %62, %61, %47, %40, %18
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @httpserver_bindsocket(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %42, label %10
10: ; preds = %3
%11 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %8, i32 %12, i32 %13, i8* %11, i32 4)
%15 = bitcast %struct.sockaddr_in* %5 to i8*
%16 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %1)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i32 @bind(i32 %8, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i32 %24, 0
br i1 %25, label %40, label %26
26: ; preds = %10
%27 = call i32 @listen(i32 %8, i32 %2)
%28 = icmp slt i32 %27, 0
br i1 %28, label %40, label %29
29: ; preds = %26
%30 = load i32, i32* @F_GETFL
%31 = call i32 @fcntl(i32 %8, i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %29
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* @O_NONBLOCK
%36 = or i32 %35, %31
%37 = call i32 @fcntl(i32 %8, i32 %34, i32 %36)
%38 = icmp sgt i32 %37, -1
%39 = select i1 %38, i32 %8, i32 -1
br label %40
40: ; preds = %33, %29, %26, %10
%41 = phi i32 [ -1, %10 ], [ -1, %26 ], [ -1, %29 ], [ %39, %33 ]
br label %42
42: ; preds = %3, %40
%43 = phi i32 [ %41, %40 ], [ -1, %3 ]
ret i32 %43
}