#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 epoll_event {
  int events;
  TYPE_1__ data;
};

/* Variables and functions */
int EPOLLET;
int EPOLLIN;
int EPOLLRDHUP;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int epoll_ctl(int, int /*<<< orphan*/, int, struct epoll_event *);
int set_nonblocking_fd(int);
int add_fd(int epollfd, int fd) {
  int s;
  struct epoll_event event;
  event.data.fd = fd;
  event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
  s = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
  if (s != 0)
    return -1;
  s = set_nonblocking_fd(epollfd);
  if (s != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<add_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    0x2e84(%rip),%rcx
or     (%rcx),%eax
mov    0x2e83(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rcx
call   1030 <epoll_ctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1183 <add_fd+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <add_fd+0x8b>
mov    -0x8(%rbp),%edi
call   1040 <set_nonblocking_fd@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11a4 <add_fd+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ab <add_fd+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_fd>:
push   %rbx
sub    $0x10,%rsp
mov    0x2ebc(%rip),%rax
mov    0x2e95(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %esi,%edx
mov    %edi,%ebx
mov    0x2e8e(%rip),%rax
or     (%rax),%ecx
mov    %esi,0xc(%rsp)
mov    %ecx,0x8(%rsp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
call   1030 <epoll_ctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    1175 <add_fd+0x55>
mov    %ebx,%edi
call   1040 <set_nonblocking_fd@plt>
neg    %eax
sbb    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLLRDHUP = global i32 0
@EPOLL_CTL_ADD = global i32 0
define i32 @add_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.epoll_event
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 1
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* @EPOLLIN
%12 = load i32, i32* @EPOLLET
%13 = or i32 %11, %12
%14 = load i32, i32* @EPOLLRDHUP
%15 = or i32 %13, %14
%16 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %4
%18 = load i32, i32* @EPOLL_CTL_ADD
%19 = load i32, i32* %5
%20 = call i32 @epoll_ctl(i32 %17, i32 %18, i32 %19, %struct.epoll_event* %7)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 -1, i32* %3
br label %31
24: ; preds = %2
%25 = load i32, i32* %4
%26 = call i32 @set_nonblocking_fd(i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 -1, i32* %3
br label %31
30: ; preds = %24
store i32 0, i32* %3
br label %31
31: ; preds = %30, %29, %23
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLLRDHUP = global i32 0
@EPOLL_CTL_ADD = global i32 0
define i32 @add_fd(i32 %0, i32 %1) {
%3 = alloca %struct.epoll_event
%4 = bitcast %struct.epoll_event* %3 to i8*
%5 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 1, i32 0
store i32 %1, i32* %5
%6 = load i32, i32* @EPOLLIN
%7 = load i32, i32* @EPOLLET
%8 = or i32 %7, %6
%9 = load i32, i32* @EPOLLRDHUP
%10 = or i32 %8, %9
%11 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @EPOLL_CTL_ADD
%13 = call i32 @epoll_ctl(i32 %0, i32 %12, i32 %1, %struct.epoll_event* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @set_nonblocking_fd(i32 %0)
%17 = icmp ne i32 %16, 0
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %15, %2
%20 = phi i32 [ -1, %2 ], [ %18, %15 ]
ret i32 %20
}