#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 */
typedef int /*<<< orphan*/ yes;
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*/ listen_addr;

/* 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*/ err(int, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_server_socket(void) {
  int fd, yes = 1;
  struct sockaddr_in listen_addr;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    err(1, "socket() failed");
  }
  memset(&listen_addr, 0, sizeof(listen_addr));
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_addr.s_addr = INADDR_ANY;
  listen_addr.sin_port = htons(10000);
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
    err(1, "setsockopt failed");
  }
  if (bind(fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) {
    err(1, "bind failed");
  }
  if (listen(fd, 10) < 0) {
    err(1, "listen failed");
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0x8(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11b6 <create_server_socket+0x46>
mov    $0x1,%edi
lea    0xe4f(%rip),%rsi
call   1060 <err@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x2710,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    122a <create_server_socket+0xba>
mov    $0x1,%edi
lea    0xdeb(%rip),%rsi
call   1060 <err@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1256 <create_server_socket+0xe6>
mov    $0x1,%edi
lea    0xdd1(%rip),%rsi
call   1060 <err@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    127e <create_server_socket+0x10e>
mov    $0x1,%edi
lea    0xdb5(%rip),%rsi
call   1060 <err@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x1,0x4(%rsp)
mov    0x2e5a(%rip),%r14
mov    (%r14),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11b0 <create_server_socket+0x40>
lea    0xe5a(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x2710,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jns    121d <create_server_socket+0xad>
lea    0xdfd(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1244 <create_server_socket+0xd4>
lea    0xde8(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    1266 <create_server_socket+0xf6>
lea    0xdd2(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
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 = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [18 x i8] c"setsockopt failed\00"
@.str.2 = [12 x i8] c"bind failed\00"
@.str.3 = [14 x i8] c"listen failed\00"
define i32 @create_server_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
store i32 1, i32* %2
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 10000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %1
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i64 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %2, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %11
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %11
%28 = load i32, i32* %1
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = load i32, i32* %1
%36 = call i64 @listen(i32 %35, i32 10)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* %1
ret i32 %41
}

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