#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[111];
  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_1_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("aio_read/3-1.c"
           " Error at open(): %s\n",
           strerror(errno));
    exit(2);
  }
  unlink(tmpfname);
  if (write(fd, buf, 111) != 111) {
    printf("aio_read/3-1.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 = 111;
  if (aio_read(&aiocb) == -1) {
    printf("aio_read/3-1.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-1.c"
           " Error at aio_error()\n");
    exit(1);
  }
  if (aio_return(&aiocb) != 111) {
    printf("aio_read/3-1.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    $0x1b0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xdf7(%rip),%rax
mov    %rax,-0x1a8(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10f0 <sysconf@plt>
cmp    $0x30db0,%rax
jge    1236 <main+0x46>
movl   $0x4,-0x4(%rbp)
jmp    145e <main+0x26e>
lea    -0x110(%rbp),%rax
mov    %rax,-0x1b0(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x1b0(%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,-0x184(%rbp)
cmpl   $0xffffffff,-0x184(%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    -0x184(%rbp),%edi
lea    -0x180(%rbp),%rsi
mov    $0x6f,%edx
call   1040 <write@plt>
cmp    $0x6f,%eax
je     133b <main+0x14b>
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   1100 <exit@plt>
lea    -0x1a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1090 <memset@plt>
mov    -0x184(%rbp),%eax
mov    %eax,-0x1a0(%rbp)
lea    -0x180(%rbp),%rax
mov    %rax,-0x198(%rbp)
movl   $0x6f,-0x190(%rbp)
lea    -0x1a0(%rbp),%rdi
call   10b0 <aio_read@plt>
cmp    $0xffffffff,%eax
jne    13b0 <main+0x1c0>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
lea    0xccb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
jmp    13b5 <main+0x1c5>
lea    -0x1a8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <nanosleep@plt>
lea    -0x1a0(%rbp),%rdi
call   10c0 <aio_error@plt>
mov    %eax,-0x188(%rbp)
mov    -0x188(%rbp),%eax
mov    0x2bf4(%rip),%rcx
cmp    (%rcx),%eax
je     13b5 <main+0x1c5>
cmpl   $0x0,-0x188(%rbp)
je     1411 <main+0x221>
lea    0xc92(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x1a0(%rbp),%rdi
call   10d0 <aio_return@plt>
cmp    $0x6f,%eax
je     143e <main+0x24e>
lea    0xc8a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x184(%rbp),%edi
call   10a0 <close@plt>
lea    0xc8d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x198,%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     1367 <main+0x167>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xdb4(%rip),%rdx
lea    0x90(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2d86(%rip),%rax
mov    0x2d67(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d34(%rip),%rax
or     (%rax),%esi
mov    0x2d1b(%rip),%rax
mov    0x2d34(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     1375 <main+0x175>
mov    %eax,%r15d
lea    0x90(%rsp),%rdi
call   1030 <unlink@plt>
lea    0x20(%rsp),%r14
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x6f,%edx
call   1050 <write@plt>
cmp    $0x6f,%eax
jne    138c <main+0x18c>
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   $0x6f,0x18(%rsp)
mov    %rbx,%rdi
call   10c0 <aio_read@plt>
cmp    $0xffffffff,%eax
je     13b5 <main+0x1b5>
mov    %rsp,%r14
lea    0x8(%rsp),%rbp
mov    0x2cbb(%rip),%rbx
nopl   (%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    13de <main+0x1de>
lea    0x8(%rsp),%rdi
call   10e0 <aio_return@plt>
cmp    $0x6f,%eax
jne    13e7 <main+0x1e7>
mov    %r15d,%edi
call   10b0 <close@plt>
lea    0xd2a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc8f(%rip),%rdi
jmp    13a1 <main+0x1a1>
mov    0x2c1d(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc9c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   1110 <exit@plt>
mov    0x2bf4(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc98(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xcd6(%rip),%rdi
jmp    13ee <main+0x1ee>
lea    0xca8(%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_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 = [36 x i8] c"aio_read/3-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"aio_read/3-1.c Error at write(): %s\0A\00"
@.str.3 = [40 x i8] c"aio_read/3-1.c Error at aio_read(): %s\0A\00"
@EINPROGRESS = global i32 0
@.str.4 = [37 x i8] c"aio_read/3-1.c Error at aio_error()\0A\00"
@.str.5 = [38 x i8] c"aio_read/3-1.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 [111 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 [111 x i8], [111 x i8]* %3, i64 0, i64 0
%41 = call i32 @write(i32 %39, i8* %40, i32 111)
%42 = icmp ne i32 %41, 111
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 [111 x i8], [111 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 111, 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, 111
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_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 = [36 x i8] c"aio_read/3-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"aio_read/3-1.c Error at write(): %s\0A\00"
@.str.3 = [40 x i8] c"aio_read/3-1.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-1.c Error at aio_return()\00"
@str.8 = [36 x i8] c"aio_read/3-1.c Error at aio_error()\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [111 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 [111 x i8], [111 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 111)
%35 = icmp eq i32 %34, 111
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 111, 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, 111
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
}