#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 int /*<<< orphan*/ tmpfname;
struct timespec {
  int member_1;
  int /*<<< orphan*/ member_0;
};
struct aiocb {
  int aio_fildes;
  char *aio_buf;
  int aio_nbytes;
};

/* Variables and functions */
int EINPROGRESS;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _SC_ASYNCHRONOUS_IO;
int aio_error(struct aiocb *);
int aio_return(struct aiocb *);
int aio_write(struct aiocb *);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ memset(struct aiocb *, int /*<<< orphan*/, int);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int);
long sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char tmpfname[256];
  char buf[512];
  int fd;
  struct aiocb aiocb;
  int err;
  int ret;
  struct timespec completion_wait_ts = {0, 10000000};
  if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
    return 4;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_write_5_1_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("aio_write/5-1.c"
           " Error at open(): %s\n",
           strerror(errno));
    exit(2);
  }
  unlink(tmpfname);
  memset(&aiocb, 0, sizeof(struct aiocb));
  aiocb.aio_fildes = fd;
  aiocb.aio_buf = buf;
  aiocb.aio_nbytes = 512;
  if (aio_write(&aiocb) == -1) {
    printf("aio_write/5-1.c"
           " Error at aio_write(): %s\n",
           strerror(errno));
    exit(1);
  }
  do {
    nanosleep(&completion_wait_ts, NULL);
    err = aio_error(&aiocb);
  } while (err == EINPROGRESS);
  ret = aio_return(&aiocb);
  if (err != 0) {
    printf("aio_write/5-1.c"
           " Error at aio_error() : %s\n",
           strerror(err));
    close(fd);
    exit(1);
  }
  if (ret != 512) {
    printf("aio_write/5-1.c"
           " Error at aio_return()\n");
    close(fd);
    exit(1);
  }
  close(fd);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe07(%rip),%rax
