#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;
  size_t aio_nbytes;
  TYPE_1__ aio_sigevent;
  void *aio_buf;
  scalar_t__ aio_offset;
};
typedef int ssize_t;

/* Variables and functions */
int EINPROGRESS;
int /*<<< orphan*/ SIGEV_NONE;
int aio_error(struct aiocb *);
int aio_return(struct aiocb *);
int aio_write(struct aiocb *);
int /*<<< orphan*/ bzero(struct aiocb *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
ssize_t fd_aio_write(int fd, void *buf, size_t len) {
  ssize_t retval;
  ssize_t nbytes;
  struct aiocb myaiocb;
  bzero(&myaiocb, sizeof(struct aiocb));
  myaiocb.aio_fildes = fd;
  myaiocb.aio_offset = 0;
  myaiocb.aio_buf = buf;
  myaiocb.aio_nbytes = len;
  myaiocb.aio_sigevent.sigev_notify = SIGEV_NONE;
  retval = aio_write(&myaiocb);
  if (retval == -1) {
    perror("aio_write:");
    exit(1);
  }
  while ((retval = aio_error(&myaiocb)) == EINPROGRESS)
    ;
  int err = aio_error(&myaiocb);
  if (err != 0)
    printf("aio_write err : %s\n", strerror(err));
  nbytes = aio_return(&myaiocb);
  printf("aio_read nbytes %d err %d retval %d\n", nbytes, err, retval);
  return nbytes;
}


x86 assembly (clang, -O0):
<fd_aio_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x48(%rbp),%rdi
mov    $0x28,%esi
call   1080 <bzero@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
lea    -0x48(%rbp),%rdi
call   1030 <aio_write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f7 <fd_aio_write+0x77>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11fc <fd_aio_write+0x7c>
lea    -0x48(%rbp),%rdi
call   1050 <aio_error@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%eax
jne    121c <fd_aio_write+0x9c>
jmp    11fc <fd_aio_write+0x7c>
lea    -0x48(%rbp),%rdi
call   1050 <aio_error@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     124b <fd_aio_write+0xcb>
mov    -0x4c(%rbp),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x48(%rbp),%rdi
call   1060 <aio_return@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x4c(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_aio_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x28,%esi
call   1080 <bzero@plt>
mov    %ebx,(%rsp)
movq   $0x0,0x20(%rsp)
mov    %rbp,0x18(%rsp)
mov    %r14,0x8(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
call   1030 <aio_write@plt>
cmp    $0xffffffff,%eax
je     1243 <fd_aio_write+0xc3>
mov    %rsp,%r14
mov    0x2e04(%rip),%rbp
nopl   0x0(%rax)
mov    %r14,%rdi
call   1050 <aio_error@plt>
cmp    0x0(%rbp),%eax
je     11e0 <fd_aio_write+0x60>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1050 <aio_error@plt>
mov    %eax,%r14d
test   %eax,%eax
je     1217 <fd_aio_write+0x97>
mov    %r14d,%edi
call   10a0 <strerror@plt>
lea    0xdfe(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%rdi
call   1060 <aio_return@plt>
mov    %eax,%ebp
lea    0xdf7(%rip),%rdi
mov    %eax,%esi
mov    %r14d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.aiocb = type { i32, i64, %struct.TYPE_2__, i8*, i64 }
%struct.TYPE_2__ = type { i32 }
@SIGEV_NONE = global i32 0
@.str = [11 x i8] c"aio_write:\00"
@EINPROGRESS = global i32 0
@.str.1 = [20 x i8] c"aio_write err : %s\0A\00"
@.str.2 = [37 x i8] c"aio_read nbytes %d err %d retval %d\0A\00"
define i32 @fd_aio_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.aiocb
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = call i32 @bzero(%struct.aiocb* %9, i32 40)
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %9, i32 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %9, i32 0, i32 4
store i64 0, i64* %14
%15 = load i8*, i8** %5
%16 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %9, i32 0, i32 3
store i8* %15, i8** %16
%17 = load i64, i64* %6
%18 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %9, i32 0, i32 1
store i64 %17, i64* %18
%19 = load i32, i32* @SIGEV_NONE
%20 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %9, i32 0, i32 2
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @aio_write(%struct.aiocb* %9)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %3
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %3
br label %29
29: ; preds = %33, %28
%30 = call i32 @aio_error(%struct.aiocb* %9)
store i32 %30, i32* %7
%31 = load i32, i32* @EINPROGRESS
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %29
34: ; preds = %29
%35 = call i32 @aio_error(%struct.aiocb* %9)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* %10
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %38, %34
%43 = call i32 @aio_return(%struct.aiocb* %9)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = load i32, i32* %10
%46 = load i32, i32* %7
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* %8
ret i32 %48
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i64, %struct.TYPE_2__, i8*, i64 }
%struct.TYPE_2__ = type { i32 }
@SIGEV_NONE = global i32 0
@.str = [11 x i8] c"aio_write:\00"
@EINPROGRESS = global i32 0
@.str.1 = [20 x i8] c"aio_write err : %s\0A\00"
@.str.2 = [37 x i8] c"aio_read nbytes %d err %d retval %d\0A\00"
define i32 @fd_aio_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.aiocb
%5 = bitcast %struct.aiocb* %4 to i8*
%6 = call i32 @bzero(%struct.aiocb* %4, i32 40)
%7 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %4, i64 0, i32 0
store i32 %0, i32* %7
%8 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %4, i64 0, i32 4
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %4, i64 0, i32 3
store i8* %1, i8** %9
%10 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %4, i64 0, i32 1
store i64 %2, i64* %10
%11 = load i32, i32* @SIGEV_NONE
%12 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %4, i64 0, i32 2, i32 0
store i32 %11, i32* %12
%13 = call i32 @aio_write(%struct.aiocb* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3, %18
%19 = call i32 @aio_error(%struct.aiocb* %4)
%20 = load i32, i32* @EINPROGRESS
%21 = icmp eq i32 %19, %20
br i1 %21, label %18, label %22
22: ; preds = %18
%23 = call i32 @aio_error(%struct.aiocb* %4)
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = call i8* @strerror(i32 %23)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %22
%29 = call i32 @aio_return(%struct.aiocb* %4)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %23, i32 %19)
ret i32 %29
}