#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*/ sa_handler;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct sigaction {
  int /*<<< orphan*/ sa_handler;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ sigact;
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sockaddr_in *,
                             int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server_create(int port) {
  int server_socket;
  struct sockaddr_in server_addr;
  struct sigaction sigact;
  int reuse = 1;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_socket = socket(PF_INET, SOCK_STREAM, 0);
  if (!server_socket) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &reuse,
                 sizeof(reuse)) < 0) {
    perror("setsockopt");
    exit(EXIT_FAILURE);
  }
  if (bind(server_socket, (const struct sockaddr *)&server_addr,
           sizeof(server_addr)) < 0) {
    perror("bind");
    exit(EXIT_FAILURE);
  }
  if (listen(server_socket, SOMAXCONN) < 0) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  memset(&sigact, 0, sizeof(sigact));
  sigact.sa_handler = SIG_IGN;
  sigaction(SIGPIPE, &sigact, NULL);
  printf("Server socket bound and listening\n");
  return server_socket;
}


x86 assembly (clang, -O0):
<server_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1225 <server_create+0x85>
lea    0xdee(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    126d <server_create+0xcd>
lea    0xdad(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12a2 <server_create+0x102>
lea    0xd83(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12d7 <server_create+0x137>
lea    0xd53(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
lea    0xd0e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_create>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
movl   $0x1,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
je     12a8 <server_create+0x108>
mov    %eax,%ebx
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
test   %rax,%rax
js     12b1 <server_create+0x111>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12ba <server_create+0x11a>
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <listen@plt>
test   %rax,%rax
js     12c3 <server_create+0x123>
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
lea    0xd85(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd51(%rip),%rdi
jmp    12ca <server_create+0x12a>
lea    0xd4f(%rip),%rdi
jmp    12ca <server_create+0x12a>
lea    0xd51(%rip),%rdi
jmp    12ca <server_create+0x12a>
lea    0xd4d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sigaction = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@SOMAXCONN = global i32 0
@.str.3 = [7 x i8] c"listen\00"
@SIG_IGN = global i32 0
@SIGPIPE = global i32 0
@.str.4 = [35 x i8] c"Server socket bound and listening\0A\00"
define i32 @server_create(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sigaction
%6 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %6
%7 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 16)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* %2
%11 = call i32 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %1
%26 = load i32, i32* %3
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i64 @setsockopt(i32 %26, i32 %27, i32 %28, i32* %6, i32 4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %25
%36 = load i32, i32* %3
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i64 @bind(i32 %36, %struct.sockaddr* %37, i32 16)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %35
%45 = load i32, i32* %3
%46 = load i32, i32* @SOMAXCONN
%47 = call i64 @listen(i32 %45, i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %44
%54 = bitcast %struct.sigaction* %5 to %struct.sockaddr_in*
%55 = call i32 @memset(%struct.sockaddr_in* %54, i32 0, i32 16)
%56 = load i32, i32* @SIG_IGN
%57 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i32 0, i32 0
store i32 %56, i32* %57
%58 = load i32, i32* @SIGPIPE
%59 = bitcast %struct.sigaction* %5 to %struct.sockaddr_in*
%60 = call i32 @sigaction(i32 %58, %struct.sockaddr_in* %59, i32* null)
%61 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sigaction = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@SOMAXCONN = global i32 0
@.str.3 = [7 x i8] c"listen\00"
@SIG_IGN = global i32 0
@SIGPIPE = global i32 0
@str = [34 x i8] c"Server socket bound and listening\00"
define i32 @server_create(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sigaction
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sigaction* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%8 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 16)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %9, i32* %10
%11 = call i32 @htons(i32 %0)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %1
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i64 @setsockopt(i32 %17, i32 %24, i32 %25, i32* %4, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
%33 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%34 = call i64 @bind(i32 %17, %struct.sockaddr* %33, i32 16)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = load i32, i32* @SOMAXCONN
%42 = call i64 @listen(i32 %17, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %40
%49 = bitcast %struct.sigaction* %3 to %struct.sockaddr_in*
%50 = call i32 @memset(%struct.sockaddr_in* %49, i32 0, i32 16)
%51 = load i32, i32* @SIG_IGN
%52 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i32 %51, i32* %52
%53 = load i32, i32* @SIGPIPE
%54 = call i32 @sigaction(i32 %53, %struct.sockaddr_in* %49, i32* null)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
ret i32 %17
}