#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 */
typedef int /*<<< orphan*/ uint32_t;
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;
};
typedef int /*<<< orphan*/ srvr_addr;
typedef int /*<<< orphan*/ reuseon;
typedef scalar_t__ int64_t;
typedef int /*<<< orphan*/ int32_t;
typedef int /*<<< orphan*/ int16_t;

/* 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*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int32_t tcp_server_start(int64_t *out, uint32_t ip, int16_t port) {
  int ret;
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  int reuseon = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon));
  struct sockaddr_in srvr_addr;
  memset(&srvr_addr, 0, sizeof(srvr_addr));
  srvr_addr.sin_family = AF_INET;
  srvr_addr.sin_addr.s_addr = htonl(ip);
  srvr_addr.sin_port = htons(port);
  ret = bind(sockfd, (struct sockaddr *)&srvr_addr, sizeof(srvr_addr));
  if (0 == ret) {
    ret = listen(sockfd, 10);
    if (0 == ret) {
      *out = (int64_t)sockfd;
      return 0;
    } else {
      ret = -errno;
      fprintf(stderr, "%s +%d: listen: %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/iamyuiwong/swift-socket/refs/heads/"
              "master/socket/tcp.c",
              415, strerror(errno));
      close(sockfd);
      sockfd = -1;
      return (int32_t)ret;
    }
  } else {
    ret = -errno;
    fprintf(stderr, "%s +%d: bind: %s\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/iamyuiwong/swift-socket/refs/heads/"
            "master/socket/tcp.c",
            424, strerror(errno));
    close(sockfd);
    sockfd = -1;
    return (int32_t)ret;
  }
}


x86 assembly (clang, -O0):
<tcp_server_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    12dc <tcp_server_start+0x13c>
mov    -0x20(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    127c <tcp_server_start+0xdc>
movslq -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1337 <tcp_server_start+0x197>
xor    %eax,%eax
mov    0x2d3b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%r8
lea    0xd4f(%rip),%rsi
lea    0xd5c(%rip),%rdx
mov    $0x19f,%ecx
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1337 <tcp_server_start+0x197>
xor    %eax,%eax
mov    0x2cdb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2cdf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%r8
lea    0xd94(%rip),%rsi
lea    0xcfc(%rip),%rdx
mov    $0x1a8,%ecx
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server_start>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
mov    0x2e22(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r13d
movl   $0x1,0x4(%rsp)
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %r12d,%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %eax,%eax
je     1278 <tcp_server_start+0xd8>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebp
neg    %ebp
mov    0x2d76(%rip),%rax
mov    (%rax),%ebx
call   10b0 <strerror@plt>
lea    0xe3d(%rip),%rsi
lea    0xda5(%rip),%rdx
mov    %ebx,%edi
mov    $0x1a8,%ecx
jmp    12bb <tcp_server_start+0x11b>
mov    %r13d,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
test   %eax,%eax
je     12dc <tcp_server_start+0x13c>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebp
neg    %ebp
mov    0x2d33(%rip),%rax
mov    (%rax),%r14d
call   10b0 <strerror@plt>
lea    0xd54(%rip),%rsi
lea    0xd61(%rip),%rdx
mov    %r14d,%edi
mov    $0x19f,%ecx
mov    %rax,%r8
call   1080 <fprintf@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %r13d,%rax
mov    %rax,(%r14)
xor    %ebp,%ebp
jmp    12cb <tcp_server_start+0x12b>

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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"%s +%d: listen: %s\0A\00"
@.str.1 = [145 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/iamyuiwong/swift-socket/refs/heads/master/socket/tcp.c\00"
@.str.2 = [18 x i8] c"%s +%d: bind: %s\0A\00"
define i32 @tcp_server_start(i64* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i64* %0, i64** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%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* %9
store i32 1, i32* %10
%15 = load i32, i32* %9
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %10, i32 4)
%19 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %6
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i32, i32* %7
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %9
%30 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp eq i32 0, %32
br i1 %33, label %34, label %53
34: ; preds = %3
%35 = load i32, i32* %9
%36 = call i32 @listen(i32 %35, i32 10)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp eq i32 0, %37
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = load i64*, i64** %5
store i64 %41, i64* %42
store i32 0, i32* %4
br label %63
43: ; preds = %34
%44 = load i32, i32* @errno
%45 = sub nsw i32 0, %44
store i32 %45, i32* %8
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.1, i64 0, i64 0), i32 415, i8* %48)
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %9
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %63
53: ; preds = %3
%54 = load i32, i32* @errno
%55 = sub nsw i32 0, %54
store i32 %55, i32* %8
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.1, i64 0, i64 0), i32 424, i8* %58)
%60 = load i32, i32* %9
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %9
%62 = load i32, i32* %8
store i32 %62, i32* %4
br label %63
63: ; preds = %53, %43, %39
%64 = load i32, i32* %4
ret i32 %64
}

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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"%s +%d: listen: %s\0A\00"
@.str.1 = [145 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/iamyuiwong/swift-socket/refs/heads/master/socket/tcp.c\00"
@.str.2 = [18 x i8] c"%s +%d: bind: %s\0A\00"
define i32 @tcp_server_start(i64* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_REUSEADDR
%12 = call i32 @setsockopt(i32 %8, i32 %10, i32 %11, i32* %4, i32 4)
%13 = bitcast %struct.sockaddr_in* %5 to i8*
%14 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htonl(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = call i32 @htons(i32 %2)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%22 = call i32 @bind(i32 %8, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %3
%25 = call i32 @listen(i32 %8, i32 10)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = sext i32 %8 to i64
store i64 %28, i64* %0
br label %43
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
%32 = load i32, i32* @stderr
%33 = call i8* @strerror(i32 %30)
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.1, i64 0, i64 0), i32 415, i8* %33)
%35 = call i32 @close(i32 %8)
br label %43
36: ; preds = %3
%37 = load i32, i32* @errno
%38 = sub nsw i32 0, %37
%39 = load i32, i32* @stderr
%40 = call i8* @strerror(i32 %37)
%41 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.1, i64 0, i64 0), i32 424, i8* %40)
%42 = call i32 @close(i32 %8)
br label %43
43: ; preds = %36, %29, %27
%44 = phi i32 [ 0, %27 ], [ %31, %29 ], [ %38, %36 ]
ret i32 %44
}