#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 {
  int /*<<< orphan*/ events;
  TYPE_1__ data;
};
struct sockaddr {
  int dummy;
};
struct epoll_event {
  int /*<<< orphan*/ events;
  TYPE_1__ data;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ e;
typedef int /*<<< orphan*/ cli_addr_len;
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*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int handle_server_fd(int server_fd, int epoll_fd) {
  int cli_fd = -1;
  struct sockaddr_in cli_addr;
  socklen_t cli_addr_len = sizeof(cli_addr_len);
  struct epoll_event e;
  memset(&cli_addr, 0, sizeof(cli_addr));
  cli_fd = accept(server_fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
  if (cli_fd < 0) {
    printf("Cannot accept client\n");
    close(epoll_fd);
    close(server_fd);
    return 1;
  }
  memset(&e, 0, sizeof(e));
  e.data.fd = cli_fd;
  e.events = EPOLLIN;
  if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, cli_fd, &e) < 0) {
    printf("Cannot accept client\n");
    close(epoll_fd);
    close(server_fd);
    return 1;
  }
}


x86 assembly (clang, -O0):
<handle_server_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0x4,-0x1c(%rbp)
lea    -0x18(%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    11c1 <handle_server_fd+0x71>
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1228 <handle_server_fd+0xd8>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
lea    -0x28(%rbp),%rcx
call   1060 <epoll_ctl@plt>
cmp    $0x0,%rax
jge    1228 <handle_server_fd+0xd8>
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_server_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <accept@plt>
test   %eax,%eax
js     11d0 <handle_server_fd+0x80>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %ebp,0xc(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %r15,%rcx
call   1060 <epoll_ctl@plt>
test   %rax,%rax
jns    11eb <handle_server_fd+0x9b>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%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.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Cannot accept client\0A\00"
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
define i32 @handle_server_fd(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 4, i32* %8
%10 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 8)
%11 = load i32, i32* %4
%12 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%13 = call i32 @accept(i32 %11, %struct.sockaddr* %12, i32* %8)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
store i32 1, i32* %3
br label %42
22: ; preds = %2
%23 = bitcast %struct.epoll_event* %9 to %struct.sockaddr_in*
%24 = call i32 @memset(%struct.sockaddr_in* %23, i32 0, i32 8)
%25 = load i32, i32* %6
%26 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %9, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* @EPOLLIN
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %9, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %5
%31 = load i32, i32* @EPOLL_CTL_ADD
%32 = load i32, i32* %6
%33 = bitcast %struct.epoll_event* %9 to %struct.sockaddr_in*
%34 = call i64 @epoll_ctl(i32 %30, i32 %31, i32 %32, %struct.sockaddr_in* %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %22
%37 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
store i32 1, i32* %3
br label %42
42: ; preds = %16, %36, %22
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.sockaddr = type { i32 }
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@str.1 = [21 x i8] c"Cannot accept client\00"
define i32 @handle_server_fd(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 4, 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.sockaddr_in* %3 to %struct.sockaddr*
%11 = call i32 @accept(i32 %0, %struct.sockaddr* %10, i32* %4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %2
%14 = bitcast %struct.epoll_event* %5 to %struct.sockaddr_in*
%15 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 8)
%16 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i64 0, i32 1, i32 0
store i32 %11, i32* %16
%17 = load i32, i32* @EPOLLIN
%18 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @EPOLL_CTL_ADD
%20 = call i64 @epoll_ctl(i32 %1, i32 %19, i32 %11, %struct.sockaddr_in* %14)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %13, %2
%23 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.1, i64 0, i64 0), %13 ]
%24 = call i32 @puts(i8* dereferenceable(1) %23)
%25 = call i32 @close(i32 %1)
%26 = call i32 @close(i32 %0)
br label %27
27: ; preds = %22, %13
ret i32 1
}