#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 EIO;
int ENODEV;
int ENOMEM;
int /*<<< orphan*/ IIO_GET_EVENT_FD_IOCTL;
int asprintf(char **, char *, int);
int 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 *, char const *, int);
int read(int, struct iio_event_data *, int);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "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) {
      return -ENOMEM;
    }
  } else {
    chrdev_name = strdup(device_name);
    if (!chrdev_name) {
      return -ENOMEM;
    }
  }
  fd = open(chrdev_name, 0);
  if (fd == -1) {
    ret = -errno;
    fprintf(stderr, "Failed to open %s\n", chrdev_name);
    goto error_free_chrdev_name;
  }
  ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
  if (ret == -1 || event_fd == -1) {
    ret = -errno;
    if (ret == -ENODEV)
      fprintf(stderr, "This device does not support events\n");
    else {
      fprintf(stderr, "Failed to retrieve event fd\n");
    }
    if (close(fd) == -1) {
      perror("Failed to close character device file");
    }
    goto error_free_chrdev_name;
  }
  if (close(fd) == -1) {
    ret = -errno;
    goto error_free_chrdev_name;
  }
  while (true) {
    ret = read(event_fd, &event, sizeof(event));
    if (ret == -1) {
      if (errno == EAGAIN) {
        fprintf(stderr, "nothing available\n");
        continue;
      } else {
        ret = -errno;
        perror("Failed to read event from device");
        break;
      }
    }
    if (ret != sizeof(event)) {
      fprintf(stderr, "Reading event failed!\n");
      ret = -EIO;
      break;
    }
    print_event(&event);
  }
  if (close(event_fd) == -1) {
    perror("Failed to close event file");
  }
error_free_chrdev_name:
  free(chrdev_name);
  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     120a <main+0x4a>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14aa <main+0x2ea>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdf8(%rip),%rsi
