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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
unsigned short INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
void *htons(unsigned short);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bind_and_listen(unsigned short port) {
  int fd, rc;
  int opt = 1;
  struct sockaddr_in addr = {0};
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htons(INADDR_ANY);
  addr.sin_port = htons(port);
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    perror("socket");
    return -1;
  }
  rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  if (rc == -1) {
    perror("setsockopt");
    close(fd);
    return -1;
  }
  rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (rc == -1) {
    perror("bind");
    close(fd);
    return -1;
  }
  rc = listen(fd, SOMAXCONN);
  if (rc == -1) {
    perror("listen");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<bind_and_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
movl   $0x1,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e1f(%rip),%rax
movzwl (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x28(%rbp)
movzwl -0x6(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1210 <bind_and_listen+0x90>
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <bind_and_listen+0x163>
mov    -0xc(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1261 <bind_and_listen+0xe1>
lea    0xdbf(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <bind_and_listen+0x163>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    129f <bind_and_listen+0x11f>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <bind_and_listen+0x163>
mov    -0xc(%rbp),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12dd <bind_and_listen+0x15d>
lea    0xd53(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <bind_and_listen+0x163>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_and_listen>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
movl   $0x1,0xc(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e4f(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x20(%rsp)
mov    0x2e39(%rip),%rax
movzwl (%rax),%edi
call   1040 <htons@plt>
mov    %rax,0x18(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %rax,0x10(%rsp)
mov    (%r14),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     122f <bind_and_listen+0xbf>
mov    %eax,%ebx
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     123d <bind_and_listen+0xcd>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1246 <bind_and_listen+0xd6>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    125e <bind_and_listen+0xee>
lea    0xdea(%rip),%rdi
jmp    124d <bind_and_listen+0xdd>
lea    0xdca(%rip),%rdi
call   1080 <perror@plt>
jmp    1259 <bind_and_listen+0xe9>
lea    0xdc3(%rip),%rdi
jmp    124d <bind_and_listen+0xdd>
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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