#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 timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};
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 /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ EINPROGRESS;
int EXIT_FAILURE;
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ SIGEV_NONE;
scalar_t__ aio_error(struct aiocb *);
int /*<<< orphan*/ aio_write(struct aiocb *);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
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 timespec tspec;
  tspec.tv_sec = 0;
  tspec.tv_nsec = 50;
  while (aio_error(&aio) == EINPROGRESS)
    clock_nanosleep(CLOCK_REALTIME, 0, &tspec, NULL);
  char *buf = malloc(len);
  read(fd, buf, len);
  printf("read: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11dd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe31(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    120a <main+0x6a>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe0f(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131b <main+0x17b>
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    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%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    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x50(%rbp),%rdi
call   1030 <aio_write@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x58(%rbp)
movl   $0x32,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
call   1090 <aio_error@plt>
mov    0x2d15(%rip),%rcx
cmp    (%rcx),%rax
jne    12e9 <main+0x149>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x60(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10c0 <clock_nanosleep@plt>
jmp    12b3 <main+0x113>
mov    -0x24(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
mov    -0x68(%rbp),%rsi
lea    0xd0c(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%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    12ca <main+0x12a>
mov    0x8(%rbp),%rbx
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     12ca <main+0x12a>
mov    0x10(%rbp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    0x2db5(%rip),%rax
mov    0x2e06(%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    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <aio_write@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,%r14d
movq   $0x0,0x8(%rsp)
movl   $0x32,(%rsp)
mov    %rbp,%rdi
call   1090 <aio_error@plt>
mov    0x2d6d(%rip),%r12
cmp    (%r12),%rax
jne    129e <main+0xfe>
mov    0x2d38(%rip),%r13
mov    %rsp,%rbx
lea    0x10(%rsp),%rbp
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10c0 <clock_nanosleep@plt>
mov    %rbp,%rdi
call   1090 <aio_error@plt>
cmp    (%r12),%rax
je     1280 <main+0xe0>
mov    %r15d,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
lea    0xd5d(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
jmp    12ec <main+0x14c>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd25(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%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 }
%struct.timespec = type { i32, i64 }
@.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
@EINPROGRESS = global i64 0
@CLOCK_REALTIME = 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.timespec
%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 %75
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
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
store i64 0, i64* %57
%58 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
store i32 50, i32* %58
br label %59
59: ; preds = %63, %28
%60 = call i64 @aio_error(%struct.aiocb* %10)
%61 = load i64, i64* @EINPROGRESS
%62 = icmp eq i64 %60, %61
br i1 %62, label %63, label %66
63: ; preds = %59
%64 = load i32, i32* @CLOCK_REALTIME
%65 = call i32 @clock_nanosleep(i32 %64, i32 0, %struct.timespec* %11, i32* null)
br label %59
66: ; preds = %59
%67 = load i32, i32* %8
%68 = call i8* @malloc(i32 %67)
store i8* %68, i8** %12
%69 = load i32, i32* %9
%70 = load i8*, i8** %12
%71 = load i32, i32* %8
%72 = call i32 @read(i32 %69, i8* %70, i32 %71)
%73 = load i8*, i8** %12
%74 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %66, %21
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i8*, i32, %struct.TYPE_2__, i64, i64 }
%struct.TYPE_2__ = type { i32 }
%struct.timespec = type { i32, i64 }
@.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
@EINPROGRESS = global i64 0
@CLOCK_REALTIME = 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.timespec
%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 %51
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.timespec* %4 to i8*
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i64 0, i64* %36
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
store i32 50, i32* %37
%38 = call i64 @aio_error(%struct.aiocb* %3)
%39 = load i64, i64* @EINPROGRESS
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %47
41: ; preds = %16, %41
%42 = load i32, i32* @CLOCK_REALTIME
%43 = call i32 @clock_nanosleep(i32 %42, i32 0, %struct.timespec* %4, i32* null)
%44 = call i64 @aio_error(%struct.aiocb* %3)
%45 = load i64, i64* @EINPROGRESS
%46 = icmp eq i64 %44, %45
br i1 %46, label %41, label %47
47: ; preds = %41, %16
%48 = call i8* @malloc(i32 %19)
%49 = call i32 @read(i32 %34, i8* %48, i32 %19)
%50 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %48)
br label %51
51: ; preds = %47, %11
%52 = phi i32 [ %15, %11 ], [ 0, %47 ]
ret i32 %52
}