#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_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ ops;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int pb_server(int port) {
  struct sockaddr_in server;
  int s, ops = 1;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("pb_server (socket):");
    return -1;
  }
  server.sin_family = AF_INET;
  server.sin_port = htons(port);
  server.sin_addr.s_addr = INADDR_ANY;
  if ((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &ops, sizeof(ops))) < 0)
    perror("pb_server (reuseaddr):");
  if ((bind(s, (struct sockaddr *)&server, sizeof(server))) < 0) {
    perror("pb_server (bind):");
    goto cleanup;
  }
  if ((listen(s, 10)) < 0) {
    perror("pb_server (listen):");
    goto cleanup;
  }
  return s;
cleanup:
  close(s);
  return -1;
}


x86 assembly (clang, -O0):
<pb_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11bf <pb_server+0x4f>
lea    0xe52(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <pb_server+0x11a>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    121c <pb_server+0xac>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1248 <pb_server+0xd8>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
jmp    127b <pb_server+0x10b>
mov    -0x1c(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1270 <pb_server+0x100>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
jmp    127b <pb_server+0x10b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128a <pb_server+0x11a>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pb_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e57(%rip),%r14
mov    (%r14),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     121c <pb_server+0xac>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     122a <pb_server+0xba>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1238 <pb_server+0xc8>
mov    %ebx,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
test   %rax,%rax
js     1241 <pb_server+0xd1>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
jmp    1254 <pb_server+0xe4>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
jmp    11ea <pb_server+0x7a>
lea    0xdec(%rip),%rdi
jmp    1248 <pb_server+0xd8>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    1211 <pb_server+0xa1>

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

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