#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 {
  TYPE_1__ data;
  int /*<<< orphan*/ events;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ EPOLL_CTL_DEL;
int /*<<< orphan*/ close(int);
int dup(int);
int epoll_create(int);
int epoll_ctl(int, int /*<<< orphan*/, int, struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int pfd[2];
  pipe(pfd);
  int rfd = pfd[0];
  int wfd = pfd[1];
  char buf[1] = {0};
  write(wfd, buf, sizeof(buf));
  int epfd = epoll_create(1);
  struct epoll_event ev = {.events = EPOLLIN, .data.fd = rfd};
  epoll_ctl(epfd, EPOLL_CTL_ADD, rfd, &ev);
  int rfd2 = dup(rfd);
  printf("close(fd=%d)\n", rfd);
  close(rfd);
  int r = epoll_wait(epfd, &ev, 1, 1);
  if (r == 0) {
    printf("epoll_wait() finished with no events... okay...\n");
  } else {
    printf("epoll_wait() got Event! on fd %d! wait...\n", ev.data.fd);
  }
  r = epoll_ctl(epfd, EPOLL_CTL_DEL, rfd, NULL);
  if (r != 0) {
    perror("epoll_ctl(EPOLL_CTL_DEL, rfd)");
  }
  r = epoll_ctl(epfd, EPOLL_CTL_DEL, rfd2, NULL);
  if (r != 0) {
    perror("epoll_ctl(EPOLL_CTL_DEL, rfd2)");
  }
  rfd = dup(rfd2);
  r = epoll_ctl(epfd, EPOLL_CTL_DEL, rfd, NULL);
  if (r == 0) {
    printf("epoll_ctl(EPOLL_CTL_DEL, rfd)... okay? how come?\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x15(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   10c0 <epoll_create@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
lea    -0x28(%rbp),%rcx
call   1090 <epoll_ctl@plt>
mov    -0x10(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%esi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%ecx
mov    %ecx,%edx
call   10a0 <epoll_wait@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1273 <main+0xd3>
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1284 <main+0xe4>
mov    -0x28(%rbp),%esi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <epoll_ctl@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     12b5 <main+0x115>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <epoll_ctl@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     12e6 <main+0x146>
lea    0xda7(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <epoll_ctl@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1324 <main+0x184>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
mov    0x10(%rsp),%ebp
mov    0x14(%rsp),%edi
movb   $0x0,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   10c0 <epoll_create@plt>
mov    %eax,%ebx
mov    %ebp,0x8(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%r15
mov    %ebx,%edi
mov    %ebp,%edx
mov    %r15,%rcx
call   1090 <epoll_ctl@plt>
mov    %ebp,%edi
call   1060 <dup@plt>
mov    %eax,%r14d
lea    0xde8(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
call   10a0 <epoll_wait@plt>
test   %eax,%eax
je     1254 <main+0xb4>
mov    0x8(%rsp),%esi
lea    0xdc3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1260 <main+0xc0>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d91(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1090 <epoll_ctl@plt>
test   %eax,%eax
jne    12bf <main+0x11f>
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1090 <epoll_ctl@plt>
test   %eax,%eax
jne    12cd <main+0x12d>
mov    %r14d,%edi
call   1060 <dup@plt>
mov    (%r15),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %ecx,%ecx
call   1090 <epoll_ctl@plt>
test   %eax,%eax
jne    12b2 <main+0x112>
lea    0xdc9(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
jmp    1279 <main+0xd9>
lea    0xd83(%rip),%rdi
call   10b0 <perror@plt>
jmp    128c <main+0xec>

LLVM IR (-O0):
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str = [14 x i8] c"close(fd=%d)\0A\00"
@.str.1 = [49 x i8] c"epoll_wait() finished with no events... okay...\0A\00"
@.str.2 = [43 x i8] c"epoll_wait() got Event! on fd %d! wait...\0A\00"
@EPOLL_CTL_DEL = global i32 0
@.str.3 = [30 x i8] c"epoll_ctl(EPOLL_CTL_DEL, rfd)\00"
@.str.4 = [31 x i8] c"epoll_ctl(EPOLL_CTL_DEL, rfd2)\00"
@.str.5 = [50 x i8] c"epoll_ctl(EPOLL_CTL_DEL, rfd)... okay? how come?\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x i8]
%6 = alloca i32
%7 = alloca %struct.epoll_event
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = load i32, i32* %12
store i32 %13, i32* %3
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%15 = load i32, i32* %14
store i32 %15, i32* %4
%16 = bitcast [1 x i8]* %5 to i8*
%17 = load i32, i32* %4
%18 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 1)
%20 = call i32 @epoll_create(i32 1)
store i32 %20, i32* %6
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
%23 = load i32, i32* %3
store i32 %23, i32* %22
%24 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 1
%25 = load i32, i32* @EPOLLIN
store i32 %25, i32* %24
%26 = load i32, i32* %6
%27 = load i32, i32* @EPOLL_CTL_ADD
%28 = load i32, i32* %3
%29 = call i32 @epoll_ctl(i32 %26, i32 %27, i32 %28, %struct.epoll_event* %7)
%30 = load i32, i32* %3
%31 = call i32 @dup(i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %6
%37 = call i32 @epoll_wait(i32 %36, %struct.epoll_event* %7, i32 1, i32 1)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %47
42: ; preds = %0
%43 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %42, %40
%48 = load i32, i32* %6
%49 = load i32, i32* @EPOLL_CTL_DEL
%50 = load i32, i32* %3
%51 = call i32 @epoll_ctl(i32 %48, i32 %49, i32 %50, %struct.epoll_event* null)
store i32 %51, i32* %9
%52 = load i32, i32* %9
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %47
%57 = load i32, i32* %6
%58 = load i32, i32* @EPOLL_CTL_DEL
%59 = load i32, i32* %8
%60 = call i32 @epoll_ctl(i32 %57, i32 %58, i32 %59, %struct.epoll_event* null)
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %56
%64 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %56
%66 = load i32, i32* %8
%67 = call i32 @dup(i32 %66)
store i32 %67, i32* %3
%68 = load i32, i32* %6
%69 = load i32, i32* @EPOLL_CTL_DEL
%70 = load i32, i32* %3
%71 = call i32 @epoll_ctl(i32 %68, i32 %69, i32 %70, %struct.epoll_event* null)
store i32 %71, i32* %9
%72 = load i32, i32* %9
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %65
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %65
ret i32 0
}

LLVM IR (-O2):
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str = [14 x i8] c"close(fd=%d)\0A\00"
@.str.2 = [43 x i8] c"epoll_wait() got Event! on fd %d! wait...\0A\00"
@EPOLL_CTL_DEL = global i32 0
@.str.3 = [30 x i8] c"epoll_ctl(EPOLL_CTL_DEL, rfd)\00"
@.str.4 = [31 x i8] c"epoll_ctl(EPOLL_CTL_DEL, rfd2)\00"
@str = [49 x i8] c"epoll_ctl(EPOLL_CTL_DEL, rfd)... okay? how come?\00"
@str.6 = [48 x i8] c"epoll_wait() finished with no events... okay...\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1 x i8]
%3 = alloca %struct.epoll_event
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = load i32, i32* %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
store i8 0, i8* %10
%11 = call i32 @write(i32 %9, i8* %10, i32 1)
%12 = call i32 @epoll_create(i32 1)
%13 = bitcast %struct.epoll_event* %3 to i8*
%14 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0, i32 0
store i32 %7, i32* %14
%15 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 1
%16 = load i32, i32* @EPOLLIN
store i32 %16, i32* %15
%17 = load i32, i32* @EPOLL_CTL_ADD
%18 = call i32 @epoll_ctl(i32 %12, i32 %17, i32 %7, %struct.epoll_event* %3)
%19 = call i32 @dup(i32 %7)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%21 = call i32 @close(i32 %7)
%22 = call i32 @epoll_wait(i32 %12, %struct.epoll_event* %3, i32 1, i32 1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %0
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.6, i64 0, i64 0))
br label %29
26: ; preds = %0
%27 = load i32, i32* %14
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %24
%30 = load i32, i32* @EPOLL_CTL_DEL
%31 = call i32 @epoll_ctl(i32 %12, i32 %30, i32 %7, %struct.epoll_event* null)
%32 = icmp eq i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* @EPOLL_CTL_DEL
%37 = call i32 @epoll_ctl(i32 %12, i32 %36, i32 %19, %struct.epoll_event* null)
%38 = icmp eq i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = call i32 @dup(i32 %19)
%43 = load i32, i32* @EPOLL_CTL_DEL
%44 = call i32 @epoll_ctl(i32 %12, i32 %43, i32 %42, %struct.epoll_event* null)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
ret i32 0
}