#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 */
struct aiocb {
  int aio_fildes;
};

/* Variables and functions */
scalar_t__ EBADF;
int /*<<< orphan*/ O_SYNC;
int /*<<< orphan*/ _SC_ASYNCHRONOUS_IO;
int aio_fsync(int /*<<< orphan*/, struct aiocb *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct aiocb *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
long sysconf(int /*<<< orphan*/);
int main(void) {
  struct aiocb aiocb;
  if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
    return 4;
  memset(&aiocb, 0, sizeof(struct aiocb));
  aiocb.aio_fildes = -1;
  if (aio_fsync(O_SYNC, &aiocb) != -1) {
    printf("aio_fsync/12-1.c"
           " aio_fsync() accepts bad filedes\n");
    exit(1);
  }
  if (errno != EBADF) {
    printf("aio_fsync/12-1.c"
           " errno is not EBADF (%d)\n",
           errno);
    exit(1);
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
cmp    $0x30db0,%rax
jge    1185 <main+0x35>
movl   $0x4,-0x4(%rbp)
jmp    1220 <main+0xd0>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <aio_fsync@plt>
cmp    $0xffffffff,%eax
je     11cf <main+0x7f>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
je     120b <main+0xbb>
mov    0x2de0(%rip),%rax
mov    (%rax),%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,%rcx
mov    $0x4,%eax
cmp    $0x30db0,%rcx
jl     11da <main+0x7a>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0xffffffff,0x8(%rsp)
ff
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <aio_fsync@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x80>
mov    0x2e13(%rip),%rax
mov    (%rax),%rsi
mov    0x2e01(%rip),%rax
cmp    (%rax),%rsi
jne    11f6 <main+0x96>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe03(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.aiocb = type { i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@O_SYNC = global i32 0
@.str = [50 x i8] c"aio_fsync/12-1.c aio_fsync() accepts bad filedes\0A\00"
@errno = global i64 0
@EBADF = global i64 0
@.str.1 = [42 x i8] c"aio_fsync/12-1.c errno is not EBADF (%d)\0A\00"
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.aiocb
store i32 0, i32* %1
%3 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%4 = call i64 @sysconf(i32 %3)
%5 = icmp slt i64 %4, 200112
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 4, i32* %1
br label %26
7: ; preds = %0
%8 = call i32 @memset(%struct.aiocb* %2, i32 0, i32 4)
%9 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i32 0, i32 0
store i32 -1, i32* %9
%10 = load i32, i32* @O_SYNC
%11 = call i32 @aio_fsync(i32 %10, %struct.aiocb* %2)
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = load i64, i64* @errno
%18 = load i64, i64* @EBADF
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i64 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %26
26: ; preds = %24, %6
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
%struct.aiocb = type { i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@O_SYNC = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@.str.1 = [42 x i8] c"aio_fsync/12-1.c errno is not EBADF (%d)\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.3 = [49 x i8] c"aio_fsync/12-1.c aio_fsync() accepts bad filedes\00"
define i32 @main() {
%1 = alloca %struct.aiocb
%2 = bitcast %struct.aiocb* %1 to i8*
%3 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%4 = tail call i64 @sysconf(i32 %3)
%5 = icmp slt i64 %4, 200112
br i1 %5, label %24, label %6
6: ; preds = %0
%7 = call i32 @memset(%struct.aiocb* %1, i32 0, i32 4)
%8 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %1, i64 0, i32 0
store i32 -1, i32* %8
%9 = load i32, i32* @O_SYNC
%10 = call i32 @aio_fsync(i32 %9, %struct.aiocb* %1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %15, label %12
12: ; preds = %6
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.3, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %6
%16 = load i64, i64* @errno
%17 = load i64, i64* @EBADF
%18 = icmp eq i64 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %0, %22
%25 = phi i32 [ 0, %22 ], [ 4, %0 ]
ret i32 %25
}