#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(unsigned short);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ set_nonblock(int);
int /*<<< orphan*/ set_reuse(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_setup(unsigned short port) {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("Failed to create socket");
    return -1;
  }
  set_nonblock(fd);
  set_reuse(fd);
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  int ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0) {
    perror("Failed to bind to socket");
    close(fd);
    return -1;
  }
  ret = listen(fd, 100);
  if (ret < 0) {
    perror("failed to listen to port");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<socket_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11dd <socket_setup+0x4d>
lea    0xe34(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <socket_setup+0x103>
mov    -0xc(%rbp),%edi
call   1060 <set_nonblock@plt>
mov    -0xc(%rbp),%edi
call   10a0 <set_reuse@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movzwl -0x6(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1253 <socket_setup+0xc3>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <socket_setup+0x103>
mov    -0xc(%rbp),%edi
mov    $0x64,%esi
call   1070 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    128d <socket_setup+0xfd>
lea    0xdbd(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <socket_setup+0x103>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_setup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e47(%rip),%r15
mov    (%r15),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     121d <socket_setup+0x8d>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <set_nonblock@plt>
mov    %ebx,%edi
call   10a0 <set_reuse@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
movzwl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %eax,%eax
js     122b <socket_setup+0x9b>
mov    %ebx,%edi
mov    $0x64,%esi
call   1070 <listen@plt>
test   %eax,%eax
js     1234 <socket_setup+0xa4>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
jmp    1247 <socket_setup+0xb7>
lea    0xde6(%rip),%rdi
jmp    123b <socket_setup+0xab>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    1210 <socket_setup+0x80>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@.str.1 = [25 x i8] c"Failed to bind to socket\00"
@.str.2 = [25 x i8] c"failed to listen to port\00"
define i32 @socket_setup(i16 zeroext %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %30
9: ; preds = %1
%10 = tail call i32 @set_nonblock(i32 %5)
%11 = tail call i32 @set_reuse(i32 %5)
%12 = bitcast %struct.sockaddr_in* %2 to i8*
%13 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @htons(i16 zeroext %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i32 @bind(i32 %5, %struct.sockaddr* %18, i32 8)
%20 = icmp slt i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %9
%22 = call i32 @listen(i32 %5, i32 100)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21, %9
%25 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), %21 ]
%26 = call i32 @perror(i8* %25)
%27 = call i32 @close(i32 %5)
br label %28
28: ; preds = %24, %21
%29 = phi i32 [ %5, %21 ], [ -1, %24 ]
br label %30
30: ; preds = %28, %7
%31 = phi i32 [ -1, %7 ], [ %29, %28 ]
ret i32 %31
}