#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*/ nVal;
typedef int /*<<< orphan*/ ServAddr;

/* 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*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setSocket(int port) {
  int sock;
  struct sockaddr_in ServAddr;
  int nVal;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket error");
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &nVal, sizeof(nVal)) < 0) {
    perror("setsocketopt error");
    return -1;
  }
  memset(&ServAddr, 0, sizeof(ServAddr));
  ServAddr.sin_family = AF_INET;
  ServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  ServAddr.sin_port = htons(port);
  if (bind(sock, (struct sockaddr *)&ServAddr, sizeof(ServAddr)) < 0) {
    perror("bind error");
    return -1;
  }
  if (listen(sock, 5) < 0) {
    perror("listen error");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<setSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c8 <setSocket+0x48>
lea    0xe49(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <setSocket+0x12e>
mov    -0xc(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    120e <setSocket+0x8e>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <setSocket+0x12e>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1279 <setSocket+0xf9>
lea    0xdb8(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <setSocket+0x12e>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12a8 <setSocket+0x128>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <setSocket+0x12e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e4c(%rip),%r15
mov    (%r15),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1241 <setSocket+0xc1>
mov    %eax,%ebx
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     124a <setSocket+0xca>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1253 <setSocket+0xd3>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    1264 <setSocket+0xe4>
lea    0xdec(%rip),%rdi
jmp    125a <setSocket+0xda>
lea    0xdb8(%rip),%rdi
jmp    125a <setSocket+0xda>
lea    0xdbc(%rip),%rdi
jmp    125a <setSocket+0xda>
lea    0xdc6(%rip),%rdi
call   1090 <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 { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [19 x i8] c"setsocketopt error\00"
@INADDR_ANY = global i32 0
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
define i32 @setSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 %0, i32* %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 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %46
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i64 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %6, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %46
21: ; preds = %13
%22 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %3
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %4
%33 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %21
%37 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %46
38: ; preds = %21
%39 = load i32, i32* %4
%40 = call i64 @listen(i32 %39, i32 5)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %46
44: ; preds = %38
%45 = load i32, i32* %4
store i32 %45, i32* %2
br label %46
46: ; preds = %44, %42, %36, %19, %11
%47 = load i32, i32* %2
ret i32 %47
}

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