#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;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int tcp_listen(char *ip, char *port) {
  struct sockaddr_in server;
  int r, s;
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1) {
    perror("socket");
    return -1;
  }
  if (!port) {
    fprintf(stderr, "port is null\n");
    return -1;
  }
  memset(&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons(atoi(port));
  server.sin_addr.s_addr = ip ? inet_addr(ip) : htonl(INADDR_ANY);
  int on = 1;
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
    perror("setsockopt");
    return -1;
  }
  r = bind(s, (struct sockaddr *)&server, sizeof(server));
  if (r == -1) {
    perror("bind");
    return -1;
  }
  r = listen(s, 10);
  if (r == -1) {
    perror("listen");
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11fe <tcp_listen+0x4e>
lea    0xe13(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1347 <tcp_listen+0x197>
cmpq   $0x0,-0x18(%rbp)
jne    122a <tcp_listen+0x7a>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1347 <tcp_listen+0x197>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1275 <tcp_listen+0xc5>
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x38(%rbp)
jmp    1286 <tcp_listen+0xd6>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    0x2d12(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12d9 <tcp_listen+0x129>
lea    0xd4d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1347 <tcp_listen+0x197>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    130f <tcp_listen+0x15f>
lea    0xd22(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1347 <tcp_listen+0x197>
mov    -0x30(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1341 <tcp_listen+0x191>
lea    0xcf5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1347 <tcp_listen+0x197>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e19(%rip),%r15
mov    (%r15),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12b1 <tcp_listen+0x101>
test   %rbx,%rbx
je     1224 <tcp_listen+0x74>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
test   %r14,%r14
je     123e <tcp_listen+0x8e>
mov    %r14,%rdi
call   1070 <inet_addr@plt>
jmp    124c <tcp_listen+0x9c>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12cf <tcp_listen+0x11f>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
movl   $0x1,0x4(%rsp)
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    12ba <tcp_listen+0x10a>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12c3 <tcp_listen+0x113>
mov    %ebp,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12d4 <tcp_listen+0x124>
lea    0xd76(%rip),%rdi
jmp    12ca <tcp_listen+0x11a>
lea    0xd48(%rip),%rdi
jmp    12ca <tcp_listen+0x11a>
lea    0xd54(%rip),%rdi
jmp    12ca <tcp_listen+0x11a>
lea    0xd56(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
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
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"port is null\0A\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @tcp_listen(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
23: ; preds = %17
%24 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i8*, i8** %5
%28 = call i32 @atoi(i8* %27)
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i8*, i8** %4
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %23
%34 = load i8*, i8** %4
%35 = call i32 @inet_addr(i8* %34)
br label %39
36: ; preds = %23
%37 = load i32, i32* @INADDR_ANY
%38 = call i32 @htonl(i32 %37)
br label %39
39: ; preds = %36, %33
%40 = phi i32 [ %35, %33 ], [ %38, %36 ]
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
store i32 %40, i32* %42
store i32 1, i32* %9
%43 = load i32, i32* %8
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = bitcast i32* %9 to i8*
%47 = call i64 @setsockopt(i32 %43, i32 %44, i32 %45, i8* %46, i32 4)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %39
%50 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
51: ; preds = %39
%52 = load i32, i32* %8
%53 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%54 = call i32 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
59: ; preds = %51
%60 = load i32, i32* %8
%61 = call i32 @listen(i32 %60, i32 10)
store i32 %61, i32* %7
%62 = load i32, i32* %7
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
66: ; preds = %59
%67 = load i32, i32* %8
store i32 %67, i32* %3
br label %68
68: ; preds = %66, %64, %57, %49, %20, %15
%69 = load i32, i32* %3
ret i32 %69
}

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
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"port is null\0A\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @tcp_listen(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%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 eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %50
12: ; preds = %2
%13 = icmp eq i8* %1, null
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %50
17: ; preds = %12
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @atoi(i8* %1)
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = icmp eq i8* %0, null
br i1 %24, label %27, label %25
25: ; preds = %17
%26 = call i32 @inet_addr(i8* %0)
br label %30
27: ; preds = %17
%28 = load i32, i32* @INADDR_ANY
%29 = call i32 @htonl(i32 %28)
br label %30
30: ; preds = %27, %25
%31 = phi i32 [ %26, %25 ], [ %29, %27 ]
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %31, i32* %32
%33 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i64 @setsockopt(i32 %8, i32 %34, i32 %35, i8* %33, i32 4)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %30
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @bind(i32 %8, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = call i32 @listen(i32 %8, i32 10)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42, %38, %30
%46 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %30 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %38 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %42 ]
%47 = call i32 @perror(i8* %46)
br label %48
48: ; preds = %45, %42
%49 = phi i32 [ %8, %42 ], [ -1, %45 ]
br label %50
50: ; preds = %48, %14, %10
%51 = phi i32 [ -1, %10 ], [ %49, %48 ], [ -1, %14 ]
ret i32 %51
}