#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;
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int /*<<< orphan*/ events;
  TYPE_1__ data;
};

/* Variables and functions */
int EPOLL_CTL_ADD;
int epoll_ctl(int, int, int, struct epoll_event *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(struct epoll_event *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int ctl_fd(int epfd, int fd, int ctl, uint32_t events) {
  int ret;
  struct epoll_event ev;
  memset(&ev, 0, sizeof(struct epoll_event));
  ev.data.fd = fd;
  if (ctl == EPOLL_CTL_ADD) {
    ev.events = events;
  }
  if ((ret = epoll_ctl(epfd, ctl, fd, &ev)) < 0) {
    perror("epoll_ctl");
    fprintf(stderr, "ctl fd %d error\n", fd);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ctl_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%eax
jne    1182 <ctl_fd+0x42>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    -0x20(%rbp),%rcx
call   1040 <epoll_ctl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11d0 <ctl_fd+0x90>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <ctl_fd+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctl_fd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    %r15d,0xc(%rsp)
mov    0x2e72(%rip),%rax
cmp    %ebp,(%rax)
jne    117f <ctl_fd+0x3f>
mov    %r12d,0x8(%rsp)
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r15d,%edx
call   1040 <epoll_ctl@plt>
test   %eax,%eax
js     11a4 <ctl_fd+0x64>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    %r15d,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%r14d
jmp    1194 <ctl_fd+0x54>

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLL_CTL_ADD = global i32 0
@.str = [10 x i8] c"epoll_ctl\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"ctl fd %d error\0A\00"
define i32 @ctl_fd(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.epoll_event
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = call i32 @memset(%struct.epoll_event* %11, i32 0, i32 8)
%13 = load i32, i32* %7
%14 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %8
%17 = load i32, i32* @EPOLL_CTL_ADD
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %4
%20 = load i32, i32* %9
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 0
store i32 %20, i32* %21
br label %22
22: ; preds = %19, %4
%23 = load i32, i32* %6
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = call i32 @epoll_ctl(i32 %23, i32 %24, i32 %25, %struct.epoll_event* %11)
store i32 %26, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = load i32, i32* %7
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 -1, i32* %5
br label %34
33: ; preds = %22
store i32 0, i32* %5
br label %34
34: ; preds = %33, %28
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLL_CTL_ADD = global i32 0
@.str = [10 x i8] c"epoll_ctl\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"ctl fd %d error\0A\00"
define i32 @ctl_fd(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.epoll_event
%6 = bitcast %struct.epoll_event* %5 to i8*
%7 = call i32 @memset(%struct.epoll_event* %5, i32 0, i32 8)
%8 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i64 0, i32 1, i32 0
store i32 %1, i32* %8
%9 = load i32, i32* @EPOLL_CTL_ADD
%10 = icmp eq i32 %9, %2
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i64 0, i32 0
store i32 %3, i32* %12
br label %13
13: ; preds = %11, %4
%14 = call i32 @epoll_ctl(i32 %0, i32 %2, i32 %1, %struct.epoll_event* %5)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %20
20: ; preds = %13, %16
%21 = phi i32 [ -1, %16 ], [ 0, %13 ]
ret i32 %21
}