#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*/ servaddr;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ 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*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int listen(int, int /*<<< orphan*/);
int make_fd_nonblock(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int server_create(unsigned short port) {
  int servfd;
  struct sockaddr_in servaddr;
  servfd = socket(AF_INET, SOCK_STREAM, 0);
  if (servfd == -1)
    do {
      perror("socket(SOCK_STREAM)");
      return -1;
    } while (0);
  int opt = 1;
  setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  if (make_fd_nonblock(servfd) == -1)
    do {
      fprintf(stderr, "%s\n", "failed to set servfd nonblock");
      return -1;
    } while (0);
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(port);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    do {
      perror("bind");
      return -1;
    } while (0);
  if (listen(servfd, SOMAXCONN) == -1)
    do {
      perror("listen");
      return -1;
    } while (0);
  return servfd;
}


x86 assembly (clang, -O0):
<server_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11f2 <server_create+0x52>
jmp    11da <server_create+0x3a>
lea    0xe1f(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <server_create+0x168>
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0xc(%rbp),%edi
call   1070 <make_fd_nonblock@plt>
cmp    $0xffffffff,%eax
jne    125b <server_create+0xbb>
jmp    1233 <server_create+0x93>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
lea    0xdce(%rip),%rdx
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <server_create+0x168>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movzwl -0x6(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12cb <server_create+0x12b>
jmp    12b3 <server_create+0x113>
lea    0xd7c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <server_create+0x168>
mov    -0xc(%rbp),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1302 <server_create+0x162>
jmp    12ea <server_create+0x14a>
lea    0xd4a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <server_create+0x168>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_create>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e2d(%rip),%r15
mov    (%r15),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1292 <server_create+0xf2>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
call   1070 <make_fd_nonblock@plt>
cmp    $0xffffffff,%eax
je     1274 <server_create+0xd4>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     129b <server_create+0xfb>
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ac <server_create+0x10c>
lea    0xdc9(%rip),%rdi
jmp    12a2 <server_create+0x102>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
lea    0xd8d(%rip),%rdx
call   1080 <fprintf@plt>
jmp    12a7 <server_create+0x107>
lea    0xd67(%rip),%rdi
jmp    12a2 <server_create+0x102>
lea    0xd94(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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"socket(SOCK_STREAM)\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [30 x i8] c"failed to set servfd nonblock\00"
@INADDR_ANY = global i32 0
@.str.3 = [5 x i8] c"bind\00"
@SOMAXCONN = global i32 0
@.str.4 = [7 x i8] c"listen\00"
define i32 @server_create(i16 zeroext %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i16 %0, i16* %3
%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* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
br label %13
13: ; preds = %12
%14 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
15: ; No predecessors!
br label %16
16: ; preds = %15, %1
store i32 1, i32* %6
%17 = load i32, i32* %4
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %6, i32 4)
%21 = load i32, i32* %4
%22 = call i32 @make_fd_nonblock(i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %16
br label %25
25: ; preds = %24
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
28: ; No predecessors!
br label %29
29: ; preds = %28, %16
%30 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %31, i32* %32
%33 = load i16, i16* %3
%34 = call i32 @htons(i16 zeroext %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %4
%41 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %29
br label %45
45: ; preds = %44
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
47: ; No predecessors!
br label %48
48: ; preds = %47, %29
%49 = load i32, i32* %4
%50 = load i32, i32* @SOMAXCONN
%51 = call i32 @listen(i32 %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %48
br label %54
54: ; preds = %53
%55 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
56: ; No predecessors!
br label %57
57: ; preds = %56, %48
%58 = load i32, i32* %4
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %54, %45, %25, %13
%60 = load i32, i32* %2
ret i32 %60
}

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