#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;
};
typedef int /*<<< orphan*/ reuseaddr_on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
struct addrinfo *_nxweb_resolve_host(char const *, int);
scalar_t__ _nxweb_set_non_block(int);
scalar_t__ _nxweb_setup_listening_socket(int);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ nxweb_log_error(char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int _nxweb_bind_socket(const char *host_and_port, int backlog) {
  struct addrinfo *ai = _nxweb_resolve_host(host_and_port, 1);
  if (!ai) {
    nxweb_log_error("can't resolve IP/port %d", errno);
    return -1;
  }
  int listen_fd;
  int reuseaddr_on = 1;
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_fd < 0) {
    nxweb_log_error("socket() failed %d", errno);
    return -1;
  }
  if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on,
                 sizeof(reuseaddr_on)) == -1) {
    nxweb_log_error("setsockopt() failed %d", errno);
    return -1;
  }
  if (bind(listen_fd, ai->ai_addr, ai->ai_addrlen) < 0) {
    nxweb_log_error("bind failed");
    return -1;
  }
  freeaddrinfo(ai);
  if (listen(listen_fd, backlog) < 0) {
    nxweb_log_error("listen() failed %d", errno);
    return -1;
  }
  if (_nxweb_set_non_block(listen_fd) < 0) {
    nxweb_log_error("failed to set server socket to non-blocking %d", errno);
    return -1;
  }
  if (_nxweb_setup_listening_socket(listen_fd) < 0) {
    nxweb_log_error("failed to setup listening socket");
    return -1;
  }
  return listen_fd;
}


