#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ sigev_notify;
};
struct aiocb {
  int aio_fildes;
  char *aio_buf;
  int aio_nbytes;
  TYPE_1__ aio_sigevent;
  scalar_t__ aio_reqprio;
  scalar_t__ aio_offset;
};

/* Variables and functions */
int EXIT_FAILURE;
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ SIGEV_NONE;
int /*<<< orphan*/ aio_read(struct aiocb *);
int /*<<< orphan*/ aio_suspend(struct aiocb const *const *, int,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ aio_write(struct aiocb *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
int main(int argc, char **argv) {
  if (argc != 3 || !strcmp(argv[1], "-h")) {
    fprintf(stderr, "usage: %s FILE TEXT...\n", argv[0]);
    return EXIT_FAILURE;
  }
  char *file = argv[1];
  char *text = argv[2];
  int len = strlen(text);
  int fd = open(file, O_WRONLY | O_CREAT, 0600);
  struct aiocb aio;
  aio.aio_fildes = fd;
  aio.aio_buf = text;
  aio.aio_nbytes = len;
  aio.aio_offset = 0;
  aio.aio_reqprio = 0;
  aio.aio_sigevent.sigev_notify = SIGEV_NONE;
  aio_write(&aio);
  fd = open(file, O_RDONLY, 0600);
  struct aiocb const *const aios = &aio;
  aio_suspend(&aios, 1, NULL);
  char *buf = malloc(len);
  aio.aio_fildes = fd;
  aio.aio_buf = buf;
  aio.aio_nbytes = len;
  aio_read(&aio);
  aio_suspend(&aios, 1, NULL);
  printf("read: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11cd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11fa <main+0x6a>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1f(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1300 <main+0x170>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x50(%rbp),%rdi
call   1030 <aio_write@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x58(%rbp),%rdi
mov    $0x1,%esi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <aio_suspend@plt>
mov    -0x24(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%rdi
call   1080 <aio_read@plt>
lea    -0x58(%rbp),%rdi
mov    $0x1,%esi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <aio_suspend@plt>
mov    -0x60(%rbp),%rsi
lea    0xd27(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    1299 <main+0x109>
mov    0x8(%rbp),%rbx
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1299 <main+0x109>
mov    0x10(%rbp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    0x2dd5(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,0x10(%rsp)
mov    %rbp,0x18(%rsp)
mov    %r15d,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <aio_write@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,%r13d
mov    %r14,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   10a0 <aio_suspend@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %r13d,0x10(%rsp)
mov    %rax,0x18(%rsp)
mov    %r15d,0x20(%rsp)
mov    %r14,%rdi
call   1080 <aio_read@plt>
mov    %r12,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   10a0 <aio_suspend@plt>
lea    0xd8c(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
jmp    12bb <main+0x12b>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd56(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.aiocb = type { i32, i8*, i32, %struct.TYPE_2__, i64, i64 }
%struct.TYPE_2__ = type { i32 }
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"usage: %s FILE TEXT...\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@SIGEV_NONE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"read: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.aiocb
%11 = alloca %struct.aiocb*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %21, label %15
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strcmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%20 = icmp ne i32 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %15, %2
%22 = load i32, i32* @stderr
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %3
br label %70
28: ; preds = %15
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
store i8* %31, i8** %6
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = call i32 @strlen(i8* %35)
store i32 %36, i32* %8
%37 = load i8*, i8** %6
%38 = load i32, i32* @O_WRONLY
%39 = load i32, i32* @O_CREAT
%40 = or i32 %38, %39
%41 = call i32 @open(i8* %37, i32 %40, i32 384)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i8*, i8** %7
%45 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 1
store i8* %44, i8** %45
%46 = load i32, i32* %8
%47 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 2
store i32 %46, i32* %47
%48 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 5
store i64 0, i64* %48
%49 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 4
store i64 0, i64* %49
%50 = load i32, i32* @SIGEV_NONE
%51 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 3
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i32 %50, i32* %52
%53 = call i32 @aio_write(%struct.aiocb* %10)
%54 = load i8*, i8** %6
%55 = load i32, i32* @O_RDONLY
%56 = call i32 @open(i8* %54, i32 %55, i32 384)
store i32 %56, i32* %9
store %struct.aiocb* %10, %struct.aiocb** %11
%57 = call i32 @aio_suspend(%struct.aiocb** %11, i32 1, i32* null)
%58 = load i32, i32* %8
%59 = call i8* @malloc(i32 %58)
store i8* %59, i8** %12
%60 = load i32, i32* %9
%61 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 0
store i32 %60, i32* %61
%62 = load i8*, i8** %12
%63 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 1
store i8* %62, i8** %63
%64 = load i32, i32* %8
%65 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %10, i32 0, i32 2
store i32 %64, i32* %65
%66 = call i32 @aio_read(%struct.aiocb* %10)
%67 = call i32 @aio_suspend(%struct.aiocb** %11, i32 1, i32* null)
%68 = load i8*, i8** %12
%69 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %68)
store i32 0, i32* %3
br label %70
70: ; preds = %28, %21
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i8*, i32, %struct.TYPE_2__, i64, i64 }
%struct.TYPE_2__ = type { i32 }
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"usage: %s FILE TEXT...\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@SIGEV_NONE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"read: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.aiocb
%4 = alloca %struct.aiocb*
%5 = icmp eq i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6, %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
br label %41
16: ; preds = %6
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @strlen(i8* %18)
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %8, i32 %22, i32 384)
%24 = bitcast %struct.aiocb* %3 to i8*
%25 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 0
store i32 %23, i32* %25
%26 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 1
store i8* %18, i8** %26
%27 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 2
store i32 %19, i32* %27
%28 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 4
%29 = bitcast i64* %28 to i8*
%30 = load i32, i32* @SIGEV_NONE
%31 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 3, i32 0
store i32 %30, i32* %31
%32 = call i32 @aio_write(%struct.aiocb* %3)
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %8, i32 %33, i32 384)
%35 = bitcast %struct.aiocb** %4 to i8*
store %struct.aiocb* %3, %struct.aiocb** %4
%36 = call i32 @aio_suspend(%struct.aiocb** %4, i32 1, i32* null)
%37 = call i8* @malloc(i32 %19)
store i32 %34, i32* %25
store i8* %37, i8** %26
store i32 %19, i32* %27
%38 = call i32 @aio_read(%struct.aiocb* %3)
%39 = call i32 @aio_suspend(%struct.aiocb** %4, i32 1, i32* null)
%40 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %41
41: ; preds = %16, %11
%42 = phi i32 [ %15, %11 ], [ 0, %16 ]
ret i32 %42
}