#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 sockaddr_storage {
  int dummy;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serverAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/, char, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned int ffi_accept(void) {
  int welcomeSocket;
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;
  int ret;
  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(7891);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
  if (!bind(welcomeSocket, (struct sockaddr *)&serverAddr,
            sizeof(serverAddr))) {
    exit(-1);
  }
  if (!listen(welcomeSocket, 1)) {
    exit(-1);
  }
  addr_size = sizeof serverStorage;
  ret = accept(welcomeSocket, (struct sockaddr *)&serverStorage, &addr_size);
  if (ret < 0) {
    exit(-1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<ffi_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x1ed3,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
lea    0xe3c(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0x0,%eax
jne    11ff <ffi_accept+0x7f>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0x0,%eax
jne    121f <ffi_accept+0x9f>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movl   $0x4,-0x24(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    124d <ffi_accept+0xcd>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffi_accept>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x1ed3,%edi
call   1030 <htons@plt>
mov    %eax,0x18(%rsp)
lea    0xe3e(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x14(%rsp)
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1070 <bind@plt>
test   %eax,%eax
je     121f <ffi_accept+0x9f>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
test   %eax,%eax
je     121f <ffi_accept+0x9f>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1080 <accept@plt>
test   %eax,%eax
js     121f <ffi_accept+0x9f>
add    $0x20,%rsp
pop    %rbx
ret
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @ffi_accept() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %1
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 3
store i32 %9, i32* %10
%11 = call i32 @htons(i32 7891)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @memset(i32 %17, i8 signext 0, i32 4)
%19 = load i32, i32* %1
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %19, %struct.sockaddr* %20, i32 16)
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %0
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %0
%26 = load i32, i32* %1
%27 = call i32 @listen(i32 %26, i32 1)
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %25
store i32 4, i32* %4
%32 = load i32, i32* %1
%33 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%34 = call i32 @accept(i32 %32, %struct.sockaddr* %33, i32* %4)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %31
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @ffi_accept() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast %struct.sockaddr_storage* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 7891)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = tail call i32 @memset(i32 undef, i8 signext 0, i32 4)
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i32 @bind(i32 %9, %struct.sockaddr* %17, i32 16)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %0
%23 = call i32 @listen(i32 %9, i32 1)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %22
store i32 4, i32* %3
%28 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%29 = call i32 @accept(i32 %9, %struct.sockaddr* %28, i32* %3)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %27
ret i32 %29
}