#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 iio_event_data {
  int dummy;
};
typedef int /*<<< orphan*/ event;

/* Variables and functions */
int EAGAIN;
int ENOMEM;
int /*<<< orphan*/ IIO_GET_EVENT_FD_IOCTL;
int asprintf(char **, char *, int);
int /*<<< orphan*/ close(int);
int errno;
int find_type_by_name(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_event(struct iio_event_data *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, struct iio_event_data *, int);
int /*<<< orphan*/ stdout;
char *strdup(char const *);
int main(int argc, char **argv) {
  struct iio_event_data event;
  const char *device_name;
  char *chrdev_name;
  int ret;
  int dev_num;
  int fd, event_fd;
  if (argc <= 1) {
    printf("Usage: %s <device_name>\n", argv[0]);
    return -1;
  }
  device_name = argv[1];
  dev_num = find_type_by_name(device_name, "iio:device");
  if (dev_num >= 0) {
    printf("Found IIO device with name %s with device number %d\n", device_name,
           dev_num);
    ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
    if (ret < 0) {
      ret = -ENOMEM;
      goto error_ret;
    }
  } else {
    chrdev_name = strdup(device_name);
  }
  fd = open(chrdev_name, 0);
  if (fd == -1) {
    fprintf(stdout, "Failed to open %s\n", chrdev_name);
    ret = -errno;
    goto error_free_chrdev_name;
  }
  ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
  close(fd);
  if (ret == -1 || event_fd == -1) {
    fprintf(stdout, "Failed to retrieve event fd\n");
    ret = -errno;
    goto error_free_chrdev_name;
  }
  while (true) {
    ret = read(event_fd, &event, sizeof(event));
    if (ret == -1) {
      if (errno == EAGAIN) {
        printf("nothing available\n");
        continue;
      } else {
        perror("Failed to read event from device");
        ret = -errno;
        break;
      }
    }
    print_event(&event);
  }
  close(event_fd);
error_free_chrdev_name:
  free(chrdev_name);
error_ret:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     1201 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ba <main+0x1fa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   10e0 <find_type_by_name@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jl     1277 <main+0xb7>
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%edx
lea    -0x28(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   10a0 <asprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1272 <main+0xb2>
xor    %eax,%eax
mov    0x2d70(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    13b4 <main+0x1f4>
jmp    1284 <main+0xc4>
mov    -0x20(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12ca <main+0x10a>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %eax,%eax
mov    0x2d10(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    13ab <main+0x1eb>
mov    -0x34(%rbp),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
je     12fe <main+0x13e>
cmpl   $0xffffffff,-0x38(%rbp)
jne    1328 <main+0x168>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %eax,%eax
mov    0x2cb2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    13ab <main+0x1eb>
jmp    132d <main+0x16d>
mov    -0x38(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1395 <main+0x1d5>
mov    0x2c7e(%rip),%rax
mov    (%rax),%eax
mov    0x2c5d(%rip),%rcx
cmp    (%rcx),%eax
jne    1376 <main+0x1b6>
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132d <main+0x16d>
lea    0xd31(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
mov    0x2c45(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    13a3 <main+0x1e3>
lea    -0x18(%rbp),%rdi
call   1050 <print_event@plt>
jmp    132d <main+0x16d>
mov    -0x38(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jg     11fa <main+0x2a>
mov    (%rsi),%rsi
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    12e7 <main+0x117>
mov    0x8(%rsi),%r14
lea    0xe14(%rip),%rsi
mov    %r14,%rdi
call   10f0 <find_type_by_name@plt>
test   %eax,%eax
js     1248 <main+0x78>
mov    %eax,%ebx
lea    0xe0a(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe2c(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %ebx,%edx
call   10b0 <asprintf@plt>
test   %eax,%eax
js     12dc <main+0x10c>
mov    0x8(%rsp),%rdi
jmp    1258 <main+0x88>
mov    %r14,%rdi
call   10e0 <strdup@plt>
mov    %rax,%rdi
mov    %rax,0x8(%rsp)
xor    %esi,%esi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12ab <main+0xdb>
mov    %eax,%ebx
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
cmp    $0xffffffff,%ebp
je     1292 <main+0xc2>
mov    0x4(%rsp),%edi
cmp    $0xffffffff,%edi
jne    12f4 <main+0x124>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    12c7 <main+0xf7>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xdab(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    12e5 <main+0x115>
mov    0x2cf5(%rip),%rax
mov    (%rax),%ebx
neg    %ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rbx
mov    0x2cd0(%rip),%r15
mov    0x2cb1(%rip),%rbp
lea    0xdae(%rip),%r14
jmp    131c <main+0x14c>
mov    %rbx,%rdi
call   1060 <print_event@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    1310 <main+0x140>
mov    (%r15),%eax
cmp    0x0(%rbp),%eax
jne    1344 <main+0x174>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    0x4(%rsp),%edi
jmp    131c <main+0x14c>
lea    0xd50(%rip),%rdi
call   10d0 <perror@plt>
mov    (%r15),%ebx
mov    0x4(%rsp),%edi
call   1080 <close@plt>
jmp    12d0 <main+0x100>

LLVM IR (-O0):
%struct.iio_event_data = type { i32 }
@.str = [25 x i8] c"Usage: %s <device_name>\0A\00"
@.str.1 = [11 x i8] c"iio:device\00"
@.str.2 = [53 x i8] c"Found IIO device with name %s with device number %d\0A\00"
@.str.3 = [18 x i8] c"/dev/iio:device%d\00"
@ENOMEM = global i32 0
@stdout = global i32 0
@.str.4 = [19 x i8] c"Failed to open %s\0A\00"
@errno = global i32 0
@IIO_GET_EVENT_FD_IOCTL = global i32 0
@.str.5 = [29 x i8] c"Failed to retrieve event fd\0A\00"
@EAGAIN = global i32 0
@.str.6 = [19 x i8] c"nothing available\0A\00"
@.str.7 = [33 x i8] c"Failed to read event from device\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.iio_event_data
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp sle i32 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %96
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = call i32 @find_type_by_name(i8* %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp sge i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %20
%29 = load i8*, i8** %7
%30 = load i32, i32* %10
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %29, i32 %30)
%32 = load i32, i32* %10
%33 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = load i32, i32* @ENOMEM
%38 = sub nsw i32 0, %37
store i32 %38, i32* %9
br label %94
39: ; preds = %28
br label %43
40: ; preds = %20
%41 = load i8*, i8** %7
%42 = call i8* @strdup(i8* %41)
store i8* %42, i8** %8
br label %43
43: ; preds = %40, %39
%44 = load i8*, i8** %8
%45 = call i32 @open(i8* %44, i32 0)
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %54
48: ; preds = %43
%49 = load i32, i32* @stdout
%50 = load i8*, i8** %8
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %50)
%52 = load i32, i32* @errno
%53 = sub nsw i32 0, %52
store i32 %53, i32* %9
br label %91
54: ; preds = %43
%55 = load i32, i32* %11
%56 = load i32, i32* @IIO_GET_EVENT_FD_IOCTL
%57 = call i32 @ioctl(i32 %55, i32 %56, i32* %12)
store i32 %57, i32* %9
%58 = load i32, i32* %11
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %9
%61 = icmp eq i32 %60, -1
br i1 %61, label %65, label %62
62: ; preds = %54
%63 = load i32, i32* %12
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %70
65: ; preds = %62, %54
%66 = load i32, i32* @stdout
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%68 = load i32, i32* @errno
%69 = sub nsw i32 0, %68
store i32 %69, i32* %9
br label %91
70: ; preds = %62
br label %71
71: ; preds = %70, %80, %86
%72 = load i32, i32* %12
%73 = call i32 @read(i32 %72, %struct.iio_event_data* %6, i32 4)
store i32 %73, i32* %9
%74 = load i32, i32* %9
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %86
76: ; preds = %71
%77 = load i32, i32* @errno
%78 = load i32, i32* @EAGAIN
%79 = icmp eq i32 %77, %78
br i1 %79, label %80, label %82
80: ; preds = %76
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %71
82: ; preds = %76
%83 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
%84 = load i32, i32* @errno
%85 = sub nsw i32 0, %84
store i32 %85, i32* %9
br label %88
86: ; preds = %71
%87 = call i32 @print_event(%struct.iio_event_data* %6)
br label %71
88: ; preds = %82
%89 = load i32, i32* %12
%90 = call i32 @close(i32 %89)
br label %91
91: ; preds = %88, %65, %48
%92 = load i8*, i8** %8
%93 = call i32 @free(i8* %92)
br label %94
94: ; preds = %91, %36
%95 = load i32, i32* %9
store i32 %95, i32* %3
br label %96
96: ; preds = %94, %15
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
%struct.iio_event_data = type { i32 }
@.str = [25 x i8] c"Usage: %s <device_name>\0A\00"
@.str.1 = [11 x i8] c"iio:device\00"
@.str.2 = [53 x i8] c"Found IIO device with name %s with device number %d\0A\00"
@.str.3 = [18 x i8] c"/dev/iio:device%d\00"
@ENOMEM = global i32 0
@stdout = global i32 0
@.str.4 = [19 x i8] c"Failed to open %s\0A\00"
@errno = global i32 0
@IIO_GET_EVENT_FD_IOCTL = global i32 0
@.str.5 = [29 x i8] c"Failed to retrieve event fd\0A\00"
@EAGAIN = global i32 0
@.str.7 = [33 x i8] c"Failed to read event from device\00"
@str = [18 x i8] c"nothing available\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.iio_event_data
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast %struct.iio_event_data* %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %75
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @find_type_by_name(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %15, i32 %16)
%20 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%21 = icmp slt i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = load i8*, i8** %4
br label %28
24: ; preds = %18
%25 = load i32, i32* @ENOMEM
br label %72
26: ; preds = %13
%27 = tail call i8* @strdup(i8* %15)
store i8* %27, i8** %4
br label %28
28: ; preds = %22, %26
%29 = phi i8* [ %23, %22 ], [ %27, %26 ]
%30 = call i32 @open(i8* %29, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @stdout
%34 = load i8*, i8** %4
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @errno
br label %68
37: ; preds = %28
%38 = load i32, i32* @IIO_GET_EVENT_FD_IOCTL
%39 = call i32 @ioctl(i32 %30, i32 %38, i32* %5)
%40 = call i32 @close(i32 %30)
%41 = icmp eq i32 %39, -1
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, -1
%44 = select i1 %41, i1 true, i1 %43
br i1 %44, label %45, label %49
45: ; preds = %37
%46 = load i32, i32* @stdout
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* @errno
br label %68
49: ; preds = %37, %59
%50 = phi i32 [ %60, %59 ], [ %42, %37 ]
%51 = call i32 @read(i32 %50, %struct.iio_event_data* %3, i32 4)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %66
53: ; preds = %49
%54 = load i32, i32* @errno
%55 = load i32, i32* @EAGAIN
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %61
57: ; preds = %53
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %59
59: ; preds = %57, %66
%60 = load i32, i32* %5
br label %49
61: ; preds = %53
%62 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
%63 = load i32, i32* @errno
%64 = load i32, i32* %5
%65 = call i32 @close(i32 %64)
br label %68
66: ; preds = %49
%67 = call i32 @print_event(%struct.iio_event_data* %3)
br label %59
68: ; preds = %61, %45, %32
%69 = phi i32 [ %36, %32 ], [ %48, %45 ], [ %63, %61 ]
%70 = load i8*, i8** %4
%71 = call i32 @free(i8* %70)
br label %72
72: ; preds = %68, %24
%73 = phi i32 [ %25, %24 ], [ %69, %68 ]
%74 = sub nsw i32 0, %73
br label %75
75: ; preds = %72, %10
%76 = phi i32 [ -1, %10 ], [ %74, %72 ]
ret i32 %76
}