#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 */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef int /*<<< orphan*/ fdset;

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int POLLPRI;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ Write_number_to_7segdisplay(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ initialize_all_gpios();
scalar_t__ initialize_button_gpio();
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int readvalue = 0;
  uint8_t led_counter;
  int gpio_fd, timeout, rc;
  struct pollfd fdset[2];
  uint8_t buf[100];
  int nfds = 2;
  printf("This program counts button press up to 9 on 7segment display\n");
  if ((initialize_all_gpios() < 0) || (initialize_button_gpio() < 0)) {
    printf("Error: Could not able to initialize the gpios..Exiting..\n");
  } else {
    printf("Waiting for the button press \n");
    gpio_fd = open("/sys/class/gpio/gpio49/value", O_RDONLY | O_NONBLOCK);
    if (gpio_fd < 0) {
      perror(" open failed\n");
    }
    timeout = (3 * 1000);
    while (1) {
      memset((void *)fdset, 0, sizeof(fdset));
      fdset[1].fd = gpio_fd;
      fdset[1].events = POLLPRI;
      rc = poll(fdset, nfds, timeout);
      if (rc < 0) {
        printf("\npoll() failed!\n");
        return -1;
      }
      if (rc == 0) {
      }
      if (fdset[1].revents & POLLPRI) {
        lseek(fdset[1].fd, 0, SEEK_SET);
        read(fdset[1].fd, buf, 100);
        led_counter++;
        Write_number_to_7segdisplay(led_counter);
        printf("\npoll() GPIO %d interrupt occurred\n", readvalue);
      }
    }
    close(gpio_fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1e0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x2,-0x1d4(%rbp)
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <initialize_all_gpios@plt>
cmp    $0x0,%rax
jl     11fa <main+0x5a>
mov    $0x0,%al
call   1030 <initialize_button_gpio@plt>
cmp    $0x0,%rax
jge    120d <main+0x6d>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1326 <main+0x186>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    0x2db5(%rip),%rax
or     (%rax),%esi
lea    0xe63(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1252 <main+0xb2>
lea    0xe67(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xbb8,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x1d4(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1090 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12b4 <main+0x114>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <main+0x186>
cmpl   $0x0,-0x24(%rbp)
jne    12c3 <main+0x123>
jmp    12c3 <main+0x123>
mov    -0x2c(%rbp),%eax
mov    0x2d23(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1321 <main+0x181>
mov    -0x34(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x34(%rbp),%edi
lea    -0x1d0(%rbp),%rsi
mov    $0x64,%edx
call   1070 <read@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   10c0 <Write_number_to_7segdisplay@plt>
mov    -0x14(%rbp),%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1259 <main+0xb9>
mov    -0x4(%rbp),%eax
add    $0x1e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1b8,%rsp
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <initialize_all_gpios@plt>
test   %rax,%rax
js     12f9 <main+0x149>
xor    %eax,%eax
call   1040 <initialize_button_gpio@plt>
test   %rax,%rax
js     12f9 <main+0x149>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd6(%rip),%rax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdec(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
js     132c <main+0x17c>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    %r12d,0xc(%rsp)
mov    0x2dae(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0xbb8,%edx
call   10a0 <poll@plt>
test   %eax,%eax
js     1307 <main+0x157>
mov    %rsp,%rbx
lea    0x20(%rsp),%r14
lea    0xdb8(%rip),%r15
jmp    12b2 <main+0x102>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    %r12d,0xc(%rsp)
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0xbb8,%edx
call   10a0 <poll@plt>
test   %eax,%eax
js     1307 <main+0x157>
mov    0x0(%r13),%eax
test   %eax,0x14(%rsp)
je     1280 <main+0xd0>
mov    0xc(%rsp),%edi
mov    0x2d01(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
add    $0x1,%ebp
mov    %ebp,%edi
call   10d0 <Write_number_to_7segdisplay@plt>
mov    %r15,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1280 <main+0xd0>
lea    0xdba(%rip),%rdi
call   1030 <puts@plt>
jmp    1318 <main+0x168>
lea    0xd9c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x1b8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcea(%rip),%rdi
call   10c0 <perror@plt>
jmp    1224 <main+0x74>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [62 x i8] c"This program counts button press up to 9 on 7segment display\0A\00"
@.str.1 = [58 x i8] c"Error: Could not able to initialize the gpios..Exiting..\0A\00"
@.str.2 = [31 x i8] c"Waiting for the button press \0A\00"
@.str.3 = [29 x i8] c"/sys/class/gpio/gpio49/value\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [14 x i8] c" open failed\0A\00"
@POLLPRI = global i32 0
@.str.5 = [17 x i8] c"\0Apoll() failed!\0A\00"
@SEEK_SET = global i32 0
@.str.6 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x %struct.pollfd]
%12 = alloca [100 x i32]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 2, i32* %13
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
%15 = call i64 (...) @initialize_all_gpios()
%16 = icmp slt i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = call i64 (...) @initialize_button_gpio()
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17, %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
br label %80
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @O_RDONLY
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %24, %25
%27 = call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
store i32 3000, i32* %9
br label %33
33: ; preds = %32, %79
%34 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %11, i64 0, i64 0
%35 = bitcast %struct.pollfd* %34 to i8*
%36 = call i32 @memset(i8* %35, i32 0, i32 24)
%37 = load i32, i32* %8
%38 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %11, i64 0, i64 1
%39 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* @POLLPRI
%41 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %11, i64 0, i64 1
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %41, i32 0, i32 1
store i32 %40, i32* %42
%43 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %11, i64 0, i64 0
%44 = load i32, i32* %13
%45 = load i32, i32* %9
%46 = call i32 @poll(%struct.pollfd* %43, i32 %44, i32 %45)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %33
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %80
51: ; preds = %33
%52 = load i32, i32* %10
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
br label %55
55: ; preds = %54, %51
%56 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %11, i64 0, i64 1
%57 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %56, i32 0, i32 2
%58 = load i32, i32* %57
%59 = load i32, i32* @POLLPRI
%60 = and i32 %58, %59
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %79
62: ; preds = %55
%63 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %11, i64 0, i64 1
%64 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = load i32, i32* @SEEK_SET
%67 = call i32 @lseek(i32 %65, i32 0, i32 %66)
%68 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %11, i64 0, i64 1
%69 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %68, i32 0, i32 0
%70 = load i32, i32* %69
%71 = getelementptr inbounds [100 x i32], [100 x i32]* %12, i64 0, i64 0
%72 = call i32 @read(i32 %70, i32* %71, i32 100)
%73 = load i32, i32* %7
%74 = add nsw i32 %73, 1
store i32 %74, i32* %7
%75 = load i32, i32* %7
%76 = call i32 @Write_number_to_7segdisplay(i32 %75)
%77 = load i32, i32* %6
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %62, %55
br label %33
80: ; preds = %49, %20
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str.3 = [29 x i8] c"/sys/class/gpio/gpio49/value\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [14 x i8] c" open failed\0A\00"
@POLLPRI = global i32 0
@SEEK_SET = global i32 0
@.str.6 = [36 x i8] c"\0Apoll() GPIO %d interrupt occurred\0A\00"
@str = [61 x i8] c"This program counts button press up to 9 on 7segment display\00"
@str.7 = [30 x i8] c"Waiting for the button press \00"
@str.8 = [16 x i8] c"\0Apoll() failed!\00"
@str.9 = [57 x i8] c"Error: Could not able to initialize the gpios..Exiting..\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x %struct.pollfd]
%4 = alloca [100 x i32]
%5 = bitcast [2 x %struct.pollfd]* %3 to i8*
%6 = bitcast [100 x i32]* %4 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
%8 = tail call i64 (...) @initialize_all_gpios()
%9 = icmp slt i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i64 (...) @initialize_button_gpio()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10, %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.9, i64 0, i64 0))
br label %58
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
%25 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%26 = call i32 @memset(i8* %5, i32 0, i32 24)
%27 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 0
store i32 %20, i32* %27
%28 = load i32, i32* @POLLPRI
%29 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 1
store i32 %28, i32* %29
%30 = call i32 @poll(%struct.pollfd* %25, i32 2, i32 3000)
%31 = icmp slt i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %24
%33 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 2
%34 = getelementptr inbounds [100 x i32], [100 x i32]* %4, i64 0, i64 0
br label %37
35: ; preds = %52, %24
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
br label %58
37: ; preds = %32, %52
%38 = phi i32 [ undef, %32 ], [ %53, %52 ]
%39 = load i32, i32* %33
%40 = load i32, i32* @POLLPRI
%41 = and i32 %40, %39
%42 = icmp eq i32 %41, 0
br i1 %42, label %52, label %43
43: ; preds = %37
%44 = load i32, i32* %27
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @lseek(i32 %44, i32 0, i32 %45)
%47 = load i32, i32* %27
%48 = call i32 @read(i32 %47, i32* %34, i32 100)
%49 = add nsw i32 %38, 1
%50 = call i32 @Write_number_to_7segdisplay(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 0)
br label %52
52: ; preds = %43, %37
%53 = phi i32 [ %49, %43 ], [ %38, %37 ]
%54 = call i32 @memset(i8* %5, i32 0, i32 24)
store i32 %20, i32* %27
%55 = load i32, i32* @POLLPRI
store i32 %55, i32* %29
%56 = call i32 @poll(%struct.pollfd* %25, i32 2, i32 3000)
%57 = icmp slt i32 %56, 0
br i1 %57, label %35, label %37
58: ; preds = %13, %35
%59 = phi i32 [ 0, %13 ], [ -1, %35 ]
ret i32 %59
}