call   10e0 <find_type_by_name@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jl     127e <main+0xbe>
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%edx
lea    -0x28(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   10a0 <asprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1279 <main+0xb9>
xor    %eax,%eax
mov    0x2d61(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    14aa <main+0x2ea>
jmp    12ae <main+0xee>
mov    -0x20(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a9 <main+0xe9>
xor    %eax,%eax
mov    0x2d31(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    14aa <main+0x2ea>
jmp    12ae <main+0xee>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12f4 <main+0x134>
xor    %eax,%eax
mov    0x2cf1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    149b <main+0x2db>
mov    -0x34(%rbp),%edi
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     1320 <main+0x160>
cmpl   $0xffffffff,-0x38(%rbp)
jne    1399 <main+0x1d9>
xor    %eax,%eax
mov    0x2c97(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
xor    %ecx,%ecx
mov    0x2c7e(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    1360 <main+0x1a0>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1377 <main+0x1b7>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    1394 <main+0x1d4>
lea    0xd31(%rip),%rdi
call   10c0 <perror@plt>
jmp    149b <main+0x2db>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    13bd <main+0x1fd>
xor    %eax,%eax
mov    0x2c0d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    149b <main+0x2db>
jmp    13c2 <main+0x202>
mov    -0x38(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1433 <main+0x273>
mov    0x2bd9(%rip),%rax
mov    (%rax),%eax
mov    0x2bb8(%rip),%rcx
cmp    (%rcx),%eax
jne    1414 <main+0x254>
mov    0x2bc9(%rip),%rax
mov    (%rax),%edi
lea    0xcde(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    13c2 <main+0x202>
xor    %eax,%eax
mov    0x2ba3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
lea    0xcd0(%rip),%rdi
call   10c0 <perror@plt>
jmp    1479 <main+0x2b9>
movslq -0x2c(%rbp),%rax
cmp    $0x4,%rax
je     146b <main+0x2ab>
mov    0x2b80(%rip),%rax
mov    (%rax),%edi
lea    0xcc9(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %eax,%eax
mov    0x2b77(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    1479 <main+0x2b9>
lea    -0x18(%rbp),%rdi
call   1050 <print_event@plt>
jmp    13c2 <main+0x202>
mov    -0x38(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    1496 <main+0x2d6>
lea    0xca0(%rip),%rdi
call   10c0 <perror@plt>
jmp    149b <main+0x2db>
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   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jg     11f7 <main+0x37>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    13e3 <main+0x223>
mov    0x8(%rsi),%rbx
lea    0xe17(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <find_type_by_name@plt>
test   %eax,%eax
js     1243 <main+0x83>
mov    %eax,%ebp
lea    0xe0d(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
lea    0xe31(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %ebp,%edx
call   10a0 <asprintf@plt>
test   %eax,%eax
js     1314 <main+0x154>
mov    0x8(%rsp),%rdi
jmp    125c <main+0x9c>
mov    %rbx,%rdi
call   10d0 <strdup@plt>
mov    %rax,%rdi
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1314 <main+0x154>
xor    %esi,%esi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1324 <main+0x164>
mov    %eax,%ebp
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     134e <main+0x18e>
cmpl   $0xffffffff,0x4(%rsp)
je     134e <main+0x18e>
mov    %ebp,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     13ce <main+0x20e>
lea    0x10(%rsp),%rbp
mov    0x2d0d(%rip),%r15
mov    0x2cee(%rip),%r13
mov    0x2d07(%rip),%r12
lea    0xe1e(%rip),%r14
jmp    12d8 <main+0x118>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1050 <print_event@plt>
mov    0x4(%rsp),%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
je     12d0 <main+0x110>
cmp    $0xffffffff,%eax
jne    1397 <main+0x1d7>
mov    (%r15),%ebx
cmp    0x0(%r13),%ebx
jne    13f4 <main+0x234>
mov    (%r12),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12d8 <main+0x118>
mov    0x2cb5(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    13e3 <main+0x223>
mov    0x2c95(%rip),%rax
mov    (%rax),%ebx
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xd29(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    13d7 <main+0x217>
mov    0x2c6b(%rip),%rax
mov    (%rax),%ebx
mov    0x2c5a(%rip),%rax
cmp    %ebx,(%rax)
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rax
lea    0xd2c(%rip),%rsi
cmove  %rax,%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    13d7 <main+0x217>
lea    0xd2b(%rip),%rdi
jmp    13c7 <main+0x207>
mov    (%r12),%edi
lea    0xd78(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2c28(%rip),%rax
mov    (%rax),%ebx
mov    0x4(%rsp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    13d7 <main+0x217>
lea    0xd6a(%rip),%rdi
call   10c0 <perror@plt>
jmp    13d7 <main+0x217>
mov    0x2beb(%rip),%rax
mov    (%rax),%ebx
neg    %ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfe(%rip),%rdi
call   10c0 <perror@plt>
jmp    13b2 <main+0x1f2>

LLVM IR (-O0):
%struct.iio_event_data = type { i32 }
@stderr = global i32 0
@.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
@errno = global i32 0
@.str.4 = [19 x i8] c"Failed to open %s\0A\00"
@IIO_GET_EVENT_FD_IOCTL = global i32 0
@ENODEV = global i32 0
@.str.5 = [37 x i8] c"This device does not support events\0A\00"
@.str.6 = [29 x i8] c"Failed to retrieve event fd\0A\00"
@.str.7 = [38 x i8] c"Failed to close character device file\00"
@EAGAIN = global i32 0
@.str.8 = [19 x i8] c"nothing available\0A\00"
@.str.9 = [33 x i8] c"Failed to read event from device\00"
@.str.10 = [23 x i8] c"Reading event failed!\0A\00"
@EIO = global i32 0
@.str.11 = [27 x i8] c"Failed to close event file\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 %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
br label %136
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = call i32 @find_type_by_name(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %41
29: ; preds = %21
%30 = load i8*, i8** %7
%31 = load i32, i32* %10
%32 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32 %31)
%33 = load i32, i32* %10
%34 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = load i32, i32* @ENOMEM
%39 = sub nsw i32 0, %38
store i32 %39, i32* %3
br label %136
40: ; preds = %29
br label %50
41: ; preds = %21
%42 = load i8*, i8** %7
%43 = call i8* @strdup(i8* %42)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = icmp ne i8* %44, null
br i1 %45, label %49, label %46
46: ; preds = %41
%47 = load i32, i32* @ENOMEM
%48 = sub nsw i32 0, %47
store i32 %48, i32* %3
br label %136
49: ; preds = %41
br label %50
50: ; preds = %49, %40
%51 = load i8*, i8** %8
%52 = call i32 @open(i8* %51, i32 0)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %61
55: ; preds = %50
%56 = load i32, i32* @errno
%57 = sub nsw i32 0, %56
store i32 %57, i32* %9
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %8
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %59)
br label %132
61: ; preds = %50
%62 = load i32, i32* %11
%63 = load i32, i32* @IIO_GET_EVENT_FD_IOCTL
%64 = call i32 @ioctl(i32 %62, i32 %63, i32* %12)
store i32 %64, i32* %9
%65 = load i32, i32* %9
%66 = icmp eq i32 %65, -1
br i1 %66, label %70, label %67
67: ; preds = %61
%68 = load i32, i32* %12
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %90
70: ; preds = %67, %61
%71 = load i32, i32* @errno
%72 = sub nsw i32 0, %71
store i32 %72, i32* %9
%73 = load i32, i32* %9
%74 = load i32, i32* @ENODEV
%75 = sub nsw i32 0, %74
%76 = icmp eq i32 %73, %75
br i1 %76, label %77, label %80
77: ; preds = %70
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
br label %83
80: ; preds = %70
%81 = load i32, i32* @stderr
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %83
83: ; preds = %80, %77
%84 = load i32, i32* %11
%85 = call i32 @close(i32 %84)
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %89
87: ; preds = %83
%88 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
br label %89
89: ; preds = %87, %83
br label %132
90: ; preds = %67
%91 = load i32, i32* %11
%92 = call i32 @close(i32 %91)
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %97
94: ; preds = %90
%95 = load i32, i32* @errno
%96 = sub nsw i32 0, %95
store i32 %96, i32* %9
br label %132
97: ; preds = %90
br label %98
98: ; preds = %97, %107, %123
%99 = load i32, i32* %12
%100 = call i32 @read(i32 %99, %struct.iio_event_data* %6, i32 4)
store i32 %100, i32* %9
%101 = load i32, i32* %9
%102 = icmp eq i32 %101, -1
br i1 %102, label %103, label %114
103: ; preds = %98
%104 = load i32, i32* @errno
%105 = load i32, i32* @EAGAIN
%106 = icmp eq i32 %104, %105
br i1 %106, label %107, label %110
107: ; preds = %103
%108 = load i32, i32* @stderr
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %98
110: ; preds = %103
%111 = load i32, i32* @errno
%112 = sub nsw i32 0, %111
store i32 %112, i32* %9
%113 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
br label %125
114: ; preds = %98
%115 = load i32, i32* %9
%116 = sext i32 %115 to i64
%117 = icmp ne i64 %116, 4
br i1 %117, label %118, label %123
118: ; preds = %114
%119 = load i32, i32* @stderr
%120 = call i32 (i32, i8*, ...) @fprintf(i32 %119, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%121 = load i32, i32* @EIO
%122 = sub nsw i32 0, %121
store i32 %122, i32* %9
br label %125
123: ; preds = %114
%124 = call i32 @print_event(%struct.iio_event_data* %6)
br label %98
125: ; preds = %118, %110
%126 = load i32, i32* %12
%127 = call i32 @close(i32 %126)
%128 = icmp eq i32 %127, -1
br i1 %128, label %129, label %131
129: ; preds = %125
%130 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0))
br label %131
131: ; preds = %129, %125
br label %132
132: ; preds = %131, %94, %89, %55
%133 = load i8*, i8** %8
%134 = call i32 @free(i8* %133)
%135 = load i32, i32* %9
store i32 %135, i32* %3
br label %136
136: ; preds = %132, %46, %37, %15
%137 = load i32, i32* %3
ret i32 %137
}

LLVM IR (-O2):
%struct.iio_event_data = type { i32 }
@stderr = global i32 0
@.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
@errno = global i32 0
@.str.4 = [19 x i8] c"Failed to open %s\0A\00"
@IIO_GET_EVENT_FD_IOCTL = global i32 0
@ENODEV = global i32 0
@.str.5 = [37 x i8] c"This device does not support events\0A\00"
@.str.6 = [29 x i8] c"Failed to retrieve event fd\0A\00"
@.str.7 = [38 x i8] c"Failed to close character device file\00"
@EAGAIN = global i32 0
@.str.8 = [19 x i8] c"nothing available\0A\00"
@.str.9 = [33 x i8] c"Failed to read event from device\00"
@.str.10 = [23 x i8] c"Reading event failed!\0A\00"
@EIO = global i32 0
@.str.11 = [27 x i8] c"Failed to close event file\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 %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %97
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @find_type_by_name(i8* %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = tail call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %16, i32 %17)
%21 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%22 = icmp slt i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = load i8*, i8** %4
br label %34
25: ; preds = %19
%26 = load i32, i32* @ENOMEM
%27 = sub nsw i32 0, %26
br label %97
28: ; preds = %14
%29 = tail call i8* @strdup(i8* %16)
store i8* %29, i8** %4
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @ENOMEM
%33 = sub nsw i32 0, %32
br label %97
34: ; preds = %23, %28
%35 = phi i8* [ %24, %23 ], [ %29, %28 ]
%36 = call i32 @open(i8* %35, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %4
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %41)
br label %92
43: ; preds = %34
%44 = load i32, i32* @IIO_GET_EVENT_FD_IOCTL
%45 = call i32 @ioctl(i32 %36, i32 %44, i32* %5)
%46 = icmp eq i32 %45, -1
%47 = load i32, i32* %5
%48 = icmp eq i32 %47, -1
%49 = select i1 %46, i1 true, i1 %48
br i1 %49, label %50, label %61
50: ; preds = %43
%51 = load i32, i32* @errno
%52 = load i32, i32* @ENODEV
%53 = icmp eq i32 %52, %51
%54 = load i32, i32* @stderr
%55 = select i1 %53, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0)
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* %55)
%57 = call i32 @close(i32 %36)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %92
59: ; preds = %50
%60 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
br label %92
61: ; preds = %43
%62 = call i32 @close(i32 %36)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = load i32, i32* @errno
br label %92
66: ; preds = %61, %76
%67 = load i32, i32* %5
%68 = call i32 @read(i32 %67, %struct.iio_event_data* %3, i32 4)
switch i32 %68, label %79 [
i32 -1, label %69
i32 4, label %83
]
69: ; preds = %66
%70 = load i32, i32* @errno
%71 = load i32, i32* @EAGAIN
%72 = icmp eq i32 %70, %71
br i1 %72, label %73, label %77
73: ; preds = %69
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %76
76: ; preds = %73, %83
br label %66
77: ; preds = %69
%78 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
br label %85
79: ; preds = %66
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%82 = load i32, i32* @EIO
br label %85
83: ; preds = %66
%84 = call i32 @print_event(%struct.iio_event_data* %3)
br label %76
85: ; preds = %79, %77
%86 = phi i32 [ %70, %77 ], [ %82, %79 ]
%87 = load i32, i32* %5
%88 = call i32 @close(i32 %87)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0))
br label %92
92: ; preds = %85, %90, %50, %59, %64, %38
%93 = phi i32 [ %39, %38 ], [ %51, %59 ], [ %51, %50 ], [ %65, %64 ], [ %86, %90 ], [ %86, %85 ]
%94 = sub nsw i32 0, %93
%95 = load i8*, i8** %4
%96 = call i32 @free(i8* %95)
br label %97
97: ; preds = %92, %31, %25, %10
%98 = phi i32 [ -1, %10 ], [ %27, %25 ], [ %94, %92 ], [ %33, %31 ]
ret i32 %98
}