#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;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _SC_ASYNCHRONOUS_IO;
int aio_cancel(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int getpid();
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];
  int fd;
  if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
    return 4;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_cancel_2_2_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("aio_cancel/2-2.c"
           " Error at open(): %s\n",
           strerror(errno));
    return 2;
  }
  unlink(tmpfname);
  if (aio_cancel(fd, NULL) == -1) {
    printf("aio_cancel/2-2.c"
           " Error at aio_cancel(): %s\n",
           strerror(errno));
    return 1;
  }
  close(fd);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
cmp    $0x30db0,%rax
jge    11c8 <main+0x38>
movl   $0x4,-0x4(%rbp)
jmp    12e9 <main+0x159>
lea    -0x110(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x120(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe0e(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
mov    0x2d9d(%rip),%rax
or     (%rax),%esi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
mov    0x2d9b(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    127a <main+0xea>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e9 <main+0x159>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x114(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <aio_cancel@plt>
cmp    $0xffffffff,%eax
jne    12c9 <main+0x139>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <main+0x159>
mov    -0x114(%rbp),%edi
call   1080 <close@plt>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    $0x4,%ebx
cmp    $0x30db0,%rax
jl     12a3 <main+0x103>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe2a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e01(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
mov    0x2d9e(%rip),%rax
mov    0x2db7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0xb9>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1030 <unlink@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <aio_cancel@plt>
cmp    $0xffffffff,%eax
je     127f <main+0xdf>
mov    %ebp,%edi
call   1090 <close@plt>
lea    0xe1b(%rip),%rdi
call   1040 <puts@plt>
jmp    12a3 <main+0x103>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdad(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%ebx
jmp    12a3 <main+0x103>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdad(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [27 x i8] c"/tmp/pts_aio_cancel_2_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 = [38 x i8] c"aio_cancel/2-2.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [44 x i8] c"aio_cancel/2-2.c Error at aio_cancel(): %s\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 i32
store i32 0, i32* %1
%4 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%5 = call i64 @sysconf(i32 %4)
%6 = icmp slt i64 %5, 200112
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 4, i32* %1
br label %44
8: ; preds = %0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%10 = call i32 (...) @getpid()
%11 = call i32 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%13 = call i32 @unlink(i8* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_RDWR
%17 = or i32 %15, %16
%18 = load i32, i32* @O_EXCL
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %14, i32 %19, i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %8
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 2, i32* %1
br label %44
30: ; preds = %8
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%32 = call i32 @unlink(i8* %31)
%33 = load i32, i32* %3
%34 = call i32 @aio_cancel(i32 %33, i32* null)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 1, i32* %1
br label %44
40: ; preds = %30
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
%43 = 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 %44
44: ; preds = %40, %36, %26, %7
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [27 x i8] c"/tmp/pts_aio_cancel_2_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 = [38 x i8] c"aio_cancel/2-2.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [44 x i8] c"aio_cancel/2-2.c Error at aio_cancel(): %s\0A\00"
@str = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%4 = tail call i64 @sysconf(i32 %3)
%5 = icmp slt i64 %4, 200112
br i1 %5, label %35, label %6
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @unlink(i8* %2)
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %11, %10
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = call i32 @open(i8* %2, i32 %14, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %6
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %35
24: ; preds = %6
%25 = call i32 @unlink(i8* %2)
%26 = call i32 @aio_cancel(i32 %18, i32* null)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %35
32: ; preds = %24
%33 = call i32 @close(i32 %18)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %35
35: ; preds = %0, %32, %28, %20
%36 = phi i32 [ 2, %20 ], [ 1, %28 ], [ 0, %32 ], [ 4, %0 ]
ret i32 %36
}