#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 EPOLLPRI;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(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 fd_lookup(unsigned int);
scalar_t__ gpio_event_add(unsigned int);
int /*<<< orphan*/ gpio_event_remove(unsigned int);
int /*<<< orphan*/ gpio_export(unsigned int);
int /*<<< orphan*/ gpio_set_direction(unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ gpio_set_edge(unsigned int, unsigned int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open_value_file(unsigned int);
int read(int, char *, int);
int blocking_wait_for_edge(unsigned int gpio, unsigned int edge) {
  int fd = fd_lookup(gpio);
  int epfd, n, i;
  struct epoll_event events, ev;
  char buf;
  if ((epfd = epoll_create(1)) == -1)
    return 1;
  if (gpio_event_add(gpio) != 0)
    return 2;
  gpio_export(gpio);
  gpio_set_direction(gpio, 0);
  gpio_set_edge(gpio, edge);
  if (!fd) {
    if ((fd = open_value_file(gpio)) == -1)
      return 3;
  }
  ev.events = EPOLLIN | EPOLLET | EPOLLPRI;
  ev.data.fd = fd;
  if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
    gpio_event_remove(gpio);
    return 4;
  }
  for (i = 0; i < 2; i++)
    if ((n = epoll_wait(epfd, &events, 1, -1)) == -1) {
      gpio_event_remove(gpio);
      return 5;
    }
  if (n > 0) {
    lseek(events.data.fd, 0, SEEK_SET);
    if (read(events.data.fd, &buf, 1) != 1) {
      gpio_event_remove(gpio);
      return 6;
    }
    if (events.data.fd != fd) {
      gpio_event_remove(gpio);
      return 7;
    }
  }
  gpio_event_remove(gpio);
  close(epfd);
  return 0;
}


x86 assembly (clang, -O0):
<blocking_wait_for_edge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   10c0 <fd_lookup@plt>
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   10e0 <epoll_create@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    120b <blocking_wait_for_edge+0x3b>
movl   $0x1,-0x4(%rbp)
jmp    13af <blocking_wait_for_edge+0x1df>
mov    -0x8(%rbp),%edi
call   1040 <gpio_event_add@plt>
cmp    $0x0,%rax
je     1229 <blocking_wait_for_edge+0x59>
movl   $0x2,-0x4(%rbp)
jmp    13af <blocking_wait_for_edge+0x1df>
mov    -0x8(%rbp),%edi
call   1060 <gpio_export@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   10f0 <gpio_set_direction@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1080 <gpio_set_edge@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1275 <blocking_wait_for_edge+0xa5>
mov    -0x8(%rbp),%edi
call   10b0 <open_value_file@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1270 <blocking_wait_for_edge+0xa0>
movl   $0x3,-0x4(%rbp)
jmp    13af <blocking_wait_for_edge+0x1df>
jmp    1275 <blocking_wait_for_edge+0xa5>
mov    0x2d64(%rip),%rax
mov    (%rax),%eax
mov    0x2d3b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d62(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
lea    -0x30(%rbp),%rcx
call   1090 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    12ce <blocking_wait_for_edge+0xfe>
mov    -0x8(%rbp),%edi
call   1030 <gpio_event_remove@plt>
movl   $0x4,-0x4(%rbp)
jmp    13af <blocking_wait_for_edge+0x1df>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    1328 <blocking_wait_for_edge+0x158>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   10d0 <epoll_wait@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1315 <blocking_wait_for_edge+0x145>
mov    -0x8(%rbp),%edi
call   1030 <gpio_event_remove@plt>
movl   $0x5,-0x4(%rbp)
jmp    13af <blocking_wait_for_edge+0x1df>
jmp    131a <blocking_wait_for_edge+0x14a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12d5 <blocking_wait_for_edge+0x105>
cmpl   $0x0,-0x18(%rbp)
jle    1398 <blocking_wait_for_edge+0x1c8>
mov    -0x24(%rbp),%edi
mov    0x2c8c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
cmp    $0x1,%eax
je     1373 <blocking_wait_for_edge+0x1a3>
mov    -0x8(%rbp),%edi
call   1030 <gpio_event_remove@plt>
movl   $0x6,-0x4(%rbp)
jmp    13af <blocking_wait_for_edge+0x1df>
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     1393 <blocking_wait_for_edge+0x1c3>
mov    -0x8(%rbp),%edi
call   1030 <gpio_event_remove@plt>
movl   $0x7,-0x4(%rbp)
jmp    13af <blocking_wait_for_edge+0x1df>
jmp    1398 <blocking_wait_for_edge+0x1c8>
mov    -0x8(%rbp),%edi
call   1030 <gpio_event_remove@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blocking_wait_for_edge>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r12d
mov    %edi,%ebx
call   10c0 <fd_lookup@plt>
mov    %eax,%r14d
mov    $0x1,%ebp
mov    $0x1,%edi
call   10e0 <epoll_create@plt>
cmp    $0xffffffff,%eax
je     1211 <blocking_wait_for_edge+0x41>
mov    %eax,%r15d
mov    %ebx,%edi
call   1040 <gpio_event_add@plt>
mov    $0x2,%ebp
test   %rax,%rax
je     1220 <blocking_wait_for_edge+0x50>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1060 <gpio_export@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   10f0 <gpio_set_direction@plt>
mov    %ebx,%edi
mov    %r12d,%esi
call   1080 <gpio_set_edge@plt>
test   %r14d,%r14d
jne    1252 <blocking_wait_for_edge+0x82>
mov    %ebx,%edi
call   10b0 <open_value_file@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     133f <blocking_wait_for_edge+0x16f>
mov    0x2d87(%rip),%rax
mov    0x2d60(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d85(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x18(%rsp)
mov    %r14d,0x1c(%rsp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rcx
mov    %r15d,%edi
mov    %r14d,%edx
call   1090 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
je     132e <blocking_wait_for_edge+0x15e>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   10d0 <epoll_wait@plt>
cmp    $0xffffffff,%eax
je     131d <blocking_wait_for_edge+0x14d>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   10d0 <epoll_wait@plt>
cmp    $0xffffffff,%eax
je     131d <blocking_wait_for_edge+0x14d>
test   %eax,%eax
jle    1307 <blocking_wait_for_edge+0x137>
mov    0x14(%rsp),%edi
mov    0x2ce9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
cmp    $0x1,%eax
jne    1349 <blocking_wait_for_edge+0x179>
cmp    %r14d,0x14(%rsp)
jne    135a <blocking_wait_for_edge+0x18a>
mov    %ebx,%edi
call   1030 <gpio_event_remove@plt>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %ebp,%ebp
jmp    1211 <blocking_wait_for_edge+0x41>
mov    %ebx,%edi
call   1030 <gpio_event_remove@plt>
mov    $0x5,%ebp
jmp    1211 <blocking_wait_for_edge+0x41>
mov    %ebx,%edi
call   1030 <gpio_event_remove@plt>
mov    $0x4,%ebp
jmp    1211 <blocking_wait_for_edge+0x41>
mov    $0x3,%ebp
jmp    1211 <blocking_wait_for_edge+0x41>
mov    %ebx,%edi
call   1030 <gpio_event_remove@plt>
mov    $0x6,%ebp
jmp    1211 <blocking_wait_for_edge+0x41>
mov    %ebx,%edi
call   1030 <gpio_event_remove@plt>
mov    $0x7,%ebp
jmp    1211 <blocking_wait_for_edge+0x41>

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLLPRI = global i32 0
@EPOLL_CTL_ADD = global i32 0
@SEEK_SET = global i32 0
define i32 @blocking_wait_for_edge(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.epoll_event
%11 = alloca %struct.epoll_event
%12 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
%13 = load i32, i32* %4
%14 = call i32 @fd_lookup(i32 %13)
store i32 %14, i32* %6
%15 = call i32 @epoll_create(i32 1)
store i32 %15, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 1, i32* %3
br label %104
18: ; preds = %2
%19 = load i32, i32* %4
%20 = call i64 @gpio_event_add(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 2, i32* %3
br label %104
23: ; preds = %18
%24 = load i32, i32* %4
%25 = call i32 @gpio_export(i32 %24)
%26 = load i32, i32* %4
%27 = call i32 @gpio_set_direction(i32 %26, i32 0)
%28 = load i32, i32* %4
%29 = load i32, i32* %5
%30 = call i32 @gpio_set_edge(i32 %28, i32 %29)
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %23
%34 = load i32, i32* %4
%35 = call i32 @open_value_file(i32 %34)
store i32 %35, i32* %6
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 3, i32* %3
br label %104
38: ; preds = %33
br label %39
39: ; preds = %38, %23
%40 = load i32, i32* @EPOLLIN
%41 = load i32, i32* @EPOLLET
%42 = or i32 %40, %41
%43 = load i32, i32* @EPOLLPRI
%44 = or i32 %42, %43
%45 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %6
%47 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 1
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
store i32 %46, i32* %48
%49 = load i32, i32* %7
%50 = load i32, i32* @EPOLL_CTL_ADD
%51 = load i32, i32* %6
%52 = call i32 @epoll_ctl(i32 %49, i32 %50, i32 %51, %struct.epoll_event* %11)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %39
%55 = load i32, i32* %4
%56 = call i32 @gpio_event_remove(i32 %55)
store i32 4, i32* %3
br label %104
57: ; preds = %39
store i32 0, i32* %9
br label %58
58: ; preds = %69, %57
%59 = load i32, i32* %9
%60 = icmp slt i32 %59, 2
br i1 %60, label %61, label %72
61: ; preds = %58
%62 = load i32, i32* %7
%63 = call i32 @epoll_wait(i32 %62, %struct.epoll_event* %10, i32 1, i32 -1)
store i32 %63, i32* %8
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = load i32, i32* %4
%67 = call i32 @gpio_event_remove(i32 %66)
store i32 5, i32* %3
br label %104
68: ; preds = %61
br label %69
69: ; preds = %68
%70 = load i32, i32* %9
%71 = add nsw i32 %70, 1
store i32 %71, i32* %9
br label %58
72: ; preds = %58
%73 = load i32, i32* %8
%74 = icmp sgt i32 %73, 0
br i1 %74, label %75, label %99
75: ; preds = %72
%76 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %10, i32 0, i32 1
%77 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = load i32, i32* @SEEK_SET
%80 = call i32 @lseek(i32 %78, i32 0, i32 %79)
%81 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %10, i32 0, i32 1
%82 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = call i32 @read(i32 %83, i8* %12, i32 1)
%85 = icmp ne i32 %84, 1
br i1 %85, label %86, label %89
86: ; preds = %75
%87 = load i32, i32* %4
%88 = call i32 @gpio_event_remove(i32 %87)
store i32 6, i32* %3
br label %104
89: ; preds = %75
%90 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %10, i32 0, i32 1
%91 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %90, i32 0, i32 0
%92 = load i32, i32* %91
%93 = load i32, i32* %6
%94 = icmp ne i32 %92, %93
br i1 %94, label %95, label %98
95: ; preds = %89
%96 = load i32, i32* %4
%97 = call i32 @gpio_event_remove(i32 %96)
store i32 7, i32* %3
br label %104
98: ; preds = %89
br label %99
99: ; preds = %98, %72
%100 = load i32, i32* %4
%101 = call i32 @gpio_event_remove(i32 %100)
%102 = load i32, i32* %7
%103 = call i32 @close(i32 %102)
store i32 0, i32* %3
br label %104
104: ; preds = %99, %95, %86, %65, %54, %37, %22, %17
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLLPRI = global i32 0
@EPOLL_CTL_ADD = global i32 0
@SEEK_SET = global i32 0
define i32 @blocking_wait_for_edge(i32 %0, i32 %1) {
%3 = alloca %struct.epoll_event
%4 = alloca %struct.epoll_event
%5 = alloca i8
%6 = tail call i32 @fd_lookup(i32 %0)
%7 = bitcast %struct.epoll_event* %3 to i8*
%8 = bitcast %struct.epoll_event* %4 to i8*
%9 = tail call i32 @epoll_create(i32 1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %64, label %11
11: ; preds = %2
%12 = tail call i64 @gpio_event_add(i32 %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %64
14: ; preds = %11
%15 = tail call i32 @gpio_export(i32 %0)
%16 = tail call i32 @gpio_set_direction(i32 %0, i32 0)
%17 = tail call i32 @gpio_set_edge(i32 %0, i32 %1)
%18 = icmp eq i32 %6, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @open_value_file(i32 %0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %64, label %22
22: ; preds = %19, %14
%23 = phi i32 [ %6, %14 ], [ %20, %19 ]
%24 = load i32, i32* @EPOLLIN
%25 = load i32, i32* @EPOLLET
%26 = or i32 %25, %24
%27 = load i32, i32* @EPOLLPRI
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i64 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i64 0, i32 1, i32 0
store i32 %23, i32* %30
%31 = load i32, i32* @EPOLL_CTL_ADD
%32 = call i32 @epoll_ctl(i32 %9, i32 %31, i32 %23, %struct.epoll_event* %4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %37, label %34
34: ; preds = %22
%35 = call i32 @epoll_wait(i32 %9, %struct.epoll_event* %3, i32 1, i32 -1)
%36 = icmp eq i32 %35, -1
br i1 %36, label %44, label %39
37: ; preds = %22
%38 = call i32 @gpio_event_remove(i32 %0)
br label %64
39: ; preds = %34
%40 = call i32 @epoll_wait(i32 %9, %struct.epoll_event* %3, i32 1, i32 -1)
%41 = icmp eq i32 %40, -1
br i1 %41, label %44, label %42
42: ; preds = %39
%43 = icmp sgt i32 %40, 0
br i1 %43, label %46, label %61
44: ; preds = %39, %34
%45 = call i32 @gpio_event_remove(i32 %0)
br label %64
46: ; preds = %42
%47 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 1, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @lseek(i32 %48, i32 0, i32 %49)
%51 = load i32, i32* %47
%52 = call i32 @read(i32 %51, i8* %5, i32 1)
%53 = icmp eq i32 %52, 1
br i1 %53, label %56, label %54
54: ; preds = %46
%55 = call i32 @gpio_event_remove(i32 %0)
br label %64
56: ; preds = %46
%57 = load i32, i32* %47
%58 = icmp eq i32 %57, %23
br i1 %58, label %61, label %59
59: ; preds = %56
%60 = call i32 @gpio_event_remove(i32 %0)
br label %64
61: ; preds = %56, %42
%62 = call i32 @gpio_event_remove(i32 %0)
%63 = call i32 @close(i32 %9)
br label %64
64: ; preds = %19, %11, %2, %61, %59, %54, %44, %37
%65 = phi i32 [ 4, %37 ], [ 5, %44 ], [ 6, %54 ], [ 7, %59 ], [ 0, %61 ], [ 1, %2 ], [ 2, %11 ], [ 3, %19 ]
ret i32 %65
}