mov    %rax,-0x340(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10e0 <sysconf@plt>
cmp    $0x30db0,%rax
jge    1226 <main+0x46>
movl   $0x4,-0x4(%rbp)
jmp    1436 <main+0x256>
lea    -0x110(%rbp),%rax
mov    %rax,-0x348(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x348(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdb8(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    0x2d68(%rip),%rax
or     (%rax),%esi
mov    0x2d37(%rip),%rax
or     (%rax),%esi
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
mov    0x2d35(%rip),%rax
or     (%rax),%edx
call   10d0 <open@plt>
mov    %eax,-0x314(%rbp)
cmpl   $0xffffffff,-0x314(%rbp)
jne    12d6 <main+0xf6>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    %rax,%rsi
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x330(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1090 <memset@plt>
mov    -0x314(%rbp),%eax
mov    %eax,-0x330(%rbp)
lea    -0x310(%rbp),%rax
mov    %rax,-0x328(%rbp)
movl   $0x200,-0x320(%rbp)
lea    -0x330(%rbp),%rdi
call   1050 <aio_write@plt>
cmp    $0xffffffff,%eax
jne    1357 <main+0x177>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    %rax,%rsi
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    135c <main+0x17c>
lea    -0x340(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <nanosleep@plt>
lea    -0x330(%rbp),%rdi
call   10b0 <aio_error@plt>
mov    %eax,-0x334(%rbp)
mov    -0x334(%rbp),%eax
mov    0x2c4d(%rip),%rcx
cmp    (%rcx),%eax
je     135c <main+0x17c>
lea    -0x330(%rbp),%rdi
call   10c0 <aio_return@plt>
mov    %eax,-0x338(%rbp)
cmpl   $0x0,-0x334(%rbp)
je     13e3 <main+0x203>
mov    -0x334(%rbp),%edi
call   1100 <strerror@plt>
mov    %rax,%rsi
lea    0xcaa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x314(%rbp),%edi
call   10a0 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
cmpl   $0x200,-0x338(%rbp)
je     1416 <main+0x236>
lea    0xca2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x314(%rbp),%edi
call   10a0 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x314(%rbp),%edi
call   10a0 <close@plt>
lea    0xc9b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x320,%rsp
movabs $0x98968000000000,%rax
mov    %rax,(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10f0 <sysconf@plt>
mov    %rax,%rcx
mov    $0x4,%eax
cmp    $0x30db0,%rcx
jl     134b <main+0x15b>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdc2(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2d97(%rip),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d45(%rip),%rax
or     (%rax),%esi
mov    0x2d2c(%rip),%rax
mov    0x2d45(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10e0 <open@plt>
cmp    $0xffffffff,%eax
je     135b <main+0x16b>
mov    %eax,%r14d
lea    0x20(%rsp),%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10a0 <memset@plt>
mov    %r14d,0x8(%rsp)
lea    0x120(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x200,0x18(%rsp)
mov    %rbx,%rdi
call   1060 <aio_write@plt>
cmp    $0xffffffff,%eax
je     1384 <main+0x194>
mov    %rsp,%r15
lea    0x8(%rsp),%r12
mov    0x2ce5(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <nanosleep@plt>
mov    %r12,%rdi
call   10c0 <aio_error@plt>
cmp    (%rbx),%eax
je     1300 <main+0x110>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
call   10d0 <aio_return@plt>
test   %ebp,%ebp
jne    13ad <main+0x1bd>
cmp    $0x200,%eax
jne    13c7 <main+0x1d7>
mov    %r14d,%edi
call   10b0 <close@plt>
lea    0xd50(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x320,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xcaa(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   1100 <exit@plt>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xca6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    %ebp,%edi
call   1110 <strerror@plt>
lea    0xcae(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    13d3 <main+0x1e3>
lea    0xcd2(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
call   10b0 <close@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.aiocb = type { i32, i8*, i32 }
@__const.main.completion_wait_ts = %struct.timespec { i32 0, i32 10000000 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [26 x i8] c"/tmp/pts_aio_write_5_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [37 x i8] c"aio_write/5-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [42 x i8] c"aio_write/5-1.c Error at aio_write(): %s\0A\00"
@EINPROGRESS = global i32 0
@.str.3 = [43 x i8] c"aio_write/5-1.c Error at aio_error() : %s\0A\00"
@.str.4 = [39 x i8] c"aio_write/5-1.c Error at aio_return()\0A\00"
@.str.5 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [512 x i8]
%4 = alloca i32
%5 = alloca %struct.aiocb
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
store i32 0, i32* %1
%9 = bitcast %struct.timespec* %8 to i8*
%10 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%11 = call i64 @sysconf(i32 %10)
%12 = icmp slt i64 %11, 200112
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 4, i32* %1
br label %84
14: ; preds = %0
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%16 = call i32 (...) @getpid()
%17 = call i32 @snprintf(i8* %15, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%19 = call i32 @unlink(i8* %18)
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = load i32, i32* @O_EXCL
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %20, i32 %25, i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %14
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 2)
unreachable
37: ; preds = %14
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%39 = call i32 @unlink(i8* %38)
%40 = call i32 @memset(%struct.aiocb* %5, i32 0, i32 24)
%41 = load i32, i32* %4
%42 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 0
store i32 %41, i32* %42
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%44 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 1
store i8* %43, i8** %44
%45 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 2
store i32 512, i32* %45
%46 = call i32 @aio_write(%struct.aiocb* %5)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %37
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %37
br label %54
54: ; preds = %57, %53
%55 = call i32 @nanosleep(%struct.timespec* %8, i32* null)
%56 = call i32 @aio_error(%struct.aiocb* %5)
store i32 %56, i32* %6
br label %57
57: ; preds = %54
%58 = load i32, i32* %6
%59 = load i32, i32* @EINPROGRESS
%60 = icmp eq i32 %58, %59
br i1 %60, label %54, label %61
61: ; preds = %57
%62 = call i32 @aio_return(%struct.aiocb* %5)
store i32 %62, i32* %7
%63 = load i32, i32* %6
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %61
%66 = load i32, i32* %6
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %67)
%69 = load i32, i32* %4
%70 = call i32 @close(i32 %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %61
%73 = load i32, i32* %7
%74 = icmp ne i32 %73, 512
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%77 = load i32, i32* %4
%78 = call i32 @close(i32 %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %72
%81 = load i32, i32* %4
%82 = call i32 @close(i32 %81)
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %84
84: ; preds = %80, %13
%85 = load i32, i32* %1
ret i32 %85
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i8*, i32 }
%struct.timespec = type { i32, i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [26 x i8] c"/tmp/pts_aio_write_5_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [37 x i8] c"aio_write/5-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [42 x i8] c"aio_write/5-1.c Error at aio_write(): %s\0A\00"
@EINPROGRESS = global i32 0
@.str.3 = [43 x i8] c"aio_write/5-1.c Error at aio_error() : %s\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.6 = [38 x i8] c"aio_write/5-1.c Error at aio_return()\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [512 x i8]
%3 = alloca %struct.aiocb
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timespec*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%8 = bitcast %struct.aiocb* %3 to i8*
%9 = bitcast i64* %4 to i8*
store i64 42949672960000000, i64* %4
%10 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%11 = tail call i64 @sysconf(i32 %10)
%12 = icmp slt i64 %11, 200112
br i1 %12, label %67, label %13
13: ; preds = %0
%14 = tail call i32 (...) @getpid()
%15 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @unlink(i8* %6)
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_RDWR
%19 = or i32 %18, %17
%20 = load i32, i32* @O_EXCL
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %23, %22
%25 = call i32 @open(i8* %6, i32 %21, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %13
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %13
%33 = call i32 @unlink(i8* %6)
%34 = call i32 @memset(%struct.aiocb* %3, i32 0, i32 24)
%35 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 0
store i32 %25, i32* %35
%36 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 1
store i8* %7, i8** %36
%37 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 2
store i32 512, i32* %37
%38 = call i32 @aio_write(%struct.aiocb* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %32
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %32, %45
%46 = call i32 @nanosleep(%struct.timespec* %5, i32* null)
%47 = call i32 @aio_error(%struct.aiocb* %3)
%48 = load i32, i32* @EINPROGRESS
%49 = icmp eq i32 %47, %48
br i1 %49, label %45, label %50
50: ; preds = %45
%51 = call i32 @aio_return(%struct.aiocb* %3)
%52 = icmp eq i32 %47, 0
br i1 %52, label %58, label %53
53: ; preds = %50
%54 = call i8* @strerror(i32 %47)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %54)
%56 = call i32 @close(i32 %25)
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %50
%59 = icmp eq i32 %51, 512
br i1 %59, label %64, label %60
60: ; preds = %58
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
%62 = call i32 @close(i32 %25)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %58
%65 = call i32 @close(i32 %25)
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %67
67: ; preds = %0, %64
%68 = phi i32 [ 0, %64 ], [ 4, %0 ]
ret i32 %68
}