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

/* 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*/ close(int);
int errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int stk_server_socket() {
  int server_fd;
  struct sockaddr_in servaddr;
  int flag = 1;
  if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
    return -1;
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(9007);
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&flag,
                 sizeof(flag)) == -1) {
    close(server_fd);
    return -1;
  }
  if (bind(server_fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
    printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
    close(server_fd);
    return -1;
  }
  if (listen(server_fd, 30) == -1) {
    printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
    close(server_fd);
    return -1;
  }
  return server_fd;
}


x86 assembly (clang, -O0):
<stk_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0x1c(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1206 <stk_server_socket+0x66>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
lea    0xe0b(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <stk_server_socket+0x18b>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    $0x232f,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1281 <stk_server_socket+0xe1>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <stk_server_socket+0x18b>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12d5 <stk_server_socket+0x135>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
lea    0xd68(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <stk_server_socket+0x18b>
mov    -0x8(%rbp),%edi
mov    $0x1e,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1325 <stk_server_socket+0x185>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cbd(%rip),%rax
mov    (%rax),%edx
lea    0xd3a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <stk_server_socket+0x18b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stk_server_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x1,0x4(%rsp)
mov    0x2e2a(%rip),%r14
mov    (%r14),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     127b <stk_server_socket+0xdb>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x232f,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12bd <stk_server_socket+0x11d>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     129c <stk_server_socket+0xfc>
mov    %ebx,%edi
mov    $0x1e,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c9 <stk_server_socket+0x129>
mov    0x2d59(%rip),%r14
mov    (%r14),%edi
call   10b0 <strerror@plt>
mov    (%r14),%edx
lea    0xdcd(%rip),%rdi
jmp    12b5 <stk_server_socket+0x115>
mov    0x2d3e(%rip),%rbx
mov    (%rbx),%edi
call   10b0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd6e(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
jmp    12c4 <stk_server_socket+0x124>
mov    0x2d1d(%rip),%r14
mov    (%r14),%edi
call   10b0 <strerror@plt>
mov    (%r14),%edx
lea    0xd6f(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
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 = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [34 x i8] c"bind socket error: %s(errno: %d)\0A\00"
@.str.2 = [36 x i8] c"listen socket error: %s(errno: %d)\0A\00"
define i32 @stk_server_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i32 1, i32* %4
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = load i32, i32* @errno
%13 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
store i32 -1, i32* %1
br label %58
14: ; preds = %0
%15 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 9007)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %2
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = bitcast i32* %4 to i8*
%28 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i8* %27, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %14
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %1
br label %58
33: ; preds = %14
%34 = load i32, i32* %2
%35 = bitcast %struct.sockaddr_in* %3 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 %45
38: ; preds = %33
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = load i32, i32* @errno
%42 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %40, i32 %41)
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %1
br label %58
45: ; preds = %33
%46 = load i32, i32* %2
%47 = call i32 @listen(i32 %46, i32 30)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = load i32, i32* @errno
%53 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %51, i32 %52)
%54 = load i32, i32* %2
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %1
br label %58
56: ; preds = %45
%57 = load i32, i32* %2
store i32 %57, i32* %1
br label %58
58: ; preds = %56, %49, %38, %30, %9
%59 = load i32, i32* %1
ret i32 %59
}

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 = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [34 x i8] c"bind socket error: %s(errno: %d)\0A\00"
@.str.2 = [36 x i8] c"listen socket error: %s(errno: %d)\0A\00"
define i32 @stk_server_socket() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%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 %14
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = load i32, i32* @errno
%13 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
br label %48
14: ; preds = %0
%15 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 9007)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 @setsockopt(i32 %7, i32 %23, i32 %24, i8* %4, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @close(i32 %7)
br label %48
29: ; preds = %14
%30 = bitcast %struct.sockaddr_in* %1 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 %39
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = load i32, i32* @errno
%37 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %35, i32 %36)
%38 = call i32 @close(i32 %7)
br label %48
39: ; preds = %29
%40 = call i32 @listen(i32 %7, i32 30)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = load i32, i32* @errno
%46 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %44, i32 %45)
%47 = call i32 @close(i32 %7)
br label %48
48: ; preds = %39, %42, %33, %27, %9
%49 = phi i32 [ -1, %9 ], [ -1, %27 ], [ -1, %33 ], [ -1, %42 ], [ %7, %39 ]
ret i32 %49
}