#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;
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef scalar_t__ int64_t;
typedef int /*<<< orphan*/ int32_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int32_t tcp_accept(int64_t *out, int64_t srvrfd, uint32_t *remoteip,
                   uint16_t *remoteport) {
  int ret;
  struct sockaddr_in cli_addr;
  socklen_t calen = sizeof(cli_addr);
  int cfd = accept((int)srvrfd, (struct sockaddr *)&cli_addr, &calen);
  if (cfd < 0) {
    ret = -errno;
    fprintf(stderr, "%s +%d: srvrfd: %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",
            455, strerror(errno));
    return (int32_t)ret;
  }
  *remoteip = ntohl(cli_addr.sin_addr.s_addr);
  *remoteport = cli_addr.sin_port;
  *out = (int64_t)cfd;
  return 0;
}


x86 assembly (clang, -O0):
<tcp_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x8,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    11d0 <tcp_accept+0x90>
xor    %eax,%eax
mov    0x2e50(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%r8
lea    0xe4c(%rip),%rsi
lea    0xe59(%rip),%rdx
mov    $0x1c7,%ecx
call   1030 <fprintf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <tcp_accept+0xbb>
mov    -0x34(%rbp),%edi
call   1050 <ntohl@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movslq -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_accept>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%rax
mov    %rdi,%r15
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %eax,%edi
call   1040 <accept@plt>
test   %eax,%eax
js     1190 <tcp_accept+0x50>
mov    %eax,%ebp
mov    0x14(%rsp),%edi
call   1050 <ntohl@plt>
mov    %eax,(%rbx)
mov    0x10(%rsp),%eax
mov    %eax,(%r14)
mov    %ebp,%eax
mov    %rax,(%r15)
xor    %ebx,%ebx
jmp    11c8 <tcp_accept+0x88>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebx
neg    %ebx
mov    0x2e3c(%rip),%rax
mov    (%rax),%ebp
call   1060 <strerror@plt>
lea    0xe4e(%rip),%rsi
lea    0xe5b(%rip),%rdx
mov    %ebp,%edi
mov    $0x1c7,%ecx
mov    %rax,%r8
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"%s +%d: srvrfd: %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"
define i32 @tcp_accept(i64* %0, i64 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca i32
store i64* %0, i64** %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 8, i32* %12
%14 = load i64, i64* %7
%15 = trunc i64 %14 to i32
%16 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%17 = call i32 @accept(i32 %15, %struct.sockaddr* %16, i32* %12)
store i32 %17, i32* %13
%18 = load i32, i32* %13
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %4
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %10
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, 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 455, i8* %25)
%27 = load i32, i32* %10
store i32 %27, i32* %5
br label %40
28: ; preds = %4
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @ntohl(i32 %31)
%33 = load i32*, i32** %8
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32*, i32** %9
store i32 %35, i32* %36
%37 = load i32, i32* %13
%38 = sext i32 %37 to i64
%39 = load i64*, i64** %6
store i64 %38, i64* %39
store i32 0, i32* %5
br label %40
40: ; preds = %28, %20
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"%s +%d: srvrfd: %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"
define i32 @tcp_accept(i64* nocapture writeonly %0, i64 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 8, i32* %6
%9 = trunc i64 %1 to i32
%10 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %6)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %4
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
%16 = load i32, i32* @stderr
%17 = call i8* @strerror(i32 %14)
%18 = call i32 @fprintf(i32 %16, 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 455, i8* %17)
br label %26
19: ; preds = %4
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%21 = load i32, i32* %20
%22 = call i32 @ntohl(i32 %21)
store i32 %22, i32* %2
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %3
%25 = zext i32 %11 to i64
store i64 %25, i64* %0
br label %26
26: ; preds = %19, %13
%27 = phi i32 [ %15, %13 ], [ 0, %19 ]
ret i32 %27
}