#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 */
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef int /*<<< orphan*/ pinstate;
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int POLLPRI;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ read(int, char *, int);
int snprintf(char *, int, char *, int);
int gpio_poll(int gpio, char targetstate, int timeout) {
  int ret;
  char filename[128];
  char pinstate;
  ret = snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value",
                 gpio);
  if (ret < 0)
    goto out;
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    ret = -errno;
    perror("GPIO "
           "poll_open");
    goto out;
  }
  read(fd, &pinstate, sizeof(pinstate));
  if (pinstate == targetstate) {
    ret = pinstate;
    goto out_close;
  }
  struct pollfd pollfd = {
      .fd = fd,
      .events = POLLPRI,
      .revents = 0,
  };
  ret = poll(&pollfd, 1, timeout);
  if (ret <= 0) {
    ret = -1;
    goto out_close;
  }
  if (pollfd.revents & POLLPRI) {
    read(fd, &pinstate, sizeof(pinstate));
    ret = pinstate;
  } else {
    ret = -1;
  }
out_close:
  close(fd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<gpio_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x80,%esi
lea    0xe73(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11a4 <gpio_poll+0x44>
jmp    12cb <gpio_poll+0x16b>
lea    -0x90(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0x0,-0x98(%rbp)
jge    11eb <gpio_poll+0x8b>
xor    %eax,%eax
mov    0x2dfb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x10(%rbp)
lea    0xe3c(%rip),%rdi
call   1080 <perror@plt>
jmp    12cb <gpio_poll+0x16b>
mov    -0x98(%rbp),%edi
lea    -0x91(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0x91(%rbp),%eax
movsbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
jne    1224 <gpio_poll+0xc4>
movsbl -0x91(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    12c0 <gpio_poll+0x160>
mov    -0x98(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa4(%rbp)
movl   $0x0,-0xa0(%rbp)
mov    -0xc(%rbp),%edx
lea    -0xa8(%rbp),%rdi
mov    $0x1,%esi
call   1060 <poll@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     1276 <gpio_poll+0x116>
movl   $0xffffffff,-0x10(%rbp)
jmp    12c0 <gpio_poll+0x160>
mov    -0xa0(%rbp),%eax
mov    0x2d6d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12b4 <gpio_poll+0x154>
mov    -0x98(%rbp),%edi
lea    -0x91(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0x91(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    12bb <gpio_poll+0x15b>
movl   $0xffffffff,-0x10(%rbp)
jmp    12c0 <gpio_poll+0x160>
mov    -0x98(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_poll>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ecx
lea    0xe84(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x80,%esi
call   1030 <snprintf@plt>
test   %eax,%eax
js     11ca <gpio_poll+0x6a>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1239 <gpio_poll+0xd9>
mov    %eax,%ebx
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    %r15b,0xf(%rsp)
jne    11ce <gpio_poll+0x6e>
movsbl %r15b,%ebp
jmp    1222 <gpio_poll+0xc2>
mov    %eax,%ebp
jmp    1229 <gpio_poll+0xc9>
mov    %ebx,0x10(%rsp)
mov    0x2e17(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
movl   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1060 <poll@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jle    1222 <gpio_poll+0xc2>
mov    (%r15),%eax
test   %eax,0x18(%rsp)
je     1222 <gpio_poll+0xc2>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl 0xf(%rsp),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d90(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
jmp    1229 <gpio_poll+0xc9>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [15 x i8] c"GPIO poll_open\00"
@POLLPRI = global i32 0
define i32 @gpio_poll(i32 %0, i8 signext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
%9 = alloca i8
%10 = alloca i32
%11 = alloca %struct.pollfd
store i32 %0, i32* %4
store i8 %1, i8* %5
store i32 %2, i32* %6
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* %4
%14 = call i32 @snprintf(i8* %12, i32 128, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
br label %66
18: ; preds = %3
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
store i32 %26, i32* %7
%27 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %66
28: ; preds = %18
%29 = load i32, i32* %10
%30 = call i32 @read(i32 %29, i8* %9, i32 1)
%31 = load i8, i8* %9
%32 = sext i8 %31 to i32
%33 = load i8, i8* %5
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %32, %34
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = load i8, i8* %9
%38 = sext i8 %37 to i32
store i32 %38, i32* %7
br label %63
39: ; preds = %28
%40 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
%41 = load i32, i32* %10
store i32 %41, i32* %40
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 1
%43 = load i32, i32* @POLLPRI
store i32 %43, i32* %42
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 2
store i32 0, i32* %44
%45 = load i32, i32* %6
%46 = call i32 @poll(%struct.pollfd* %11, i32 1, i32 %45)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp sle i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %39
store i32 -1, i32* %7
br label %63
50: ; preds = %39
%51 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 2
%52 = load i32, i32* %51
%53 = load i32, i32* @POLLPRI
%54 = and i32 %52, %53
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %50
%57 = load i32, i32* %10
%58 = call i32 @read(i32 %57, i8* %9, i32 1)
%59 = load i8, i8* %9
%60 = sext i8 %59 to i32
store i32 %60, i32* %7
br label %62
61: ; preds = %50
store i32 -1, i32* %7
br label %62
62: ; preds = %61, %56
br label %63
63: ; preds = %62, %49, %36
%64 = load i32, i32* %10
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %63, %24, %17
%67 = load i32, i32* %7
ret i32 %67
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [15 x i8] c"GPIO poll_open\00"
@POLLPRI = global i32 0
define i32 @gpio_poll(i32 %0, i8 signext %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = alloca i8
%6 = alloca %struct.pollfd
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %43, label %10
10: ; preds = %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %7, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %43
18: ; preds = %10
%19 = call i32 @read(i32 %12, i8* %5, i32 1)
%20 = load i8, i8* %5
%21 = icmp eq i8 %20, %1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = sext i8 %1 to i32
br label %40
24: ; preds = %18
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 0
store i32 %12, i32* %25
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 1
%27 = load i32, i32* @POLLPRI
store i32 %27, i32* %26
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 2
store i32 0, i32* %28
%29 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 %2)
%30 = icmp slt i32 %29, 1
br i1 %30, label %40, label %31
31: ; preds = %24
%32 = load i32, i32* %28
%33 = load i32, i32* @POLLPRI
%34 = and i32 %33, %32
%35 = icmp eq i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %31
%37 = call i32 @read(i32 %12, i8* %5, i32 1)
%38 = load i8, i8* %5
%39 = sext i8 %38 to i32
br label %40
40: ; preds = %31, %24, %36, %22
%41 = phi i32 [ %23, %22 ], [ %39, %36 ], [ -1, %24 ], [ -1, %31 ]
%42 = call i32 @close(i32 %12)
br label %43
43: ; preds = %3, %40, %14
%44 = phi i32 [ %8, %3 ], [ %16, %14 ], [ %41, %40 ]
ret i32 %44
}