#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);
scalar_t__ gpio_event_added(unsigned int);
int /*<<< orphan*/ gpio_export(unsigned int);
int /*<<< orphan*/ gpio_set_direction(unsigned int, int);
int /*<<< orphan*/ gpio_set_edge(unsigned int, unsigned int);
int /*<<< orphan*/ gpio_unexport(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 epfd, fd, n, i;
  struct epoll_event events, ev;
  char buf;
  if ((epfd = epoll_create(1)) == -1)
    return 1;
  if (gpio_event_added(gpio) != 0)
    return 2;
  gpio_export(gpio);
  gpio_set_direction(gpio, 1);
  gpio_set_edge(gpio, edge);
  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_unexport(gpio);
    close(fd);
    return 4;
  }
  for (i = 0; i < 2; i++)
    if ((n = epoll_wait(epfd, &events, 1, -1)) == -1) {
      gpio_unexport(gpio);
      close(fd);
      return 5;
    }
  if (n > 0) {
    lseek(events.data.fd, 0, SEEK_SET);
    if (read(events.data.fd, &buf, 1) != 1) {
      gpio_unexport(gpio);
      close(fd);
      return 6;
    }
    if (events.data.fd != fd) {
      gpio_unexport(gpio);
      close(fd);
      return 7;
    }
  }
  gpio_unexport(gpio);
  close(fd);
  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    $0x1,%edi
call   10d0 <epoll_create@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11f0 <blocking_wait_for_edge+0x30>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <blocking_wait_for_edge+0x1f0>
mov    -0x8(%rbp),%edi
call   1030 <gpio_event_added@plt>
cmp    $0x0,%rax
je     120e <blocking_wait_for_edge+0x4e>
movl   $0x2,-0x4(%rbp)
jmp    13b0 <blocking_wait_for_edge+0x1f0>
mov    -0x8(%rbp),%edi
call   1050 <gpio_export@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   10e0 <gpio_set_direction@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <gpio_set_edge@plt>
mov    -0x8(%rbp),%edi
call   10a0 <open_value_file@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    124e <blocking_wait_for_edge+0x8e>
movl   $0x3,-0x4(%rbp)
jmp    13b0 <blocking_wait_for_edge+0x1f0>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    0x2d62(%rip),%rcx
or     (%rcx),%eax
mov    0x2d89(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
lea    -0x30(%rbp),%rcx
call   1080 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    12af <blocking_wait_for_edge+0xef>
mov    -0x8(%rbp),%edi
call   10b0 <gpio_unexport@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x4,-0x4(%rbp)
jmp    13b0 <blocking_wait_for_edge+0x1f0>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    1311 <blocking_wait_for_edge+0x151>
mov    -0x10(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   10c0 <epoll_wait@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12fe <blocking_wait_for_edge+0x13e>
mov    -0x8(%rbp),%edi
call   10b0 <gpio_unexport@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x5,-0x4(%rbp)
jmp    13b0 <blocking_wait_for_edge+0x1f0>
jmp    1303 <blocking_wait_for_edge+0x143>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12b6 <blocking_wait_for_edge+0xf6>
cmpl   $0x0,-0x18(%rbp)
jle    1391 <blocking_wait_for_edge+0x1d1>
mov    -0x24(%rbp),%edi
mov    0x2ca3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x1,%eax
je     1364 <blocking_wait_for_edge+0x1a4>
mov    -0x8(%rbp),%edi
call   10b0 <gpio_unexport@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x6,-0x4(%rbp)
jmp    13b0 <blocking_wait_for_edge+0x1f0>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     138c <blocking_wait_for_edge+0x1cc>
mov    -0x8(%rbp),%edi
call   10b0 <gpio_unexport@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x7,-0x4(%rbp)
jmp    13b0 <blocking_wait_for_edge+0x1f0>
jmp    1391 <blocking_wait_for_edge+0x1d1>
mov    -0x8(%rbp),%edi
call   10b0 <gpio_unexport@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <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,%ebx
mov    %edi,%r12d
mov    $0x1,%ebp
mov    $0x1,%edi
call   10d0 <epoll_create@plt>
cmp    $0xffffffff,%eax
je     132c <blocking_wait_for_edge+0x16c>
mov    %eax,%r15d
mov    %r12d,%edi
call   1030 <gpio_event_added@plt>
mov    $0x2,%ebp
test   %rax,%rax
jne    132c <blocking_wait_for_edge+0x16c>
mov    %r12d,%edi
call   1050 <gpio_export@plt>
mov    %r12d,%edi
mov    $0x1,%esi
call   10e0 <gpio_set_direction@plt>
mov    %r12d,%edi
mov    %ebx,%esi
call   1070 <gpio_set_edge@plt>
mov    %r12d,%edi
call   10a0 <open_value_file@plt>
cmp    $0xffffffff,%eax
je     1301 <blocking_wait_for_edge+0x141>
mov    %eax,%r14d
mov    0x2da4(%rip),%rax
mov    0x2d7d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2da2(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x18(%rsp)
mov    %r14d,0x1c(%rsp)
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rcx
mov    %r15d,%edi
mov    %r14d,%edx
call   1080 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
je     1317 <blocking_wait_for_edge+0x157>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   10c0 <epoll_wait@plt>
cmp    $0xffffffff,%eax
je     1308 <blocking_wait_for_edge+0x148>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   10c0 <epoll_wait@plt>
cmp    $0xffffffff,%eax
je     1308 <blocking_wait_for_edge+0x148>
test   %eax,%eax
jle    12ea <blocking_wait_for_edge+0x12a>
mov    0x14(%rsp),%edi
mov    0x2d06(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x1,%eax
jne    133b <blocking_wait_for_edge+0x17b>
cmp    %r14d,0x14(%rsp)
jne    134a <blocking_wait_for_edge+0x18a>
mov    %r12d,%edi
call   10b0 <gpio_unexport@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
mov    %r15d,%r14d
jmp    1324 <blocking_wait_for_edge+0x164>
mov    $0x3,%ebp
jmp    132c <blocking_wait_for_edge+0x16c>
mov    %r12d,%edi
call   10b0 <gpio_unexport@plt>
mov    $0x5,%ebp
jmp    1324 <blocking_wait_for_edge+0x164>
mov    %r12d,%edi
call   10b0 <gpio_unexport@plt>
mov    $0x4,%ebp
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%edi
call   10b0 <gpio_unexport@plt>
mov    $0x6,%ebp
jmp    1324 <blocking_wait_for_edge+0x164>
mov    %r12d,%edi
call   10b0 <gpio_unexport@plt>
mov    $0x7,%ebp
jmp    1324 <blocking_wait_for_edge+0x164>

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

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