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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ htonl(unsigned long const);
int /*<<< orphan*/ htons(int const);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, size_t);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int BindPassiveSocket(const unsigned long interface, const int portNum,
                      int *const boundSocket) {
  struct sockaddr_in sin;
  struct protoent *proto;
  int newsock, optval;
  size_t optlen;
  if ((proto = getprotobyname("tcp")) == NULL)
    return -1;
  memset(&sin.sin_zero, 0, 8);
  sin.sin_port = htons(portNum);
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(interface);
  if ((newsock = socket(PF_INET, SOCK_STREAM, proto->p_proto)) < 0)
    return -1;
  optval = 1;
  optlen = sizeof(int);
  setsockopt(newsock, SOL_SOCKET, SO_REUSEADDR, &optval, optlen);
  if (bind(newsock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0)
    return -1;
  if (listen(newsock, SOMAXCONN) < 0)
    return -1;
  *boundSocket = newsock;
  return 0;
}


x86 assembly (clang, -O0):
<BindPassiveSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe66(%rip),%rdi
call   1080 <getprotobyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11b9 <BindPassiveSocket+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <BindPassiveSocket+0x134>
lea    -0x30(%rbp),%rdi
add    $0xc,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <htonl@plt>
mov    %eax,-0x30(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    1225 <BindPassiveSocket+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <BindPassiveSocket+0x134>
movl   $0x1,-0x40(%rbp)
movq   $0x4,-0x48(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    -0x48(%rbp),%r8
lea    -0x40(%rbp),%rcx
call   1030 <setsockopt@plt>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    127d <BindPassiveSocket+0xfd>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <BindPassiveSocket+0x134>
mov    -0x3c(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12a4 <BindPassiveSocket+0x124>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <BindPassiveSocket+0x134>
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BindPassiveSocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r12
lea    0xe65(%rip),%rdi
call   1080 <getprotobyname@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1264 <BindPassiveSocket+0xe4>
mov    %rax,%rbp
lea    0x1c(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r12,%rdi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x0(%rbp),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1264 <BindPassiveSocket+0xe4>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
mov    %ebp,%edi
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1264 <BindPassiveSocket+0xe4>
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <listen@plt>
test   %rax,%rax
js     1264 <BindPassiveSocket+0xe4>
mov    %ebp,(%r14)
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"tcp\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOMAXCONN = global i32 0
define i32 @BindPassiveSocket(i64 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq %struct.protoent* %8, null
br i1 %9, label %37, label %10
10: ; preds = %3
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
%12 = call i32 @memset(i32* %11, i32 0, i32 8)
%13 = call i32 @htons(i32 %1)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @htonl(i64 %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = getelementptr inbounds %struct.protoent, %struct.protoent* %8, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @socket(i32 %19, i32 %20, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %37, label %25
25: ; preds = %10
store i32 1, i32* %5
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = call i32 @setsockopt(i32 %23, i32 %26, i32 %27, i32* %5, i64 4)
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i64 @bind(i32 %23, %struct.sockaddr* %29, i32 16)
%31 = icmp slt i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %25
%33 = load i32, i32* @SOMAXCONN
%34 = call i64 @listen(i32 %23, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32
store i32 %23, i32* %2
br label %37
37: ; preds = %32, %25, %10, %3, %36
%38 = phi i32 [ 0, %36 ], [ -1, %3 ], [ -1, %10 ], [ -1, %25 ], [ -1, %32 ]
ret i32 %38
}