#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 scalar_t__ uint64_t;
struct iocb {
  int aio_fildes;
  int aio_offset;
  int aio_nbytes;
  scalar_t__ aio_buf;
  int /*<<< orphan*/ aio_lio_opcode;
};
struct io_event {
  int res;
};
typedef int /*<<< orphan*/ cb;
typedef scalar_t__ aio_context_t;

/* Variables and functions */
int /*<<< orphan*/ IOCB_CMD_PREAD;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int io_destroy(scalar_t__);
int io_getevents(scalar_t__, int, int, struct io_event *, int /*<<< orphan*/ *);
int io_setup(int, scalar_t__ *);
int io_submit(scalar_t__, int, struct iocb **);
int /*<<< orphan*/ memset(struct iocb *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ stderr;
int main() {
  aio_context_t ctx;
  struct iocb cb;
  struct iocb *cbs[1];
  char data[4096];
  struct io_event events[1];
  int ret;
  int fd;
  fd = open("/tmp/testfile", O_RDWR | O_CREAT);
  if (fd < 0) {
    perror("open error");
    return -1;
  }
  ctx = 0;
  ret = io_setup(128, &ctx);
  if (ret < 0) {
    perror("io_setup error");
    return -1;
  }
  memset(&cb, 0, sizeof(cb));
  cb.aio_fildes = fd;
  cb.aio_lio_opcode = IOCB_CMD_PREAD;
  cb.aio_buf = (uint64_t)data;
  cb.aio_offset = 4094;
  cb.aio_nbytes = 128;
  cbs[0] = &cb;
  ret = io_submit(ctx, 1, cbs);
  if (ret != 1) {
    if (ret < 0)
      perror("io_submit error");
    else
      fprintf(stderr, "could not sumbit IOs");
    return -1;
  }
  ret = io_getevents(ctx, 1, 1, events, NULL);
  printf("io_getevents return %d, events.res %d\n", ret, events[0].res);
  ret = io_destroy(ctx);
  if (ret < 0) {
    perror("io_destroy error");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
lea    0xe45(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1050(%rbp)
cmpl   $0x0,-0x1050(%rbp)
jge    11eb <main+0x5b>
lea    0xe34(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <main+0x1bb>
movq   $0x0,-0x10(%rbp)
mov    $0x80,%edi
lea    -0x10(%rbp),%rsi
call   1030 <io_setup@plt>
mov    %eax,-0x104c(%rbp)
cmpl   $0x0,-0x104c(%rbp)
jge    122c <main+0x9c>
lea    0xdfe(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <main+0x1bb>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    -0x1050(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x1040(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0xffe,-0x2c(%rbp)
movl   $0x80,-0x28(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rdx
mov    $0x1,%esi
call   1090 <io_submit@plt>
mov    %eax,-0x104c(%rbp)
cmpl   $0x1,-0x104c(%rbp)
je     12d6 <main+0x146>
cmpl   $0x0,-0x104c(%rbp)
jge    12b5 <main+0x125>
lea    0xd7d(%rip),%rdi
call   10b0 <perror@plt>
jmp    12ca <main+0x13a>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <main+0x1bb>
mov    -0x10(%rbp),%rdi
lea    -0x1048(%rbp),%rcx
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%r8d
mov    %edx,%esi
call   1080 <io_getevents@plt>
mov    %eax,-0x104c(%rbp)
mov    -0x104c(%rbp),%esi
mov    -0x1048(%rbp),%edx
lea    0xd42(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <io_destroy@plt>
mov    %eax,-0x104c(%rbp)
cmpl   $0x0,-0x104c(%rbp)
jge    1344 <main+0x1b4>
lea    0xd41(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <main+0x1bb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1040,%rsp
mov    0x2e3e(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4c(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12a2 <main+0x112>
mov    %eax,%ebp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x80,%edi
call   1030 <io_setup@plt>
test   %eax,%eax
js     12ab <main+0x11b>
lea    0x20(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    %ebp,0x20(%rsp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0x40(%rsp),%rax
mov    %rax,0x30(%rsp)
movabs $0x8000000ffe,%rax
mov    %rax,0x24(%rsp)
mov    %rbx,0x18(%rsp)
mov    0x8(%rsp),%rdi
lea    0x18(%rsp),%rdx
mov    $0x1,%esi
call   1090 <io_submit@plt>
cmp    $0x1,%eax
jne    1287 <main+0xf7>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    $0x1,%esi
mov    $0x1,%edx
xor    %r8d,%r8d
call   1080 <io_getevents@plt>
mov    0x10(%rsp),%edx
lea    0xde4(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
call   1040 <io_destroy@plt>
test   %eax,%eax
jns    12c6 <main+0x136>
lea    0xdef(%rip),%rdi
jmp    12bb <main+0x12b>
test   %eax,%eax
js     12b4 <main+0x124>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12c0 <main+0x130>
lea    0xd65(%rip),%rdi
jmp    12bb <main+0x12b>
lea    0xd67(%rip),%rdi
jmp    12bb <main+0x12b>
lea    0xd6d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x1040,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iocb = type { i32, i32, i32, i64, i32 }
%struct.io_event = type { i32 }
@.str = [14 x i8] c"/tmp/testfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [15 x i8] c"io_setup error\00"
@IOCB_CMD_PREAD = global i32 0
@.str.3 = [16 x i8] c"io_submit error\00"
@stderr = global i32 0
@.str.4 = [21 x i8] c"could not sumbit IOs\00"
@.str.5 = [39 x i8] c"io_getevents return %d, events.res %d\0A\00"
@.str.6 = [17 x i8] c"io_destroy error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca %struct.iocb
%4 = alloca [1 x %struct.iocb*]
%5 = alloca [4096 x i8]
%6 = alloca [1 x %struct.io_event]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %65
17: ; preds = %0
store i64 0, i64* %2
%18 = call i32 @io_setup(i32 128, i64* %2)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %65
23: ; preds = %17
%24 = call i32 @memset(%struct.iocb* %3, i32 0, i32 32)
%25 = load i32, i32* %8
%26 = getelementptr inbounds %struct.iocb, %struct.iocb* %3, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @IOCB_CMD_PREAD
%28 = getelementptr inbounds %struct.iocb, %struct.iocb* %3, i32 0, i32 4
store i32 %27, i32* %28
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%30 = ptrtoint i8* %29 to i64
%31 = getelementptr inbounds %struct.iocb, %struct.iocb* %3, i32 0, i32 3
store i64 %30, i64* %31
%32 = getelementptr inbounds %struct.iocb, %struct.iocb* %3, i32 0, i32 1
store i32 4094, i32* %32
%33 = getelementptr inbounds %struct.iocb, %struct.iocb* %3, i32 0, i32 2
store i32 128, i32* %33
%34 = getelementptr inbounds [1 x %struct.iocb*], [1 x %struct.iocb*]* %4, i64 0, i64 0
store %struct.iocb* %3, %struct.iocb** %34
%35 = load i64, i64* %2
%36 = getelementptr inbounds [1 x %struct.iocb*], [1 x %struct.iocb*]* %4, i64 0, i64 0
%37 = call i32 @io_submit(i64 %35, i32 1, %struct.iocb** %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %49
40: ; preds = %23
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %48
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %45, %43
store i32 -1, i32* %1
br label %65
49: ; preds = %23
%50 = load i64, i64* %2
%51 = getelementptr inbounds [1 x %struct.io_event], [1 x %struct.io_event]* %6, i64 0, i64 0
%52 = call i32 @io_getevents(i64 %50, i32 1, i32 1, %struct.io_event* %51, i32* null)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = getelementptr inbounds [1 x %struct.io_event], [1 x %struct.io_event]* %6, i64 0, i64 0
%55 = getelementptr inbounds %struct.io_event, %struct.io_event* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %53, i32 %56)
%58 = load i64, i64* %2
%59 = call i32 @io_destroy(i64 %58)
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %49
%63 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %65
64: ; preds = %49
store i32 0, i32* %1
br label %65
65: ; preds = %64, %62, %48, %21, %15
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
%struct.iocb = type { i32, i32, i32, i64, i32 }
%struct.io_event = type { i32 }
@.str = [14 x i8] c"/tmp/testfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [15 x i8] c"io_setup error\00"
@IOCB_CMD_PREAD = global i32 0
@.str.3 = [16 x i8] c"io_submit error\00"
@stderr = global i32 0
@.str.4 = [21 x i8] c"could not sumbit IOs\00"
@.str.5 = [39 x i8] c"io_getevents return %d, events.res %d\0A\00"
@.str.6 = [17 x i8] c"io_destroy error\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca %struct.iocb
%3 = alloca [1 x %struct.iocb*]
%4 = alloca [4096 x i8]
%5 = alloca [1 x %struct.io_event]
%6 = bitcast i64* %1 to i8*
%7 = bitcast %struct.iocb* %2 to i8*
%8 = bitcast [1 x %struct.iocb*]* %3 to i8*
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%10 = bitcast [1 x %struct.io_event]* %5 to i8*
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %55
18: ; preds = %0
store i64 0, i64* %1
%19 = call i32 @io_setup(i32 128, i64* %1)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %55
23: ; preds = %18
%24 = call i32 @memset(%struct.iocb* %2, i32 0, i32 32)
%25 = getelementptr inbounds %struct.iocb, %struct.iocb* %2, i64 0, i32 0
store i32 %14, i32* %25
%26 = load i32, i32* @IOCB_CMD_PREAD
%27 = getelementptr inbounds %struct.iocb, %struct.iocb* %2, i64 0, i32 4
store i32 %26, i32* %27
%28 = ptrtoint [4096 x i8]* %4 to i64
%29 = getelementptr inbounds %struct.iocb, %struct.iocb* %2, i64 0, i32 3
store i64 %28, i64* %29
%30 = getelementptr inbounds %struct.iocb, %struct.iocb* %2, i64 0, i32 1
store i32 4094, i32* %30
%31 = getelementptr inbounds %struct.iocb, %struct.iocb* %2, i64 0, i32 2
store i32 128, i32* %31
%32 = getelementptr inbounds [1 x %struct.iocb*], [1 x %struct.iocb*]* %3, i64 0, i64 0
store %struct.iocb* %2, %struct.iocb** %32
%33 = load i64, i64* %1
%34 = call i32 @io_submit(i64 %33, i32 1, %struct.iocb** %32)
%35 = icmp eq i32 %34, 1
br i1 %35, label %43, label %36
36: ; preds = %23
%37 = icmp slt i32 %34, 0
br i1 %37, label %38, label %40
38: ; preds = %36
%39 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %55
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %55
43: ; preds = %23
%44 = load i64, i64* %1
%45 = getelementptr inbounds [1 x %struct.io_event], [1 x %struct.io_event]* %5, i64 0, i64 0
%46 = call i32 @io_getevents(i64 %44, i32 1, i32 1, %struct.io_event* %45, i32* null)
%47 = getelementptr inbounds [1 x %struct.io_event], [1 x %struct.io_event]* %5, i64 0, i64 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 %48)
%50 = load i64, i64* %1
%51 = call i32 @io_destroy(i64 %50)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %43
%54 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %43, %38, %40, %53, %21, %16
%56 = phi i32 [ -1, %16 ], [ -1, %21 ], [ -1, %53 ], [ -1, %40 ], [ -1, %38 ], [ 0, %43 ]
ret i32 %56
}