#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 aiocb {
  int aio_fildes;
  char *aio_buf;
  scalar_t__ aio_offset;
  int /*<<< orphan*/ aio_nbytes;
};

/* Variables and functions */
scalar_t__ EINPROGRESS;
int O_APPEND;
int O_CREAT;
int O_RDWR;
scalar_t__ aio_error(struct aiocb *);
scalar_t__ aio_write(struct aiocb *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct aiocb *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int async_write(char *str) {
  int file = open("/tmp/memento/", O_CREAT | O_RDWR | O_APPEND, 0644);
  if (file == -1) {
    perror("log");
    exit(1);
  }
  struct aiocb cb;
  char *buf = malloc(strlen(str));
  strcpy(buf, str);
  memset(&cb, 0, sizeof(struct aiocb));
  cb.aio_nbytes = strlen(str);
  cb.aio_fildes = file;
  cb.aio_buf = str;
  cb.aio_offset = 0;
  if (aio_write(&cb) != 0) {
    perror("aiocb");
    return -1;
  }
  while (aio_error(&cb) == EINPROGRESS) {
  }
  close(file);
  return 0;
}


x86 assembly (clang, -O0):
<async_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
lea    0xe32(%rip),%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fb <async_write+0x5b>
lea    0xe22(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
call   1040 <aio_write@plt>
cmp    $0x0,%rax
je     1279 <async_write+0xd9>
lea    0xdaa(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <async_write+0x10b>
jmp    127e <async_write+0xde>
lea    -0x38(%rbp),%rdi
call   1080 <aio_error@plt>
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
jne    129c <async_write+0xfc>
jmp    127e <async_write+0xde>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<async_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e47(%rip),%rax
mov    0x2e28(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%rbx
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
lea    0xe31(%rip),%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1286 <async_write+0xe6>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,0x20(%rsp)
mov    %r15d,0x8(%rsp)
mov    %rbx,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r14,%rdi
call   1040 <aio_write@plt>
test   %rax,%rax
jne    1273 <async_write+0xd3>
lea    0x8(%rsp),%rbx
mov    0x2d8d(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <aio_error@plt>
cmp    0x0(%rbp),%rax
je     1250 <async_write+0xb0>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd98(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1268 <async_write+0xc8>
lea    0xd81(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.aiocb = type { i32, i8*, i64, i32 }
@.str = [14 x i8] c"/tmp/memento/\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [4 x i8] c"log\00"
@.str.2 = [6 x i8] c"aiocb\00"
@EINPROGRESS = global i64 0
define i32 @async_write(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.aiocb
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = load i32, i32* @O_APPEND
%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, i32 420)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = call i32 @memset(%struct.aiocb* %5, i32 0, i32 32)
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 3
store i32 %27, i32* %28
%29 = load i32, i32* %4
%30 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i8*, i8** %3
%32 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 1
store i8* %31, i8** %32
%33 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 2
store i64 0, i64* %33
%34 = call i64 @aio_write(%struct.aiocb* %5)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %18
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
38: ; preds = %18
br label %39
39: ; preds = %43, %38
%40 = call i64 @aio_error(%struct.aiocb* %5)
%41 = load i64, i64* @EINPROGRESS
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
br label %39
44: ; preds = %39
%45 = load i32, i32* %4
%46 = call i32 @close(i32 %45)
store i32 0, i32* %2
br label %47
47: ; preds = %44, %36
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i8*, i64, i32 }
@.str = [14 x i8] c"/tmp/memento/\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [4 x i8] c"log\00"
@.str.2 = [6 x i8] c"aiocb\00"
@EINPROGRESS = global i64 0
define i32 @async_write(i8* %0) {
%2 = alloca %struct.aiocb
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @O_APPEND
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7, i32 420)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = bitcast %struct.aiocb* %2 to i8*
%15 = tail call i32 @strlen(i8* %0)
%16 = tail call i8* @malloc(i32 %15)
%17 = tail call i32 @strcpy(i8* %16, i8* %0)
%18 = call i32 @memset(%struct.aiocb* %2, i32 0, i32 32)
%19 = call i32 @strlen(i8* %0)
%20 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 3
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 0
store i32 %8, i32* %21
%22 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 1
store i8* %0, i8** %22
%23 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 2
store i64 0, i64* %23
%24 = call i64 @aio_write(%struct.aiocb* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %13
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %34
28: ; preds = %13, %28
%29 = call i64 @aio_error(%struct.aiocb* %2)
%30 = load i64, i64* @EINPROGRESS
%31 = icmp eq i64 %29, %30
br i1 %31, label %28, label %32
32: ; preds = %28
%33 = call i32 @close(i32 %8)
br label %34
34: ; preds = %32, %26
%35 = phi i32 [ -1, %26 ], [ 0, %32 ]
ret i32 %35
}