#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 aiocb {
  int aio_fildes;
  char *aio_buf;
  int aio_reqprio;
  int aio_nbytes;
};

/* Variables and functions */
int EINVAL;
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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ memset(struct aiocb *, int /*<<< orphan*/, int);
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 main(void) {
  char tmpfname[256];
  char buf[512];
  int fd;
  struct aiocb aiocb;
  int ret = 0;
  if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
    return 4;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_error_3_1_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("aio_error/3-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_reqprio = -1;
  aiocb.aio_nbytes = 512;
  ret = aio_error(&aiocb);
  if (ret != EINVAL) {
    printf("aio_error/3-1.c"
           " return code didn't match expected "
           "value (%d != %d).\n",
           ret, EINVAL);
    return 5;
  }
  close(fd);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x334(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
cmp    $0x30db0,%rax
jge    11f2 <main+0x42>
movl   $0x4,-0x4(%rbp)
jmp    135f <main+0x1af>
lea    -0x110(%rbp),%rax
mov    %rax,-0x340(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x340(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xde4(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2d9c(%rip),%rax
or     (%rax),%esi
mov    0x2d73(%rip),%rax
or     (%rax),%esi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
mov    0x2d71(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x314(%rbp)
cmpl   $0xffffffff,-0x314(%rbp)
jne    12a2 <main+0xf2>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x330(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    -0x314(%rbp),%eax
mov    %eax,-0x330(%rbp)
lea    -0x310(%rbp),%rax
mov    %rax,-0x328(%rbp)
movl   $0xffffffff,-0x320(%rbp)
ff ff ff
movl   $0x200,-0x31c(%rbp)
lea    -0x330(%rbp),%rdi
call   1090 <aio_error@plt>
mov    %eax,-0x334(%rbp)
mov    -0x334(%rbp),%eax
mov    0x2c9a(%rip),%rcx
cmp    (%rcx),%eax
je     133f <main+0x18f>
mov    -0x334(%rbp),%esi
mov    0x2c85(%rip),%rax
mov    (%rax),%edx
lea    0xd13(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    135f <main+0x1af>
mov    -0x314(%rbp),%edi
call   1080 <close@plt>
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x320,%rsp
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    $0x4,%ebx
cmp    $0x30db0,%rax
jl     12d3 <main+0x113>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe08(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2ddd(%rip),%rax
mov    0x2dbe(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    0x2d7a(%rip),%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12e1 <main+0x121>
mov    %eax,%ebp
lea    0x20(%rsp),%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1080 <memset@plt>
mov    %ebp,0x8(%rsp)
lea    0x120(%rsp),%rax
mov    %rax,0x10(%rsp)
movabs $0x200ffffffff,%rax
mov    %rax,0x18(%rsp)
mov    %r14,%rdi
call   10a0 <aio_error@plt>
mov    0x2d05(%rip),%rcx
mov    (%rcx),%edx
cmp    %edx,%eax
jne    12be <main+0xfe>
mov    %ebp,%edi
call   1090 <close@plt>
lea    0xdcd(%rip),%rdi
call   1040 <puts@plt>
jmp    12d3 <main+0x113>
lea    0xd7a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x5,%ebx
mov    %ebx,%eax
add    $0x320,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd24(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.aiocb = type { i32, i8*, i32, i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [26 x i8] c"/tmp/pts_aio_error_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 = [37 x i8] c"aio_error/3-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [69 x i8] c"aio_error/3-1.c return code didn't match expected value (%d != %d).\0A\00"
@.str.3 = [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
store i32 0, i32* %1
store i32 0, i32* %6
%7 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%8 = call i64 @sysconf(i32 %7)
%9 = icmp slt i64 %8, 200112
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 4, i32* %1
br label %56
11: ; preds = %0
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%13 = call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %12, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%16 = call i32 @unlink(i8* %15)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_RDWR
%20 = or i32 %18, %19
%21 = load i32, i32* @O_EXCL
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %17, i32 %22, i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %11
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %11
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%36 = call i32 @unlink(i8* %35)
%37 = call i32 @memset(%struct.aiocb* %5, i32 0, i32 24)
%38 = load i32, i32* %4
%39 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 0
store i32 %38, i32* %39
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%41 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 1
store i8* %40, i8** %41
%42 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 2
store i32 -1, i32* %42
%43 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %5, i32 0, i32 3
store i32 512, i32* %43
%44 = call i32 @aio_error(%struct.aiocb* %5)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = load i32, i32* @EINVAL
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %34
%49 = load i32, i32* %6
%50 = load i32, i32* @EINVAL
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %50)
store i32 5, i32* %1
br label %56
52: ; preds = %34
%53 = load i32, i32* %4
%54 = call i32 @close(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %56
56: ; preds = %52, %48, %10
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i8*, i32, i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [26 x i8] c"/tmp/pts_aio_error_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 = [37 x i8] c"aio_error/3-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.2 = [69 x i8] c"aio_error/3-1.c return code didn't match expected value (%d != %d).\0A\00"
@str = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [512 x i8]
%3 = alloca %struct.aiocb
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.aiocb* %3 to i8*
%7 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%8 = tail call i64 @sysconf(i32 %7)
%9 = icmp slt i64 %8, 200112
br i1 %9, label %44, label %10
10: ; preds = %0
%11 = tail call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 @unlink(i8* %4)
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_RDWR
%16 = or i32 %15, %14
%17 = load i32, i32* @O_EXCL
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %20, %19
%22 = call i32 @open(i8* %4, i32 %18, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %10
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %10
%30 = call i32 @unlink(i8* %4)
%31 = call i32 @memset(%struct.aiocb* %3, i32 0, i32 24)
%32 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 0
store i32 %22, i32* %32
%33 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 1
store i8* %5, i8** %33
%34 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 2
store i32 -1, i32* %34
%35 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 3
store i32 512, i32* %35
%36 = call i32 @aio_error(%struct.aiocb* %3)
%37 = load i32, i32* @EINVAL
%38 = icmp eq i32 %36, %37
br i1 %38, label %41, label %39
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %37)
br label %44
41: ; preds = %29
%42 = call i32 @close(i32 %22)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %44
44: ; preds = %0, %41, %39
%45 = phi i32 [ 5, %39 ], [ 0, %41 ], [ 4, %0 ]
ret i32 %45
}