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

/* 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*/ 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(int port) {
  int server_sock, optval = 1;
  struct sockaddr_in server_addr;
  if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return 0;
  if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval,
                 sizeof(optval)) < 0)
    return 0;
  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;
  if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) !=
      0)
    return 0;
  if (listen(server_sock, 20) < 0)
    return 0;
  return server_sock;
}


x86 assembly (clang, -O0):
<create_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11a3 <create_server_socket+0x43>
movl   $0x0,-0x4(%rbp)
jmp    1260 <create_server_socket+0x100>
mov    -0xc(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11dd <create_server_socket+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    1260 <create_server_socket+0x100>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1237 <create_server_socket+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    1260 <create_server_socket+0x100>
mov    -0xc(%rbp),%edi
mov    $0x14,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    125a <create_server_socket+0xfa>
movl   $0x0,-0x4(%rbp)
jmp    1260 <create_server_socket+0x100>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
movl   $0x1,0xc(%rsp)
mov    0x2e62(%rip),%r12
mov    (%r12),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1228 <create_server_socket+0xc8>
mov    %eax,%ebx
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1228 <create_server_socket+0xc8>
lea    0x10(%rsp),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    1228 <create_server_socket+0xc8>
mov    %ebx,%edi
mov    $0x14,%esi
call   1060 <listen@plt>
xor    %ecx,%ecx
test   %rax,%rax
cmovs  %ecx,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
define i32 @create_server_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 1, i32* %5
%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 %12
11: ; preds = %1
store i32 0, i32* %2
br label %41
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i64 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %5, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 0, i32* %2
br label %41
19: ; preds = %12
%20 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* %3
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %4
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %19
store i32 0, i32* %2
br label %41
34: ; preds = %19
%35 = load i32, i32* %4
%36 = call i64 @listen(i32 %35, i32 20)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 0, i32* %2
br label %41
39: ; preds = %34
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %38, %33, %18, %11
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
define i32 @create_server_socket(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%5 = bitcast %struct.sockaddr_in* %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 %30, label %10
10: ; preds = %1
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = call i64 @setsockopt(i32 %8, i32 %11, i32 %12, i32* %2, i32 4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %10
%16 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i64 @bind(i32 %8, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = call i64 @listen(i32 %8, i32 20)
%28 = icmp slt i64 %27, 0
%29 = select i1 %28, i32 0, i32 %8
br label %30
30: ; preds = %26, %15, %10, %1
%31 = phi i32 [ 0, %1 ], [ 0, %10 ], [ 0, %15 ], [ %29, %26 ]
ret i32 %31
}