x86 assembly (clang, -O0):
<_nxweb_bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <_nxweb_resolve_host@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <_nxweb_bind_socket+0x4f>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1090 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <_nxweb_bind_socket+0x1d0>
movl   $0x1,-0x28(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    122f <_nxweb_bind_socket+0x9f>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1090 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <_nxweb_bind_socket+0x1d0>
mov    -0x24(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    127f <_nxweb_bind_socket+0xef>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1090 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <_nxweb_bind_socket+0x1d0>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    12b8 <_nxweb_bind_socket+0x128>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1090 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <_nxweb_bind_socket+0x1d0>
mov    -0x20(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    12f9 <_nxweb_bind_socket+0x169>
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1090 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <_nxweb_bind_socket+0x1d0>
mov    -0x24(%rbp),%edi
call   1080 <_nxweb_set_non_block@plt>
cmp    $0x0,%rax
jge    132e <_nxweb_bind_socket+0x19e>
mov    0x2cb6(%rip),%rax
mov    (%rax),%esi
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1090 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <_nxweb_bind_socket+0x1d0>
mov    -0x24(%rbp),%edi
call   1070 <_nxweb_setup_listening_socket@plt>
cmp    $0x0,%rax
jge    135a <_nxweb_bind_socket+0x1ca>
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1090 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <_nxweb_bind_socket+0x1d0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nxweb_bind_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    $0x1,%esi
call   1030 <_nxweb_resolve_host@plt>
test   %rax,%rax
je     124f <_nxweb_bind_socket+0xbf>
mov    %rax,%rbp
movl   $0x1,0xc(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1261 <_nxweb_bind_socket+0xd1>
mov    %eax,%ebx
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1273 <_nxweb_bind_socket+0xe3>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   1060 <bind@plt>
test   %rax,%rax
js     1285 <_nxweb_bind_socket+0xf5>
mov    %rbp,%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    %r14d,%esi
call   1050 <listen@plt>
test   %rax,%rax
js     1295 <_nxweb_bind_socket+0x105>
mov    %ebx,%edi
call   1080 <_nxweb_set_non_block@plt>
test   %rax,%rax
js     12a7 <_nxweb_bind_socket+0x117>
mov    %ebx,%edi
call   1070 <_nxweb_setup_listening_socket@plt>
test   %rax,%rax
jns    12c3 <_nxweb_bind_socket+0x133>
lea    0xe44(%rip),%rdi
jmp    128c <_nxweb_bind_socket+0xfc>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
lea    0xda1(%rip),%rdi
jmp    12b7 <_nxweb_bind_socket+0x127>
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
lea    0xda8(%rip),%rdi
jmp    12b7 <_nxweb_bind_socket+0x127>
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    0xda9(%rip),%rdi
jmp    12b7 <_nxweb_bind_socket+0x127>
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1090 <nxweb_log_error@plt>
jmp    12be <_nxweb_bind_socket+0x12e>
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
lea    0xdaa(%rip),%rdi
jmp    12b7 <_nxweb_bind_socket+0x127>
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
lea    0xdab(%rip),%rdi
xor    %eax,%eax
call   1090 <nxweb_log_error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@.str = [25 x i8] c"can't resolve IP/port %d\00"
@errno = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [19 x i8] c"socket() failed %d\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [23 x i8] c"setsockopt() failed %d\00"
@.str.3 = [12 x i8] c"bind failed\00"
@.str.4 = [19 x i8] c"listen() failed %d\00"
@.str.5 = [47 x i8] c"failed to set server socket to non-blocking %d\00"
@.str.6 = [33 x i8] c"failed to setup listening socket\00"
define i32 @_nxweb_bind_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.addrinfo* @_nxweb_resolve_host(i8* %9, i32 1)
store %struct.addrinfo* %10, %struct.addrinfo** %6
%11 = load %struct.addrinfo*, %struct.addrinfo** %6
%12 = icmp ne %struct.addrinfo* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 -1, i32* %3
br label %71
16: ; preds = %2
store i32 1, i32* %8
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* @errno
%24 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 -1, i32* %3
br label %71
25: ; preds = %16
%26 = load i32, i32* %7
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i32 @setsockopt(i32 %26, i32 %27, i32 %28, i32* %8, i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i32, i32* @errno
%33 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %32)
store i32 -1, i32* %3
br label %71
34: ; preds = %25
%35 = load i32, i32* %7
%36 = load %struct.addrinfo*, %struct.addrinfo** %6
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %6
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i64 @bind(i32 %35, i32 %38, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %34
%45 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
46: ; preds = %34
%47 = load %struct.addrinfo*, %struct.addrinfo** %6
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
%49 = load i32, i32* %7
%50 = load i32, i32* %5
%51 = call i64 @listen(i32 %49, i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = load i32, i32* @errno
%55 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %54)
store i32 -1, i32* %3
br label %71
56: ; preds = %46
%57 = load i32, i32* %7
%58 = call i64 @_nxweb_set_non_block(i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = load i32, i32* @errno
%62 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i32 %61)
store i32 -1, i32* %3
br label %71
63: ; preds = %56
%64 = load i32, i32* %7
%65 = call i64 @_nxweb_setup_listening_socket(i32 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
69: ; preds = %63
%70 = load i32, i32* %7
store i32 %70, i32* %3
br label %71
71: ; preds = %69, %67, %60, %53, %44, %31, %22, %13
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@.str = [25 x i8] c"can't resolve IP/port %d\00"
@errno = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [19 x i8] c"socket() failed %d\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [23 x i8] c"setsockopt() failed %d\00"
@.str.3 = [12 x i8] c"bind failed\00"
@.str.4 = [19 x i8] c"listen() failed %d\00"
@.str.5 = [47 x i8] c"failed to set server socket to non-blocking %d\00"
@.str.6 = [33 x i8] c"failed to setup listening socket\00"
define i32 @_nxweb_bind_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = tail call %struct.addrinfo* @_nxweb_resolve_host(i8* %0, i32 1)
%5 = icmp eq %struct.addrinfo* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %55
9: ; preds = %2
%10 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %53
18: ; preds = %9
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %13, i32 %19, i32 %20, i32* %3, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %53
26: ; preds = %18
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @bind(i32 %13, i32 %28, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %53
35: ; preds = %26
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %4)
%37 = call i64 @listen(i32 %13, i32 %1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32, i32* @errno
%41 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %40)
br label %53
42: ; preds = %35
%43 = call i64 @_nxweb_set_non_block(i32 %13)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @errno
%47 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i32 %46)
br label %53
48: ; preds = %42
%49 = call i64 @_nxweb_setup_listening_socket(i32 %13)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %48, %51, %45, %39, %33, %23, %15
%54 = phi i32 [ -1, %15 ], [ -1, %23 ], [ -1, %33 ], [ -1, %39 ], [ -1, %45 ], [ -1, %51 ], [ %13, %48 ]
br label %55
55: ; preds = %53, %6
%56 = phi i32 [ %54, %53 ], [ -1, %6 ]
ret i32 %56
}