#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*/ srv_addr;
typedef int /*<<< orphan*/ mode;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket4(int port) {
  int sockfd;
  struct sockaddr_in srv_addr;
  int mode;
  int opt_reuseadddr;
  mode = 1;
  srv_addr.sin_family = AF_INET;
  srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  srv_addr.sin_port = htons(port);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket4 error");
    return -1;
  }
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&mode,
                 sizeof(mode)) < 0) {
    perror("set socket4 failed");
    return -1;
  }
  if (bind(sockfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr)) == -1) {
    perror("bind socket4 error");
    return -1;
  }
  if (listen(sockfd, 10) == -1) {
    perror("listen socket4 error");
    return -1;
  }
  opt_reuseadddr = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt_reuseadddr,
                 sizeof(int)) == -1) {
    perror("Can not set SO_REUSEADDR option4");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<create_socket4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e43(%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    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11e8 <create_socket4+0x78>
lea    0xe41(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <create_socket4+0x173>
mov    -0xc(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
jge    122f <create_socket4+0xbf>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <create_socket4+0x173>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1261 <create_socket4+0xf1>
lea    0xdc3(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <create_socket4+0x173>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    128f <create_socket4+0x11f>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <create_socket4+0x173>
movl   $0x1,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12dd <create_socket4+0x16d>
lea    0xd6f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <create_socket4+0x173>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket4>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e56(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r14),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     124f <create_socket4+0xdf>
mov    %eax,%ebx
mov    0x2e15(%rip),%r14
mov    (%r14),%esi
mov    0x2de3(%rip),%r15
mov    (%r15),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
mov    %eax,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1258 <create_socket4+0xe8>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1261 <create_socket4+0xf1>
mov    %ebx,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     126a <create_socket4+0xfa>
movl   $0x1,0x8(%rsp)
mov    (%r14),%esi
mov    (%r15),%edx
lea    0x8(%rsp),%rcx
mov    $0x4,%r8d
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    127b <create_socket4+0x10b>
lea    0xdee(%rip),%rdi
jmp    1271 <create_socket4+0x101>
lea    0xdc2(%rip),%rdi
jmp    1271 <create_socket4+0x101>
lea    0xda1(%rip),%rdi
jmp    1271 <create_socket4+0x101>
lea    0xdab(%rip),%rdi
jmp    1271 <create_socket4+0x101>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"socket4 error\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [19 x i8] c"set socket4 failed\00"
@.str.2 = [19 x i8] c"bind socket4 error\00"
@.str.3 = [21 x i8] c"listen socket4 error\00"
@.str.4 = [33 x i8] c"Can not set SO_REUSEADDR option4\00"
define i32 @create_socket4(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %6
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = call i32 @htonl(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* %3
%15 = call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
24: ; preds = %1
%25 = load i32, i32* %4
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = bitcast i32* %6 to i8*
%29 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %25, i32 %26, i32 %27, i8* %28, i64 4)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = 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 %56
33: ; preds = %24
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i32 @bind(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
40: ; preds = %33
%41 = load i32, i32* %4
%42 = call i32 @listen(i32 %41, i32 10)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
46: ; preds = %40
store i32 1, i32* %7
%47 = load i32, i32* %4
%48 = load i32, i32* @SOL_SOCKET
%49 = load i32, i32* @SO_REUSEADDR
%50 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %47, i32 %48, i32 %49, i32* %7, i64 4)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %56
54: ; preds = %46
%55 = load i32, i32* %4
store i32 %55, i32* %2
br label %56
56: ; preds = %54, %52, %44, %38, %31, %22
%57 = load i32, i32* %2
ret i32 %57
}

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"socket4 error\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [19 x i8] c"set socket4 failed\00"
@.str.2 = [19 x i8] c"bind socket4 error\00"
@.str.3 = [21 x i8] c"listen socket4 error\00"
@.str.4 = [33 x i8] c"Can not set SO_REUSEADDR option4\00"
define i32 @create_socket4(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %3
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = tail call i32 @htonl(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %13, i32* %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 ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %46
21: ; preds = %1
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %17, i32 %22, i32 %23, i32* %3, i64 4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %46
28: ; preds = %21
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %17, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %46
34: ; preds = %28
%35 = call i32 @listen(i32 %17, i32 10)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %46
39: ; preds = %34
store i32 1, i32* %4
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %17, i32 %40, i32 %41, i32* %4, i64 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %39, %44, %37, %32, %26, %19
%47 = phi i32 [ -1, %19 ], [ -1, %26 ], [ -1, %32 ], [ -1, %37 ], [ -1, %44 ], [ %17, %39 ]
ret i32 %47
}