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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ x_set_sock_default_opt(int);
int x_create_tcp_server(const char *ip, int port) {
  struct sockaddr_in addr;
  int s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1) {
    return -1;
  }
  x_set_sock_default_opt(s);
  addr.sin_family = AF_INET;
  if (strcmp(ip, "0.0.0.0") == 0) {
    addr.sin_addr.s_addr = INADDR_ANY;
  } else {
    addr.sin_addr.s_addr = inet_addr(ip);
  }
  addr.sin_port = htons(port);
  if (bind(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1) {
    close(s);
    s = -1;
    return -1;
  }
  if (listen(s, 5) == -1) {
    close(s);
    s = -1;
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<x_create_tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c1 <x_create_tcp_server+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <x_create_tcp_server+0x103>
mov    -0x24(%rbp),%edi
call   1040 <x_set_sock_default_opt@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1200 <x_create_tcp_server+0x80>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    120c <x_create_tcp_server+0x8c>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    124c <x_create_tcp_server+0xcc>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <x_create_tcp_server+0x103>
mov    -0x24(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    127d <x_create_tcp_server+0xfd>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <x_create_tcp_server+0x103>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_create_tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e4f(%rip),%r12
mov    (%r12),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     1230 <x_create_tcp_server+0xb0>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <x_set_sock_default_opt@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
lea    0xe30(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11e7 <x_create_tcp_server+0x67>
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
jmp    11f0 <x_create_tcp_server+0x70>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1229 <x_create_tcp_server+0xa9>
mov    %ebp,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1229 <x_create_tcp_server+0xa9>
mov    %ebp,%r15d
jmp    1230 <x_create_tcp_server+0xb0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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 = [8 x i8] c"0.0.0.0\00"
@INADDR_ANY = global i32 0
define i32 @x_create_tcp_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %51
14: ; preds = %2
%15 = load i32, i32* %7
%16 = call i32 @x_set_sock_default_opt(i32 %15)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i8*, i8** %4
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
br label %31
26: ; preds = %14
%27 = load i8*, i8** %4
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
br label %31
31: ; preds = %26, %22
%32 = load i32, i32* %5
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %7
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %7
store i32 -1, i32* %3
br label %51
42: ; preds = %31
%43 = load i32, i32* %7
%44 = call i32 @listen(i32 %43, i32 5)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %7
store i32 -1, i32* %3
br label %51
49: ; preds = %42
%50 = load i32, i32* %7
store i32 %50, i32* %3
br label %51
51: ; preds = %49, %46, %39, %13
%52 = load i32, i32* %3
ret i32 %52
}

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 = [8 x i8] c"0.0.0.0\00"
@INADDR_ANY = global i32 0
define i32 @x_create_tcp_server(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 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 eq i32 %7, -1
br i1 %8, label %32, label %9
9: ; preds = %2
%10 = tail call i32 @x_set_sock_default_opt(i32 %7)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = load i32, i32* @INADDR_ANY
br label %19
17: ; preds = %9
%18 = tail call i32 @inet_addr(i8* %0)
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %16, %15 ], [ %18, %17 ]
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 %1)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %7, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %19
%28 = call i32 @listen(i32 %7, i32 5)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27, %19
%31 = call i32 @close(i32 %7)
br label %32
32: ; preds = %30, %27, %2
%33 = phi i32 [ -1, %2 ], [ %7, %27 ], [ -1, %30 ]
ret i32 %33
}