#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ iOption;
typedef int /*<<< orphan*/ addrServer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int listen(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_listen(char *strIp, int iPort, int iListen) {
  int iReturn = 0;
  struct sockaddr_in addrServer;
  int iOption = 1;
  int isockServer = socket(AF_INET, SOCK_STREAM, 0);
  if (isockServer < 0) {
    return -1;
  }
  if (setsockopt(isockServer, SOL_SOCKET, SO_REUSEADDR, &iOption,
                 sizeof(iOption)) < 0) {
    return -1;
  }
  bzero(&addrServer, sizeof(addrServer));
  addrServer.sin_family = AF_INET;
  inet_aton(strIp, &(addrServer.sin_addr));
  addrServer.sin_port = htons(iPort);
  iReturn =
      bind(isockServer, (struct sockaddr *)&addrServer, sizeof(addrServer));
  if (0 != iReturn) {
    return -1;
  }
  iReturn = listen(isockServer, iListen);
  if (0 != iReturn) {
    return -1;
  }
  return isockServer;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11c2 <tcp_listen+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <tcp_listen+0x118>
mov    -0x30(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11fc <tcp_listen+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <tcp_listen+0x118>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
call   1050 <inet_aton@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
je     125d <tcp_listen+0xed>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <tcp_listen+0x118>
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <listen@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
je     1282 <tcp_listen+0x112>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <tcp_listen+0x118>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %esi,%r15d
mov    %rdi,%r12
movl   $0x1,0x4(%rsp)
mov    0x2e4a(%rip),%r14
mov    (%r14),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     123a <tcp_listen+0xca>
mov    %eax,%ebp
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     123a <tcp_listen+0xca>
mov    %r13d,(%rsp)
lea    0x8(%rsp),%r13
mov    %r13,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%rsi
mov    %r12,%rdi
call   1050 <inet_aton@plt>
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jne    123a <tcp_listen+0xca>
mov    %ebp,%edi
mov    (%rsp),%esi
call   1060 <listen@plt>
neg    %eax
sbb    %ebx,%ebx
or     %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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