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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x540,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xdf7(%rip),%rax
mov    %rax,-0x538(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10f0 <sysconf@plt>
cmp    $0x30db0,%rax
jge    1236 <main+0x46>
movl   $0x4,-0x4(%rbp)
jmp    1462 <main+0x272>
lea    -0x110(%rbp),%rax
mov    %rax,-0x540(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x540(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xda8(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    0x2d58(%rip),%rax
or     (%rax),%esi
mov    0x2d27(%rip),%rax
or     (%rax),%esi
mov    0x2d0e(%rip),%rax
mov    (%rax),%edx
mov    0x2d25(%rip),%rax
or     (%rax),%edx
call   10e0 <open@plt>
mov    %eax,-0x514(%rbp)
cmpl   $0xffffffff,-0x514(%rbp)
jne    12e6 <main+0xf6>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   1100 <exit@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x514(%rbp),%edi
lea    -0x510(%rbp),%rsi
mov    $0x200,%edx
call   1040 <write@plt>
cmp    $0x200,%eax
je     133d <main+0x14d>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   1100 <exit@plt>
lea    -0x530(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1090 <memset@plt>
mov    -0x514(%rbp),%eax
mov    %eax,-0x530(%rbp)
lea    -0x510(%rbp),%rax
mov    %rax,-0x528(%rbp)
movl   $0x400,-0x520(%rbp)
lea    -0x530(%rbp),%rdi
call   10b0 <aio_read@plt>
cmp    $0xffffffff,%eax
jne    13b2 <main+0x1c2>
mov    0x2c20(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
lea    0xcc9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
jmp    13b7 <main+0x1c7>
lea    -0x538(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <nanosleep@plt>
lea    -0x530(%rbp),%rdi
call   10c0 <aio_error@plt>
mov    %eax,-0x518(%rbp)
mov    -0x518(%rbp),%eax
mov    0x2bf2(%rip),%rcx
cmp    (%rcx),%eax
je     13b7 <main+0x1c7>
cmpl   $0x0,-0x518(%rbp)
je     1413 <main+0x223>
lea    0xc90(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x530(%rbp),%rdi
call   10d0 <aio_return@plt>
cmp    $0x200,%eax
je     1442 <main+0x252>
lea    0xc86(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x514(%rbp),%edi
call   10a0 <close@plt>
lea    0xc89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x540,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x528,%rsp
movabs $0x98968000000000,%rax
mov    %rax,(%rsp)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1100 <sysconf@plt>
mov    %rax,%rcx
mov    $0x4,%eax
cmp    $0x30db0,%rcx
jl     1369 <main+0x169>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xdb4(%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    0x2d89(%rip),%rax
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d37(%rip),%rax
or     (%rax),%esi
mov    0x2d1e(%rip),%rax
mov    0x2d37(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     1377 <main+0x177>
mov    %eax,%r15d
lea    0x20(%rsp),%rdi
call   1030 <unlink@plt>
lea    0x120(%rsp),%r14
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1050 <write@plt>
cmp    $0x200,%eax
jne    138e <main+0x18e>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10a0 <memset@plt>
mov    %r15d,0x8(%rsp)
mov    %r14,0x10(%rsp)
movl   $0x400,0x18(%rsp)
mov    %rbx,%rdi
call   10c0 <aio_read@plt>
cmp    $0xffffffff,%eax
je     13b7 <main+0x1b7>
mov    %rsp,%r14
lea    0x8(%rsp),%rbp
mov    0x2cbc(%rip),%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <nanosleep@plt>
mov    %rbp,%rdi
call   10d0 <aio_error@plt>
cmp    (%rbx),%eax
je     1320 <main+0x120>
test   %eax,%eax
jne    13e0 <main+0x1e0>
lea    0x8(%rsp),%rdi
call   10e0 <aio_return@plt>
cmp    $0x200,%eax
jne    13e9 <main+0x1e9>
mov    %r15d,%edi
call   10b0 <close@plt>
lea    0xd28(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x528,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c32(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc8d(%rip),%rdi
jmp    13a3 <main+0x1a3>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc9a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   1110 <exit@plt>
mov    0x2bf2(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc96(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xcd4(%rip),%rdi
jmp    13f0 <main+0x1f0>
lea    0xca6(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1110 <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 = [25 x i8] c"/tmp/pts_aio_read_3_2_%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 = [36 x i8] c"aio_read/3-2.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"aio_read/3-2.c Error at write(): %s\0A\00"
@.str.3 = [40 x i8] c"aio_read/3-2.c Error at aio_read(): %s\0A\00"
@EINPROGRESS = global i32 0
@.str.4 = [37 x i8] c"aio_read/3-2.c Error at aio_error()\0A\00"
@.str.5 = [38 x i8] c"aio_read/3-2.c Error at aio_return()\0A\00"
@.str.6 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.aiocb
%7 = alloca %struct.timespec
store i32 0, i32* %1
%8 = bitcast %struct.timespec* %7 to i8*
%9 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%10 = call i64 @sysconf(i32 %9)
%11 = icmp slt i64 %10, 200112
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 4, i32* %1
br label %86
13: ; preds = %0
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%15 = call i32 (...) @getpid()
%16 = call i32 @snprintf(i8* %14, i32 256, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%18 = call i32 @unlink(i8* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_RDWR
%22 = or i32 %20, %21
%23 = load i32, i32* @O_EXCL
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %19, i32 %24, i32 %27)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %13
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %13
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%38 = call i32 @unlink(i8* %37)
%39 = load i32, i32* %4
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%41 = call i32 @write(i32 %39, i8* %40, i32 512)
%42 = icmp ne i32 %41, 512
br i1 %42, label %43, label %48
43: ; preds = %36
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 2)
unreachable
48: ; preds = %36
%49 = call i32 @memset(%struct.aiocb* %6, i32 0, i32 24)
%50 = load i32, i32* %4
%51 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %6, i32 0, i32 0
store i32 %50, i32* %51
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%53 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %6, i32 0, i32 1
store i8* %52, i8** %53
%54 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %6, i32 0, i32 2
store i32 1024, i32* %54
%55 = call i32 @aio_read(%struct.aiocb* %6)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %62
57: ; preds = %48
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %48
br label %63
63: ; preds = %66, %62
%64 = call i32 @nanosleep(%struct.timespec* %7, i32* null)
%65 = call i32 @aio_error(%struct.aiocb* %6)
store i32 %65, i32* %5
br label %66
66: ; preds = %63
%67 = load i32, i32* %5
%68 = load i32, i32* @EINPROGRESS
%69 = icmp eq i32 %67, %68
br i1 %69, label %63, label %70
70: ; preds = %66
%71 = load i32, i32* %5
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %70
%77 = call i32 @aio_return(%struct.aiocb* %6)
%78 = icmp ne i32 %77, 512
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %76
%83 = load i32, i32* %4
%84 = call i32 @close(i32 %83)
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %86
86: ; preds = %82, %12
%87 = load i32, i32* %1
ret i32 %87
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i8*, i32 }
%struct.timespec = type { i32, i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [25 x i8] c"/tmp/pts_aio_read_3_2_%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 = [36 x i8] c"aio_read/3-2.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"aio_read/3-2.c Error at write(): %s\0A\00"
@.str.3 = [40 x i8] c"aio_read/3-2.c Error at aio_read(): %s\0A\00"
@EINPROGRESS = global i32 0
@str = [12 x i8] c"Test PASSED\00"
@str.7 = [37 x i8] c"aio_read/3-2.c Error at aio_return()\00"
@str.8 = [36 x i8] c"aio_read/3-2.c Error at aio_error()\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [1024 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 [1024 x i8], [1024 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 %72, label %13
13: ; preds = %0
%14 = tail call i32 (...) @getpid()
%15 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([25 x i8], [25 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 ([36 x i8], [36 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 @write(i32 %25, i8* %7, i32 512)
%35 = icmp eq i32 %34, 512
br i1 %35, label %41, label %36
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %32
%42 = call i32 @memset(%struct.aiocb* %3, i32 0, i32 24)
%43 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 0
store i32 %25, i32* %43
%44 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 1
store i8* %7, i8** %44
%45 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 2
store i32 1024, i32* %45
%46 = call i32 @aio_read(%struct.aiocb* %3)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %41
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %41, %53
%54 = call i32 @nanosleep(%struct.timespec* %5, i32* null)
%55 = call i32 @aio_error(%struct.aiocb* %3)
%56 = load i32, i32* @EINPROGRESS
%57 = icmp eq i32 %55, %56
br i1 %57, label %53, label %58
58: ; preds = %53
%59 = icmp eq i32 %55, 0
br i1 %59, label %63, label %60
60: ; preds = %58
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %58
%64 = call i32 @aio_return(%struct.aiocb* %3)
%65 = icmp eq i32 %64, 512
br i1 %65, label %69, label %66
66: ; preds = %63
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.7, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %63
%70 = call i32 @close(i32 %25)
%71 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %72
72: ; preds = %0, %69
%73 = phi i32 [ 0, %69 ], [ 4, %0 ]
ret i32 %73
}