#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 void *uint32_t;
struct gpiohandle_data {
  unsigned int lineoffset;
  int fd;
  int *values;
  int /*<<< orphan*/ consumer_label;
  void *eventflags;
  void *handleflags;
};
struct gpioevent_request {
  unsigned int lineoffset;
  int fd;
  int *values;
  int /*<<< orphan*/ consumer_label;
  void *eventflags;
  void *handleflags;
};
struct gpioevent_data {
  int id;
  int /*<<< orphan*/ timestamp;
};
typedef int /*<<< orphan*/ event;

/* Variables and functions */
int EAGAIN;
int EIO;
int ENOMEM;
#define GPIOEVENT_EVENT_FALLING_EDGE 129
#define GPIOEVENT_EVENT_RISING_EDGE 128
int /*<<< orphan*/ GPIOHANDLE_GET_LINE_VALUES_IOCTL;
int /*<<< orphan*/ GPIO_GET_LINEEVENT_IOCTL;
int asprintf(char **, char *, char const *);
int close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int ioctl(int, int /*<<< orphan*/, struct gpiohandle_data *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, struct gpioevent_data *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int monitor_device(const char *device_name, unsigned int line,
                   uint32_t handleflags, uint32_t eventflags,
                   unsigned int loops) {
  struct gpioevent_request req;
  struct gpiohandle_data data;
  char *chrdev_name;
  int fd;
  int ret;
  int i = 0;
  ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  if (ret < 0)
    return -ENOMEM;
  fd = open(chrdev_name, 0);
  if (fd == -1) {
    ret = -errno;
    fprintf(stderr, "Failed to open %s\n", chrdev_name);
    goto exit_close_error;
  }
  req.lineoffset = line;
  req.handleflags = handleflags;
  req.eventflags = eventflags;
  strcpy(req.consumer_label, "gpio-event-mon");
  ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
  if (ret == -1) {
    ret = -errno;
    fprintf(stderr,
            "Failed to issue GET EVENT "
            "IOCTL (%d)\n",
            ret);
    goto exit_close_error;
  }
  ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
  if (ret == -1) {
    ret = -errno;
    fprintf(stderr,
            "Failed to issue GPIOHANDLE GET LINE "
            "VALUES IOCTL (%d)\n",
            ret);
    goto exit_close_error;
  }
  fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
  fprintf(stdout, "Initial line value: %d\n", data.values[0]);
  while (1) {
    struct gpioevent_data event;
    ret = read(req.fd, &event, sizeof(event));
    if (ret == -1) {
      if (errno == -EAGAIN) {
        fprintf(stderr, "nothing available\n");
        continue;
      } else {
        ret = -errno;
        fprintf(stderr, "Failed to read event (%d)\n", ret);
        break;
      }
    }
    if (ret != sizeof(event)) {
      fprintf(stderr, "Reading event failed\n");
      ret = -EIO;
      break;
    }
    fprintf(stdout, "GPIO EVENT %llu: ", event.timestamp);
    switch (event.id) {
    case GPIOEVENT_EVENT_RISING_EDGE:
      fprintf(stdout, "rising edge");
      break;
    case GPIOEVENT_EVENT_FALLING_EDGE:
      fprintf(stdout, "falling edge");
      break;
    default:
      fprintf(stdout, "unknown event");
    }
    fprintf(stdout, "\n");
    i++;
    if (i == loops)
      break;
  }
exit_close_error:
  if (close(fd) == -1)
    perror("Failed to close GPIO character device file");
  free(chrdev_name);
  return ret;
}


x86 assembly (clang, -O0):
<monitor_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
movl   $0x0,-0x94(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x88(%rbp),%rdi
lea    0xe36(%rip),%rsi
call   1090 <asprintf@plt>
mov    %eax,-0x90(%rbp)
cmpl   $0x0,-0x90(%rbp)
jge    11f5 <monitor_device+0x65>
xor    %eax,%eax
mov    0x2de5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    154a <monitor_device+0x3ba>
mov    -0x88(%rbp),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    %eax,-0x8c(%rbp)
cmpl   $0xffffffff,-0x8c(%rbp)
jne    124a <monitor_device+0xba>
xor    %eax,%eax
mov    0x2da1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x90(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x88(%rbp),%rdx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1515 <monitor_device+0x385>
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%edi
lea    0xdb1(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x8c(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x90(%rbp)
cmpl   $0xffffffff,-0x90(%rbp)
jne    12cd <monitor_device+0x13d>
xor    %eax,%eax
mov    0x2d1d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x90(%rbp)
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    -0x90(%rbp),%edx
lea    0xd69(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1515 <monitor_device+0x385>
mov    -0x54(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x90(%rbp)
cmpl   $0xffffffff,-0x90(%rbp)
jne    1328 <monitor_device+0x198>
xor    %eax,%eax
mov    0x2cc2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x90(%rbp)
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
mov    -0x90(%rbp),%edx
lea    0xd34(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1515 <monitor_device+0x385>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xd48(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c63(%rip),%rax
mov    (%rax),%edi
mov    -0x78(%rbp),%rax
mov    (%rax),%edx
lea    0xd45(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x54(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x8,%edx
call   1070 <read@plt>
mov    %eax,-0x90(%rbp)
cmpl   $0xffffffff,-0x90(%rbp)
jne    13f5 <monitor_device+0x265>
mov    0x2c2f(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2c04(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    13c2 <monitor_device+0x232>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
lea    0xd03(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1363 <monitor_device+0x1d3>
xor    %eax,%eax
mov    0x2bf5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x90(%rbp)
mov    0x2bee(%rip),%rax
mov    (%rax),%edi
mov    -0x90(%rbp),%edx
lea    0xce3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1510 <monitor_device+0x380>
movslq -0x90(%rbp),%rax
cmp    $0x8,%rax
je     1433 <monitor_device+0x2a3>
mov    0x2bbb(%rip),%rax
mov    (%rax),%edi
lea    0xcd1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    0x2bb2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x90(%rbp)
jmp    1510 <monitor_device+0x380>
mov    0x2b76(%rip),%rax
mov    (%rax),%edi
mov    -0x9c(%rbp),%edx
lea    0xcb4(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    -0xa0(%rbp),%eax
mov    %eax,-0xa4(%rbp)
sub    $0x80,%eax
je     1482 <monitor_device+0x2f2>
jmp    146c <monitor_device+0x2dc>
mov    -0xa4(%rbp),%eax
sub    $0x81,%eax
je     149e <monitor_device+0x30e>
jmp    14ba <monitor_device+0x32a>
mov    0x2b27(%rip),%rax
mov    (%rax),%edi
lea    0xc7d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    14d1 <monitor_device+0x341>
mov    0x2b0b(%rip),%rax
mov    (%rax),%edi
lea    0xc6d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    14d1 <monitor_device+0x341>
mov    0x2aef(%rip),%rax
mov    (%rax),%edi
lea    0xc5e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2ad8(%rip),%rax
mov    (%rax),%edi
lea    0xba4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    150b <monitor_device+0x37b>
jmp    1510 <monitor_device+0x380>
jmp    1363 <monitor_device+0x1d3>
jmp    1515 <monitor_device+0x385>
mov    -0x8c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1535 <monitor_device+0x3a5>
lea    0xc06(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
mov    -0x90(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monitor_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %r8d,%r15d
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %rdi,%r13
lea    0xe4c(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r13,%rdx
call   1090 <asprintf@plt>
test   %eax,%eax
js     1345 <monitor_device+0x1b5>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1355 <monitor_device+0x1c5>
mov    %r12d,0x18(%rsp)
mov    %rbx,0x38(%rsp)
mov    %rbp,0x30(%rsp)
lea    0xe24(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %r14d,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1381 <monitor_device+0x1f1>
mov    0x1c(%rsp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    0x40(%rsp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     139e <monitor_device+0x20e>
mov    %r14d,0x4(%rsp)
mov    0x2d69(%rip),%r14
mov    (%r14),%edi
lea    0xe36(%rip),%rsi
mov    %r12d,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r14),%edi
mov    0x48(%rsp),%rax
mov    (%rax),%edx
lea    0xe32(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0x10(%rsp),%rbp
mov    0x2d1e(%rip),%r13
xor    %r12d,%r12d
jmp    12f8 <monitor_device+0x168>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    0x14(%rsp),%edx
lea    0xe5f(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x10(%rsp),%eax
cmp    $0x81,%eax
lea    0xe73(%rip),%rsi
lea    0xe5f(%rip),%rcx
cmove  %rcx,%rsi
cmp    $0x80,%eax
lea    0xe43(%rip),%rax
cmove  %rax,%rsi
mov    (%r14),%edi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r14),%edi
lea    0xda1(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%r12d
cmp    %r15d,%r12d
je     13e8 <monitor_device+0x258>
mov    0x1c(%rsp),%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1070 <read@plt>
cmp    $0x8,%eax
je     1290 <monitor_device+0x100>
cmp    $0xffffffff,%eax
jne    13c4 <monitor_device+0x234>
mov    0x2ca2(%rip),%rax
mov    (%rax),%ebx
mov    0x0(%r13),%eax
add    %ebx,%eax
jne    13ef <monitor_device+0x25f>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12f8 <monitor_device+0x168>
mov    0x2c84(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    1426 <monitor_device+0x296>
mov    0x2c64(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xc93(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    140f <monitor_device+0x27f>
mov    0x2c38(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    0x2c35(%rip),%rax
mov    (%rax),%edi
lea    0xc8e(%rip),%rsi
jmp    13b9 <monitor_device+0x229>
mov    0x2c1b(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    0x2c18(%rip),%rax
mov    (%rax),%edi
lea    0xc97(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    140f <monitor_device+0x27f>
mov    0x2bfd(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2bf4(%rip),%rax
sub    (%rax),%ebx
jmp    140a <monitor_device+0x27a>
mov    $0x8,%ebx
jmp    140a <monitor_device+0x27a>
neg    %ebx
mov    0x2bd0(%rip),%rax
mov    (%rax),%edi
lea    0xccb(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x4(%rsp),%r14d
mov    %r14d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     1437 <monitor_device+0x2a7>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf8(%rip),%rdi
call   10b0 <perror@plt>
jmp    141c <monitor_device+0x28c>

LLVM IR (-O0):
%struct.gpioevent_request = type { i32, i32, i32*, i32, i8*, i8* }
%struct.gpiohandle_data = type { i32, i32, i32*, i32, i8*, i8* }
%struct.gpioevent_data = type { i32, i32 }
@.str = [8 x i8] c"/dev/%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"Failed to open %s\0A\00"
@.str.2 = [15 x i8] c"gpio-event-mon\00"
@GPIO_GET_LINEEVENT_IOCTL = global i32 0
@.str.3 = [38 x i8] c"Failed to issue GET EVENT IOCTL (%d)\0A\00"
@GPIOHANDLE_GET_LINE_VALUES_IOCTL = global i32 0
@.str.4 = [55 x i8] c"Failed to issue GPIOHANDLE GET LINE VALUES IOCTL (%d)\0A\00"
@stdout = global i32 0
@.str.5 = [26 x i8] c"Monitoring line %d on %s\0A\00"
@.str.6 = [24 x i8] c"Initial line value: %d\0A\00"
@EAGAIN = global i32 0
@.str.7 = [19 x i8] c"nothing available\0A\00"
@.str.8 = [27 x i8] c"Failed to read event (%d)\0A\00"
@.str.9 = [22 x i8] c"Reading event failed\0A\00"
@EIO = global i32 0
@.str.10 = [18 x i8] c"GPIO EVENT %llu: \00"
@.str.11 = [12 x i8] c"rising edge\00"
@.str.12 = [13 x i8] c"falling edge\00"
@.str.13 = [14 x i8] c"unknown event\00"
@.str.14 = [2 x i8] c"\0A\00"
@.str.15 = [43 x i8] c"Failed to close GPIO character device file\00"
define i32 @monitor_device(i8* %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.gpioevent_request
%13 = alloca %struct.gpiohandle_data
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca %struct.gpioevent_data
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 0, i32* %17
%19 = load i8*, i8** %7
%20 = call i32 @asprintf(i8** %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 %20, i32* %16
%21 = load i32, i32* %16
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %5
%24 = load i32, i32* @ENOMEM
%25 = sub nsw i32 0, %24
store i32 %25, i32* %6
br label %149
26: ; preds = %5
%27 = load i8*, i8** %14
%28 = call i32 @open(i8* %27, i32 0)
store i32 %28, i32* %15
%29 = load i32, i32* %15
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
store i32 %33, i32* %16
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %14
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %139
37: ; preds = %26
%38 = load i32, i32* %8
%39 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %12, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i8*, i8** %9
%41 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %12, i32 0, i32 5
store i8* %40, i8** %41
%42 = load i8*, i8** %10
%43 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %12, i32 0, i32 4
store i8* %42, i8** %43
%44 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %12, i32 0, i32 3
%45 = load i32, i32* %44
%46 = call i32 @strcpy(i32 %45, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %15
%48 = load i32, i32* @GPIO_GET_LINEEVENT_IOCTL
%49 = bitcast %struct.gpioevent_request* %12 to %struct.gpiohandle_data*
%50 = call i32 @ioctl(i32 %47, i32 %48, %struct.gpiohandle_data* %49)
store i32 %50, i32* %16
%51 = load i32, i32* %16
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %59
53: ; preds = %37
%54 = load i32, i32* @errno
%55 = sub nsw i32 0, %54
store i32 %55, i32* %16
%56 = load i32, i32* @stderr
%57 = load i32, i32* %16
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %139
59: ; preds = %37
%60 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %12, i32 0, i32 1
%61 = load i32, i32* %60
%62 = load i32, i32* @GPIOHANDLE_GET_LINE_VALUES_IOCTL
%63 = call i32 @ioctl(i32 %61, i32 %62, %struct.gpiohandle_data* %13)
store i32 %63, i32* %16
%64 = load i32, i32* %16
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %72
66: ; preds = %59
%67 = load i32, i32* @errno
%68 = sub nsw i32 0, %67
store i32 %68, i32* %16
%69 = load i32, i32* @stderr
%70 = load i32, i32* %16
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 %70)
br label %139
72: ; preds = %59
%73 = load i32, i32* @stdout
%74 = load i32, i32* %8
%75 = load i8*, i8** %7
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %74, i8* %75)
%77 = load i32, i32* @stdout
%78 = getelementptr inbounds %struct.gpiohandle_data, %struct.gpiohandle_data* %13, i32 0, i32 2
%79 = load i32*, i32** %78
%80 = getelementptr inbounds i32, i32* %79, i64 0
%81 = load i32, i32* %80
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %81)
br label %83
83: ; preds = %72, %94, %137
%84 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %12, i32 0, i32 1
%85 = load i32, i32* %84
%86 = call i32 @read(i32 %85, %struct.gpioevent_data* %18, i32 8)
store i32 %86, i32* %16
%87 = load i32, i32* %16
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %103
89: ; preds = %83
%90 = load i32, i32* @errno
%91 = load i32, i32* @EAGAIN
%92 = sub nsw i32 0, %91
%93 = icmp eq i32 %90, %92
br i1 %93, label %94, label %97
94: ; preds = %89
%95 = load i32, i32* @stderr
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %83
97: ; preds = %89
%98 = load i32, i32* @errno
%99 = sub nsw i32 0, %98
store i32 %99, i32* %16
%100 = load i32, i32* @stderr
%101 = load i32, i32* %16
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i32 %101)
br label %138
103: ; preds = %83
%104 = load i32, i32* %16
%105 = sext i32 %104 to i64
%106 = icmp ne i64 %105, 8
br i1 %106, label %107, label %112
107: ; preds = %103
%108 = load i32, i32* @stderr
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
%110 = load i32, i32* @EIO
%111 = sub nsw i32 0, %110
store i32 %111, i32* %16
br label %138
112: ; preds = %103
%113 = load i32, i32* @stdout
%114 = getelementptr inbounds %struct.gpioevent_data, %struct.gpioevent_data* %18, i32 0, i32 1
%115 = load i32, i32* %114
%116 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i32 %115)
%117 = getelementptr inbounds %struct.gpioevent_data, %struct.gpioevent_data* %18, i32 0, i32 0
%118 = load i32, i32* %117
switch i32 %118, label %125 [
i32 128, label %119
i32 129, label %122
]
119: ; preds = %112
%120 = load i32, i32* @stdout
%121 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
br label %128
122: ; preds = %112
%123 = load i32, i32* @stdout
%124 = call i32 (i32, i8*, ...) @fprintf(i32 %123, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0))
br label %128
125: ; preds = %112
%126 = load i32, i32* @stdout
%127 = call i32 (i32, i8*, ...) @fprintf(i32 %126, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0))
br label %128
128: ; preds = %125, %122, %119
%129 = load i32, i32* @stdout
%130 = call i32 (i32, i8*, ...) @fprintf(i32 %129, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.14, i64 0, i64 0))
%131 = load i32, i32* %17
%132 = add nsw i32 %131, 1
store i32 %132, i32* %17
%133 = load i32, i32* %17
%134 = load i32, i32* %11
%135 = icmp eq i32 %133, %134
br i1 %135, label %136, label %137
136: ; preds = %128
br label %138
137: ; preds = %128
br label %83
138: ; preds = %136, %107, %97
br label %139
139: ; preds = %138, %66, %53, %31
%140 = load i32, i32* %15
%141 = call i32 @close(i32 %140)
%142 = icmp eq i32 %141, -1
br i1 %142, label %143, label %145
143: ; preds = %139
%144 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.15, i64 0, i64 0))
br label %145
145: ; preds = %143, %139
%146 = load i8*, i8** %14
%147 = call i32 @free(i8* %146)
%148 = load i32, i32* %16
store i32 %148, i32* %6
br label %149
149: ; preds = %145, %23
%150 = load i32, i32* %6
ret i32 %150
}

LLVM IR (-O2):
%struct.gpioevent_request = type { i32, i32, i32*, i32, i8*, i8* }
%struct.gpiohandle_data = type { i32, i32, i32*, i32, i8*, i8* }
%struct.gpioevent_data = type { i32, i32 }
@.str = [8 x i8] c"/dev/%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"Failed to open %s\0A\00"
@.str.2 = [15 x i8] c"gpio-event-mon\00"
@GPIO_GET_LINEEVENT_IOCTL = global i32 0
@.str.3 = [38 x i8] c"Failed to issue GET EVENT IOCTL (%d)\0A\00"
@GPIOHANDLE_GET_LINE_VALUES_IOCTL = global i32 0
@.str.4 = [55 x i8] c"Failed to issue GPIOHANDLE GET LINE VALUES IOCTL (%d)\0A\00"
@stdout = global i32 0
@.str.5 = [26 x i8] c"Monitoring line %d on %s\0A\00"
@.str.6 = [24 x i8] c"Initial line value: %d\0A\00"
@EAGAIN = global i32 0
@.str.7 = [19 x i8] c"nothing available\0A\00"
@.str.8 = [27 x i8] c"Failed to read event (%d)\0A\00"
@.str.9 = [22 x i8] c"Reading event failed\0A\00"
@EIO = global i32 0
@.str.10 = [18 x i8] c"GPIO EVENT %llu: \00"
@.str.11 = [12 x i8] c"rising edge\00"
@.str.12 = [13 x i8] c"falling edge\00"
@.str.13 = [14 x i8] c"unknown event\00"
@.str.14 = [2 x i8] c"\0A\00"
@.str.15 = [43 x i8] c"Failed to close GPIO character device file\00"
define i32 @monitor_device(i8* %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca %struct.gpioevent_request
%7 = alloca %struct.gpiohandle_data
%8 = alloca i8*
%9 = alloca %struct.gpioevent_data
%10 = bitcast %struct.gpioevent_request* %6 to i8*
%11 = bitcast %struct.gpiohandle_data* %7 to i8*
%12 = bitcast i8** %8 to i8*
%13 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = load i32, i32* @ENOMEM
%17 = sub nsw i32 0, %16
br label %114
18: ; preds = %5
%19 = load i8*, i8** %8
%20 = call i32 @open(i8* %19, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %8
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %105
28: ; preds = %18
%29 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %6, i64 0, i32 0
store i32 %1, i32* %29
%30 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %6, i64 0, i32 5
store i8* %2, i8** %30
%31 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %6, i64 0, i32 4
store i8* %3, i8** %31
%32 = call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @GPIO_GET_LINEEVENT_IOCTL
%34 = bitcast %struct.gpioevent_request* %6 to %struct.gpiohandle_data*
%35 = call i32 @ioctl(i32 %20, i32 %33, %struct.gpiohandle_data* %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %28
%38 = load i32, i32* @errno
%39 = sub nsw i32 0, %38
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %105
42: ; preds = %28
%43 = getelementptr inbounds %struct.gpioevent_request, %struct.gpioevent_request* %6, i64 0, i32 1
%44 = load i32, i32* %43
%45 = load i32, i32* @GPIOHANDLE_GET_LINE_VALUES_IOCTL
%46 = call i32 @ioctl(i32 %44, i32 %45, %struct.gpiohandle_data* %7)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 %50)
br label %105
53: ; preds = %42
%54 = load i32, i32* @stdout
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %1, i8* %0)
%56 = load i32, i32* @stdout
%57 = getelementptr inbounds %struct.gpiohandle_data, %struct.gpiohandle_data* %7, i64 0, i32 2
%58 = load i32*, i32** %57
%59 = load i32, i32* %58
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %59)
%61 = bitcast %struct.gpioevent_data* %9 to i8*
%62 = getelementptr inbounds %struct.gpioevent_data, %struct.gpioevent_data* %9, i64 0, i32 1
%63 = getelementptr inbounds %struct.gpioevent_data, %struct.gpioevent_data* %9, i64 0, i32 0
br label %64
64: ; preds = %102, %53
%65 = phi i32 [ 0, %53 ], [ %103, %102 ]
%66 = load i32, i32* %43
%67 = call i32 @read(i32 %66, %struct.gpioevent_data* %9, i32 8)
switch i32 %67, label %80 [
i32 -1, label %68
i32 8, label %85
]
68: ; preds = %64
%69 = load i32, i32* @errno
%70 = load i32, i32* @EAGAIN
%71 = sub nsw i32 0, %70
%72 = icmp eq i32 %69, %71
br i1 %72, label %73, label %76
73: ; preds = %68
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %102
76: ; preds = %68
%77 = sub nsw i32 0, %69
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i32 %77)
br label %100
80: ; preds = %64
%81 = load i32, i32* @stderr
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
%83 = load i32, i32* @EIO
%84 = sub nsw i32 0, %83
br label %100
85: ; preds = %64
%86 = load i32, i32* @stdout
%87 = load i32, i32* %62
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i32 %87)
%89 = load i32, i32* %63
%90 = icmp eq i32 %89, 129
%91 = select i1 %90, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0)
%92 = icmp eq i32 %89, 128
%93 = select i1 %92, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0), i8* %91
%94 = load i32, i32* @stdout
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* %93)
%96 = load i32, i32* @stdout
%97 = call i32 (i32, i8*, ...) @fprintf(i32 %96, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.14, i64 0, i64 0))
%98 = add nsw i32 %65, 1
%99 = icmp eq i32 %98, %4
br i1 %99, label %104, label %102
100: ; preds = %76, %80
%101 = phi i32 [ %84, %80 ], [ %77, %76 ]
br label %105
102: ; preds = %85, %73
%103 = phi i32 [ %65, %73 ], [ %98, %85 ]
br label %64
104: ; preds = %85
br label %105
105: ; preds = %104, %100, %48, %37, %22
%106 = phi i32 [ %24, %22 ], [ %39, %37 ], [ %50, %48 ], [ %101, %100 ], [ 8, %104 ]
%107 = call i32 @close(i32 %20)
%108 = icmp eq i32 %107, -1
br i1 %108, label %109, label %111
109: ; preds = %105
%110 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.15, i64 0, i64 0))
br label %111
111: ; preds = %109, %105
%112 = load i8*, i8** %8
%113 = call i32 @free(i8* %112)
br label %114
114: ; preds = %111, %15
%115 = phi i32 [ %17, %15 ], [ %106, %111 ]
ret i32 %115
}