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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINTR;
scalar_t__ EINVAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ host_addr(char *);
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bind_port(char *host, int port) {
  int sd = 0;
  struct sockaddr_in addr;
  int one = 1;
  (memset((char *)&addr, '\0', sizeof(addr)));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = host_addr(host);
  if (errno == EINVAL) {
    perror(host);
    return (-1);
  }
  if (sizeof(addr.sin_port) == sizeof(short)) {
    addr.sin_port = htons(port);
  } else {
    addr.sin_port = htonl(port);
  }
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror(host);
    return (-1);
  }
  setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
  if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(sd);
    perror("bind()");
    return (-1);
  }
  while (listen(sd, 3) == -1) {
    if (errno != EINTR) {
      return (-1);
    }
  }
  return (sd);
}


x86 assembly (clang, -O0):
<bind_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <host_addr@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%rax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <bind_port+0x74>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f4 <bind_port+0x164>
mov    -0x14(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1249 <bind_port+0xb9>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f4 <bind_port+0x164>
mov    -0x18(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12a8 <bind_port+0x118>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xd69(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f4 <bind_port+0x164>
jmp    12ad <bind_port+0x11d>
mov    -0x18(%rbp),%edi
mov    $0x3,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ee <bind_port+0x15e>
mov    0x2cfe(%rip),%rax
mov    (%rax),%rax
mov    0x2cec(%rip),%rcx
cmp    (%rcx),%rax
je     12e9 <bind_port+0x159>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f4 <bind_port+0x164>
jmp    12ad <bind_port+0x11d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_port>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
movl   $0x1,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e20(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %r14,%rdi
call   1070 <host_addr@plt>
mov    %eax,0xc(%rsp)
mov    0x2ded(%rip),%r15
mov    (%r15),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     1289 <bind_port+0xf9>
mov    %ebx,%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    (%r12),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1289 <bind_port+0xf9>
mov    %eax,%ebx
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     127b <bind_port+0xeb>
mov    0x2d6c(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    $0x3,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1296 <bind_port+0x106>
mov    (%r15),%rax
cmp    (%r14),%rax
je     1260 <bind_port+0xd0>
jmp    1291 <bind_port+0x101>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xd77(%rip),%r14
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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
@errno = global i64 0
@EINVAL = global i64 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [7 x i8] c"bind()\00"
@EINTR = global i64 0
define i32 @bind_port(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i32 1, i32* %8
%9 = bitcast %struct.sockaddr_in* %7 to i8*
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i8*, i8** %4
%14 = call i32 @host_addr(i8* %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINVAL
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32 @perror(i8* %21)
store i32 -1, i32* %3
br label %61
23: ; preds = %2
%24 = load i32, i32* %5
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %6
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = load i8*, i8** %4
%33 = call i32 @perror(i8* %32)
store i32 -1, i32* %3
br label %61
34: ; preds = %23
%35 = load i32, i32* %6
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_REUSEADDR
%38 = bitcast i32* %8 to i8*
%39 = call i32 @setsockopt(i32 %35, i32 %36, i32 %37, i8* %38, i32 4)
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %34
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
48: ; preds = %34
br label %49
49: ; preds = %58, %48
%50 = load i32, i32* %6
%51 = call i32 @listen(i32 %50, i32 3)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %59
53: ; preds = %49
%54 = load i64, i64* @errno
%55 = load i64, i64* @EINTR
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 -1, i32* %3
br label %61
58: ; preds = %53
br label %49
59: ; preds = %49
%60 = load i32, i32* %6
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %57, %44, %31, %20
%62 = load i32, i32* %3
ret i32 %62
}

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
@errno = global i64 0
@EINVAL = global i64 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [7 x i8] c"bind()\00"
@EINTR = global i64 0
define i32 @bind_port(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = call i32 @memset(i8* %5, i8 signext 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @host_addr(i8* %0)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %10, i32* %11
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINVAL
%14 = icmp eq i64 %12, %13
br i1 %14, label %38, label %15
15: ; preds = %2
%16 = call i32 @htonl(i32 %1)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %38, label %22
22: ; preds = %15
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 @setsockopt(i32 %20, i32 %23, i32 %24, i8* %6, i32 4)
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i64 @bind(i32 %20, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @close(i32 %20)
br label %38
31: ; preds = %22, %34
%32 = call i32 @listen(i32 %20, i32 3)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %31, label %41
38: ; preds = %15, %2, %29
%39 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %29 ], [ %0, %2 ], [ %0, %15 ]
%40 = call i32 @perror(i8* %39)
br label %41
41: ; preds = %31, %34, %38
%42 = phi i32 [ -1, %38 ], [ %20, %31 ], [ -1, %34 ]
ret i32 %42
}