#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 */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ listen(int, int);
int sock_bind(int, char const *, int);
int sock_create(char *);
int /*<<< orphan*/ sock_setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                                   int);
int /*<<< orphan*/ stderr;
int sock_tcp_server(const char *hostname, int port) {
  int oldfl = 0;
  int sock;
  if ((sock = sock_create("tcp")) < 0)
    do {
      fprintf(stderr, "initialize server socket failed\n");
      exit(0);
    } while (0);
  oldfl = fcntl(sock, F_GETFL, 0);
  if (!(oldfl & O_NONBLOCK))
    fcntl(sock, F_SETFL, oldfl | O_NONBLOCK);
  sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
  if ((sock_bind(sock, hostname, port)) == -1)
    return -1;
  if (listen(sock, 1024) != 0)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<sock_tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe73(%rip),%rdi
call   1030 <sock_create@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11bf <sock_tcp_server+0x4f>
jmp    11a3 <sock_tcp_server+0x33>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e01(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1207 <sock_tcp_server+0x97>
mov    -0x1c(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
call   1050 <fcntl@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ecx
call   1090 <sock_setsockopt@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <sock_bind@plt>
cmp    $0xffffffff,%eax
jne    124a <sock_tcp_server+0xda>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <sock_tcp_server+0x103>
mov    -0x1c(%rbp),%edi
mov    $0x400,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     126d <sock_tcp_server+0xfd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <sock_tcp_server+0x103>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_tcp_server>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xe7f(%rip),%rdi
call   1030 <sock_create@plt>
test   %eax,%eax
js     1210 <sock_tcp_server+0xa0>
mov    %eax,%ebx
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edx
test   %eax,%edx
jne    11c1 <sock_tcp_server+0x51>
mov    0x2e12(%rip),%rcx
mov    (%rcx),%esi
or     %eax,%edx
mov    %ebx,%edi
call   1050 <fcntl@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x1,%ecx
call   1090 <sock_setsockopt@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <sock_bind@plt>
cmp    $0xffffffff,%eax
je     1206 <sock_tcp_server+0x96>
mov    %ebx,%edi
mov    $0x400,%esi
call   1070 <listen@plt>
neg    %rax
sbb    %eax,%eax
or     %ebx,%eax
jmp    120b <sock_tcp_server+0x9b>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"tcp\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"initialize server socket failed\0A\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @sock_tcp_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%8 = call i32 @sock_create(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %8, i32* %7
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %2
br label %11
11: ; preds = %10
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; No predecessors!
br label %16
16: ; preds = %15, %2
%17 = load i32, i32* %7
%18 = load i32, i32* @F_GETFL
%19 = call i32 @fcntl(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = load i32, i32* @O_NONBLOCK
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %16
%25 = load i32, i32* %7
%26 = load i32, i32* @F_SETFL
%27 = load i32, i32* %6
%28 = load i32, i32* @O_NONBLOCK
%29 = or i32 %27, %28
%30 = call i32 @fcntl(i32 %25, i32 %26, i32 %29)
br label %31
31: ; preds = %24, %16
%32 = load i32, i32* %7
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i32 @sock_setsockopt(i32 %32, i32 %33, i32 %34, i32 1)
%36 = load i32, i32* %7
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = call i32 @sock_bind(i32 %36, i8* %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %31
store i32 -1, i32* %3
br label %49
42: ; preds = %31
%43 = load i32, i32* %7
%44 = call i64 @listen(i32 %43, i32 1024)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %3
br label %49
47: ; preds = %42
%48 = load i32, i32* %7
store i32 %48, i32* %3
br label %49
49: ; preds = %47, %46, %41
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [4 x i8] c"tcp\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"initialize server socket failed\0A\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @sock_tcp_server(i8* %0, i32 %1) {
%3 = tail call i32 @sock_create(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = load i32, i32* @F_GETFL
%11 = tail call i32 @fcntl(i32 %3, i32 %10, i32 0)
%12 = load i32, i32* @O_NONBLOCK
%13 = and i32 %12, %11
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i32, i32* @F_SETFL
%17 = or i32 %12, %11
%18 = tail call i32 @fcntl(i32 %3, i32 %16, i32 %17)
br label %19
19: ; preds = %15, %9
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = tail call i32 @sock_setsockopt(i32 %3, i32 %20, i32 %21, i32 1)
%23 = tail call i32 @sock_bind(i32 %3, i8* %0, i32 %1)
%24 = icmp eq i32 %23, -1
br i1 %24, label %29, label %25
25: ; preds = %19
%26 = tail call i64 @listen(i32 %3, i32 1024)
%27 = icmp eq i64 %26, 0
%28 = select i1 %27, i32 %3, i32 -1
br label %29
29: ; preds = %25, %19
%30 = phi i32 [ -1, %19 ], [ %28, %25 ]
ret i32 %30
}