#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 fd;
};
struct sockaddr_in {
  TYPE_1__ data;
  int /*<<< orphan*/ events;
};
struct sockaddr {
  int dummy;
};
struct epoll_event {
  TYPE_1__ data;
  int /*<<< orphan*/ events;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ ee;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
scalar_t__ epoll_ctl(int, int /*<<< orphan*/, int, struct sockaddr_in *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int accept_client(int s, int e) {
  int cli_fd = -1;
  struct sockaddr_in cli_addr;
  socklen_t cli_addr_len = sizeof(cli_addr);
  struct epoll_event ee;
  memset(&cli_addr, 0, sizeof(cli_addr));
  memset(&ee, 0, sizeof(ee));
  cli_fd = accept(s, (struct sockaddr *)&cli_addr, &cli_addr_len);
  if (cli_fd < 0) {
    printf("Cannot accept client: %s\n", strerror(errno));
    close(e);
    close(s);
    return -1;
  }
  ee.events = EPOLLIN;
  ee.data.fd = cli_fd;
  if (epoll_ctl(e, EPOLL_CTL_ADD, cli_fd, &ee) < 0) {
    printf("Cannot add client to epoll\n");
    close(e);
    close(s);
    return -1;
  }
  return cli_fd;
}


x86 assembly (clang, -O0):
<accept_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0x8,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11f4 <accept_client+0x94>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <accept_client+0xf8>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
lea    -0x28(%rbp),%rcx
call   1060 <epoll_ctl@plt>
cmp    $0x0,%rax
jge    1252 <accept_client+0xf2>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <accept_client+0xf8>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_client>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
movl   $0x8,0x14(%rsp)
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <accept@plt>
test   %eax,%eax
js     11fb <accept_client+0x8b>
mov    %eax,%ebp
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
mov    %r14d,%edi
mov    %ebp,%edx
call   1070 <epoll_ctl@plt>
test   %rax,%rax
jns    122e <accept_client+0xbe>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
jmp    121a <accept_client+0xaa>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf0(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"Cannot accept client: %s\0A\00"
@errno = global i32 0
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.1 = [28 x i8] c"Cannot add client to epoll\0A\00"
define i32 @accept_client(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca %struct.epoll_event
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 -1, i32* %6
store i32 8, i32* %8
%10 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 8)
%11 = bitcast %struct.epoll_event* %9 to %struct.sockaddr_in*
%12 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 8)
%13 = load i32, i32* %4
%14 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%15 = call i32 @accept(i32 %13, %struct.sockaddr* %14, i32* %8)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %3
br label %46
26: ; preds = %2
%27 = load i32, i32* @EPOLLIN
%28 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %9, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %6
%30 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %9, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* %5
%33 = load i32, i32* @EPOLL_CTL_ADD
%34 = load i32, i32* %6
%35 = bitcast %struct.epoll_event* %9 to %struct.sockaddr_in*
%36 = call i64 @epoll_ctl(i32 %32, i32 %33, i32 %34, %struct.sockaddr_in* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %26
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %3
br label %46
44: ; preds = %26
%45 = load i32, i32* %6
store i32 %45, i32* %3
br label %46
46: ; preds = %44, %38, %18
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"Cannot accept client: %s\0A\00"
@errno = global i32 0
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@str = [27 x i8] c"Cannot add client to epoll\00"
define i32 @accept_client(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca %struct.epoll_event
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 8, i32* %4
%8 = bitcast %struct.epoll_event* %5 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 8)
%10 = bitcast %struct.epoll_event* %5 to %struct.sockaddr_in*
%11 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 8)
%12 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %12, i32* %4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %28
19: ; preds = %2
%20 = load i32, i32* @EPOLLIN
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i64 0, i32 0, i32 0
store i32 %13, i32* %22
%23 = load i32, i32* @EPOLL_CTL_ADD
%24 = call i64 @epoll_ctl(i32 %1, i32 %23, i32 %13, %struct.sockaddr_in* %10)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %15, %26
%29 = call i32 @close(i32 %1)
%30 = call i32 @close(i32 %0)
br label %31
31: ; preds = %28, %19
%32 = phi i32 [ %13, %19 ], [ -1, %28 ]
ret i32 %32
}