#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 {
  scalar_t__ fd;
  int events;
  int revents;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int POLLIN;
struct pollfd *calloc(int, int);
int /*<<< orphan*/ close(scalar_t__);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
scalar_t__ open(char *, int);
scalar_t__ poll(struct pollfd *, int, int);
scalar_t__ read(scalar_t__, char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ write(int /*<<< orphan*/, char *, scalar_t__);
int ktest_poll(int dev_no, char *dev[], int nonblock) {
  int flags, i, retval = 0;
  struct pollfd *fds;
  char *buf;
  size_t size;
  ssize_t count;
  flags = nonblock ? (O_RDONLY | O_NONBLOCK) : (O_RDONLY);
  fds = calloc(dev_no, sizeof(struct pollfd));
  for (i = 0; i < dev_no; i++) {
    fds[i].fd = open(dev[i], flags);
    fds[i].events = POLLIN;
    if (fds[i].fd < 0) {
      fprintf(stderr, "Can't open %s\n", dev[i]);
      retval = -1;
      goto err1;
    }
  }
  size = (4 * 1024);
  buf = malloc(size);
  if (buf == NULL) {
    fprintf(stderr, "Can't get memory\n");
    retval = -1;
    goto err2;
  }
  while (1) {
    if (poll(fds, dev_no, -1) < 0) {
      fprintf(stderr, "Can't poll devices\n");
      retval = -1;
      goto err3;
    }
    for (i = 0; i < dev_no; i++) {
      if (fds[i].revents & POLLIN) {
        count = read(fds[i].fd, buf, size);
        if (count < 0) {
          fprintf(stderr, "Can't read %s\n", dev[i]);
          retval = -1;
          goto err3;
        }
        count = write(fileno(stdout), buf, count);
        fds[i].revents &= ~POLLIN;
      }
    }
  }
  free(buf);
  for (i = 0; i < dev_no; i++)
    close(fds[i].fd);
  return retval;
err3:
  free(buf);
err2:
  for (i = 0; i < dev_no; i++)
    close(fds[i].fd);
err1:
  return retval;
}


x86 assembly (clang, -O0):
<ktest_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11dd <ktest_poll+0x3d>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    0x2e15(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
jmp    11e9 <ktest_poll+0x49>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x10,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12b2 <ktest_poll+0x112>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%esi
call   10c0 <open@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
mov    0x2d95(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,(%rax)
jge    129f <ktest_poll+0xff>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    146c <ktest_poll+0x2cc>
jmp    12a4 <ktest_poll+0x104>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1207 <ktest_poll+0x67>
movq   $0x1000,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12f5 <ktest_poll+0x155>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd2d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    142f <ktest_poll+0x28f>
jmp    12fa <ktest_poll+0x15a>
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    $0xffffffff,%edx
call   10b0 <poll@plt>
cmp    $0x0,%rax
jge    1338 <ktest_poll+0x198>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
lea    0xcfc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    1426 <ktest_poll+0x286>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1421 <ktest_poll+0x281>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0xc(%rax),%eax
mov    0x2c74(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     140e <ktest_poll+0x26e>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    13cc <ktest_poll+0x22c>
mov    0x2c24(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xc7c(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    1426 <ktest_poll+0x286>
mov    0x2bed(%rip),%rax
mov    (%rax),%edi
call   1090 <fileno@plt>
mov    %eax,%edi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x40(%rbp)
mov    0x2be4(%rip),%rax
mov    (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
and    0xc(%rax),%ecx
mov    %ecx,0xc(%rax)
jmp    1413 <ktest_poll+0x273>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    133f <ktest_poll+0x19f>
jmp    12fa <ktest_poll+0x15a>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1467 <ktest_poll+0x2c7>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rdi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1436 <ktest_poll+0x296>
jmp    146c <ktest_poll+0x2cc>
mov    -0x20(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ktest_poll>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,0x10(%rsp)
mov    %edi,%ebp
mov    0x2e21(%rip),%rax
test   %edx,%edx
je     11cd <ktest_poll+0x2d>
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%r14d
or     (%rax),%r14d
mov    %ebp,%edi
mov    $0x10,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
mov    %ebp,%r13d
mov    %ebp,0xc(%rsp)
test   %ebp,%ebp
jle    122f <ktest_poll+0x8f>
lea    0x0(,%r13,8),%r15
xor    %ebp,%ebp
mov    0x2ddd(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rax
mov    (%rax,%rbp,1),%rdi
mov    %r14d,%esi
call   10c0 <open@plt>
mov    %rax,(%rbx,%rbp,2)
mov    (%r12),%ecx
mov    %ecx,0x8(%rbx,%rbp,2)
test   %rax,%rax
js     131c <ktest_poll+0x17c>
add    $0x8,%rbp
cmp    %rbp,%r15
jne    1200 <ktest_poll+0x60>
mov    $0x1000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     133e <ktest_poll+0x19e>
mov    %rax,%r12
mov    %rbx,%rdi
mov    0xc(%rsp),%ebp
mov    %ebp,%esi
mov    $0xffffffff,%edx
call   10b0 <poll@plt>
test   %rax,%rax
js     135f <ktest_poll+0x1bf>
lea    0x0(,%r13,8),%r14
jmp    128c <ktest_poll+0xec>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    0xc(%rsp),%ebp
mov    %ebp,%esi
mov    $0xffffffff,%edx
call   10b0 <poll@plt>
test   %rax,%rax
js     135f <ktest_poll+0x1bf>
test   %ebp,%ebp
jle    1270 <ktest_poll+0xd0>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
xor    %r15d,%r15d
jmp    12d9 <ktest_poll+0x139>
xchg   %ax,%ax
mov    %rax,%rbp
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   1090 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1040 <write@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
not    %ecx
and    %ecx,0xc(%rbx,%r15,2)
add    $0x8,%r15
cmp    %r15,%r14
je     1270 <ktest_poll+0xd0>
test   %eax,0xc(%rbx,%r15,2)
je     12d0 <ktest_poll+0x130>
mov    (%rbx,%r15,2),%rdi
mov    $0x1000,%edx
mov    %r12,%rsi
call   1060 <read@plt>
test   %rax,%rax
jns    12a0 <ktest_poll+0x100>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rax
mov    (%rax,%r15,1),%rdx
lea    0xd26(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0xc(%rsp),%ebp
jmp    1376 <ktest_poll+0x1d6>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rax
mov    (%rax,%rbp,1),%rdx
lea    0xccb(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    13a2 <ktest_poll+0x202>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
lea    0xcc1(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0xc(%rsp),%ebp
test   %ebp,%ebp
jg     1382 <ktest_poll+0x1e2>
jmp    13a2 <ktest_poll+0x202>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
lea    0xcb2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jle    13a2 <ktest_poll+0x202>
shl    $0x4,%r13
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,1),%rdi
call   1050 <close@plt>
add    $0x10,%rbp
cmp    %rbp,%r13
jne    1390 <ktest_poll+0x1f0>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i64, i32, i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@POLLIN = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@.str.1 = [18 x i8] c"Can't get memory\0A\00"
@.str.2 = [20 x i8] c"Can't poll devices\0A\00"
@.str.3 = [15 x i8] c"Can't read %s\0A\00"
@stdout = global i32 0
define i32 @ktest_poll(i32 %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.pollfd*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
store i32 0, i32* %9
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
br label %22
20: ; preds = %3
%21 = load i32, i32* @O_RDONLY
br label %22
22: ; preds = %20, %16
%23 = phi i32 [ %19, %16 ], [ %21, %20 ]
store i32 %23, i32* %7
%24 = load i32, i32* %4
%25 = call %struct.pollfd* @calloc(i32 %24, i32 16)
store %struct.pollfd* %25, %struct.pollfd** %10
store i32 0, i32* %8
br label %26
26: ; preds = %65, %22
%27 = load i32, i32* %8
%28 = load i32, i32* %4
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %68
30: ; preds = %26
%31 = load i8**, i8*** %5
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = load i32, i32* %7
%37 = call i64 @open(i8* %35, i32 %36)
%38 = load %struct.pollfd*, %struct.pollfd** %10
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %38, i64 %40
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %41, i32 0, i32 0
store i64 %37, i64* %42
%43 = load i32, i32* @POLLIN
%44 = load %struct.pollfd*, %struct.pollfd** %10
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %44, i64 %46
%48 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %47, i32 0, i32 1
store i32 %43, i32* %48
%49 = load %struct.pollfd*, %struct.pollfd** %10
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %49, i64 %51
%53 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %52, i32 0, i32 0
%54 = load i64, i64* %53
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %30
%57 = load i32, i32* @stderr
%58 = load i8**, i8*** %5
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %62)
store i32 -1, i32* %9
br label %160
64: ; preds = %30
br label %65
65: ; preds = %64
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %26
68: ; preds = %26
store i64 4096, i64* %12
%69 = load i64, i64* %12
%70 = call i8* @malloc(i64 %69)
store i8* %70, i8** %11
%71 = load i8*, i8** %11
%72 = icmp eq i8* %71, null
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 ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %9
br label %143
76: ; preds = %68
br label %77
77: ; preds = %76, %139
%78 = load %struct.pollfd*, %struct.pollfd** %10
%79 = load i32, i32* %4
%80 = call i64 @poll(%struct.pollfd* %78, i32 %79, i32 -1)
%81 = icmp slt i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %77
%83 = load i32, i32* @stderr
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %9
br label %140
85: ; preds = %77
store i32 0, i32* %8
br label %86
86: ; preds = %136, %85
%87 = load i32, i32* %8
%88 = load i32, i32* %4
%89 = icmp slt i32 %87, %88
br i1 %89, label %90, label %139
90: ; preds = %86
%91 = load %struct.pollfd*, %struct.pollfd** %10
%92 = load i32, i32* %8
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %91, i64 %93
%95 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %94, i32 0, i32 2
%96 = load i32, i32* %95
%97 = load i32, i32* @POLLIN
%98 = and i32 %96, %97
%99 = icmp ne i32 %98, 0
br i1 %99, label %100, label %135
100: ; preds = %90
%101 = load %struct.pollfd*, %struct.pollfd** %10
%102 = load i32, i32* %8
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %101, i64 %103
%105 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %104, i32 0, i32 0
%106 = load i64, i64* %105
%107 = load i8*, i8** %11
%108 = load i64, i64* %12
%109 = call i64 @read(i64 %106, i8* %107, i64 %108)
store i64 %109, i64* %13
%110 = load i64, i64* %13
%111 = icmp slt i64 %110, 0
br i1 %111, label %112, label %120
112: ; preds = %100
%113 = load i32, i32* @stderr
%114 = load i8**, i8*** %5
%115 = load i32, i32* %8
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds i8*, i8** %114, i64 %116
%118 = load i8*, i8** %117
%119 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %118)
store i32 -1, i32* %9
br label %140
120: ; preds = %100
%121 = load i32, i32* @stdout
%122 = call i32 @fileno(i32 %121)
%123 = load i8*, i8** %11
%124 = load i64, i64* %13
%125 = call i64 @write(i32 %122, i8* %123, i64 %124)
store i64 %125, i64* %13
%126 = load i32, i32* @POLLIN
%127 = xor i32 %126, -1
%128 = load %struct.pollfd*, %struct.pollfd** %10
%129 = load i32, i32* %8
%130 = sext i32 %129 to i64
%131 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %128, i64 %130
%132 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %131, i32 0, i32 2
%133 = load i32, i32* %132
%134 = and i32 %133, %127
store i32 %134, i32* %132
br label %135
135: ; preds = %120, %90
br label %136
136: ; preds = %135
%137 = load i32, i32* %8
%138 = add nsw i32 %137, 1
store i32 %138, i32* %8
br label %86
139: ; preds = %86
br label %77
140: ; preds = %112, %82
%141 = load i8*, i8** %11
%142 = call i32 @free(i8* %141)
br label %143
143: ; preds = %140, %73
store i32 0, i32* %8
br label %144
144: ; preds = %156, %143
%145 = load i32, i32* %8
%146 = load i32, i32* %4
%147 = icmp slt i32 %145, %146
br i1 %147, label %148, label %159
148: ; preds = %144
%149 = load %struct.pollfd*, %struct.pollfd** %10
%150 = load i32, i32* %8
%151 = sext i32 %150 to i64
%152 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %149, i64 %151
%153 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %152, i32 0, i32 0
%154 = load i64, i64* %153
%155 = call i32 @close(i64 %154)
br label %156
156: ; preds = %148
%157 = load i32, i32* %8
%158 = add nsw i32 %157, 1
store i32 %158, i32* %8
br label %144
159: ; preds = %144
br label %160
160: ; preds = %159, %56
%161 = load i32, i32* %9
ret i32 %161
}

LLVM IR (-O2):
%struct.pollfd = type { i64, i32, i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@POLLIN = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@.str.1 = [18 x i8] c"Can't get memory\0A\00"
@.str.2 = [20 x i8] c"Can't poll devices\0A\00"
@.str.3 = [15 x i8] c"Can't read %s\0A\00"
@stdout = global i32 0
define i32 @ktest_poll(i32 %0, i8** nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = select i1 %4, i32 0, i32 %6
%8 = or i32 %7, %5
%9 = tail call %struct.pollfd* @calloc(i32 %0, i32 16)
%10 = icmp sgt i32 %0, 0
br i1 %10, label %11, label %31
11: ; preds = %3
%12 = zext i32 %0 to i64
br label %13
13: ; preds = %11, %28
%14 = phi i64 [ 0, %11 ], [ %29, %28 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %14
%16 = load i8*, i8** %15
%17 = tail call i64 @open(i8* %16, i32 %8)
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i64 %14, i32 0
store i64 %17, i64* %18
%19 = load i32, i32* @POLLIN
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i64 %14, i32 1
store i32 %19, i32* %20
%21 = icmp slt i64 %17, 0
br i1 %21, label %22, label %28
22: ; preds = %13
%23 = and i64 %14, 4294967295
%24 = getelementptr inbounds i8*, i8** %1, i64 %23
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %24
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %93
28: ; preds = %13
%29 = add nuw nsw i64 %14, 1
%30 = icmp eq i64 %29, %12
br i1 %30, label %31, label %13
31: ; preds = %28, %3
%32 = tail call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
%33 = icmp eq i8* %32, null
br i1 %33, label %39, label %34
34: ; preds = %31
%35 = tail call i64 @poll(%struct.pollfd* %9, i32 %0, i32 -1)
%36 = icmp slt i64 %35, 0
br i1 %36, label %48, label %37
37: ; preds = %34
%38 = zext i32 %0 to i64
br label %45
39: ; preds = %31
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %83
42: ; preds = %77, %45
%43 = tail call i64 @poll(%struct.pollfd* %9, i32 %0, i32 -1)
%44 = icmp slt i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %37, %42
br i1 %10, label %46, label %42
46: ; preds = %45
%47 = load i32, i32* @POLLIN
br label %51
48: ; preds = %42, %34
%49 = load i32, i32* @stderr
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %81
51: ; preds = %46, %77
%52 = phi i32 [ %47, %46 ], [ %78, %77 ]
%53 = phi i64 [ 0, %46 ], [ %79, %77 ]
%54 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i64 %53, i32 2
%55 = load i32, i32* %54
%56 = and i32 %52, %55
%57 = icmp eq i32 %56, 0
br i1 %57, label %77, label %58
58: ; preds = %51
%59 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i64 %53, i32 0
%60 = load i64, i64* %59
%61 = tail call i64 @read(i64 %60, i8* %32, i64 4096)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %69
63: ; preds = %58
%64 = and i64 %53, 4294967295
%65 = load i32, i32* @stderr
%66 = getelementptr inbounds i8*, i8** %1, i64 %64
%67 = load i8*, i8** %66
%68 = tail call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %67)
br label %81
69: ; preds = %58
%70 = load i32, i32* @stdout
%71 = tail call i32 @fileno(i32 %70)
%72 = tail call i64 @write(i32 %71, i8* %32, i64 %61)
%73 = load i32, i32* @POLLIN
%74 = xor i32 %73, -1
%75 = load i32, i32* %54
%76 = and i32 %75, %74
store i32 %76, i32* %54
br label %77
77: ; preds = %51, %69
%78 = phi i32 [ %52, %51 ], [ %73, %69 ]
%79 = add nuw nsw i64 %53, 1
%80 = icmp eq i64 %79, %38
br i1 %80, label %42, label %51
81: ; preds = %63, %48
%82 = tail call i32 @free(i8* %32)
br label %83
83: ; preds = %81, %39
br i1 %10, label %84, label %93
84: ; preds = %83
%85 = zext i32 %0 to i64
br label %86
86: ; preds = %84, %86
%87 = phi i64 [ 0, %84 ], [ %91, %86 ]
%88 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i64 %87, i32 0
%89 = load i64, i64* %88
%90 = tail call i32 @close(i64 %89)
%91 = add nuw nsw i64 %87, 1
%92 = icmp eq i64 %91, %85
br i1 %92, label %93, label %86
93: ; preds = %86, %83, %22
ret i32 -1
}