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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int listen(int, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(int port) {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  int opt = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
    return -1;
  struct sockaddr_in server_addr;
  server_addr.sin_family = PF_INET;
  server_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
  server_addr.sin_port = htons(port);
  bzero(&server_addr.sin_zero, sizeof(server_addr.sin_zero));
  if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
    return -1;
  if (listen(sock, 200) == -1)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ad <create_socket+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <create_socket+0xff>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11ed <create_socket+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <create_socket+0xff>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xe00(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1080 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1247 <create_socket+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <create_socket+0xff>
mov    -0xc(%rbp),%edi
mov    $0xc8,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1269 <create_socket+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <create_socket+0xff>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     122f <create_socket+0xbf>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     122f <create_socket+0xbf>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe1f(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x4,%esi
call   1080 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     122f <create_socket+0xbf>
mov    %ebx,%edi
mov    $0xc8,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    1234 <create_socket+0xc4>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__, 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
@PF_INET = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
define i32 @create_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
%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 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %43
13: ; preds = %1
store i32 1, i32* %5
%14 = load i32, i32* %4
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %5, i32 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %2
br label %43
20: ; preds = %13
%21 = load i32, i32* @PF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 3
store i32 %21, i32* %22
%23 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i32, i32* %3
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%30 = call i32 @bzero(i32* %29, i32 4)
%31 = load i32, i32* %4
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 16)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %20
store i32 -1, i32* %2
br label %43
36: ; preds = %20
%37 = load i32, i32* %4
%38 = call i32 @listen(i32 %37, i32 200)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 -1, i32* %2
br label %43
41: ; preds = %36
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %40, %35, %19, %12
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__, 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
@PF_INET = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
define i32 @create_socket(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %35, label %8
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_REUSEADDR
%12 = call i32 @setsockopt(i32 %6, i32 %10, i32 %11, i32* %2, i32 4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %33, label %14
14: ; preds = %8
%15 = bitcast %struct.sockaddr_in* %3 to i8*
%16 = load i32, i32* @PF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %16, i32* %17
%18 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%23 = call i32 @bzero(i32* %22, i32 4)
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %6, %struct.sockaddr* %24, i32 16)
%26 = icmp eq i32 %25, -1
br i1 %26, label %31, label %27
27: ; preds = %14
%28 = call i32 @listen(i32 %6, i32 200)
%29 = icmp eq i32 %28, -1
%30 = select i1 %29, i32 -1, i32 %6
br label %31
31: ; preds = %27, %14
%32 = phi i32 [ -1, %14 ], [ %30, %27 ]
br label %33
33: ; preds = %8, %31
%34 = phi i32 [ %32, %31 ], [ -1, %8 ]
br label %35
35: ; preds = %1, %33
%36 = phi i32 [ %34, %33 ], [ -1, %1 ]
ret i32 %36
}