#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 */

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fsync(int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(scalar_t__);
int main(void) {
  int fd;
  fd = -1;
  if (fsync(fd) == -1 && errno == EBADF) {
    printf("Got EBADF when fd=-1\n");
    printf("Test PASSED\n");
    exit(0);
  } else {
    printf("Test FAILED: Expect EBADF, get: %s\n", strerror(errno));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <fsync@plt>
cmp    $0xffffffff,%eax
jne    11a4 <main+0x64>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
jne    11a4 <main+0x64>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    $0xffffffff,%edi
call   1050 <fsync@plt>
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%rdi
cmp    $0xffffffff,%eax
jne    1195 <main+0x45>
mov    0x2e67(%rip),%rax
cmp    (%rax),%rdi
jne    1195 <main+0x45>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>
call   1070 <strerror@plt>
lea    0xe5f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EBADF = global i64 0
@.str = [22 x i8] c"Got EBADF when fd=-1\0A\00"
@.str.1 = [13 x i8] c"Test PASSED\0A\00"
@.str.2 = [36 x i8] c"Test FAILED: Expect EBADF, get: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %2
%3 = load i32, i32* %2
%4 = call i32 @fsync(i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %14
6: ; preds = %0
%7 = load i64, i64* @errno
%8 = load i64, i64* @EBADF
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %6, %0
%15 = load i64, i64* @errno
%16 = call i8* @strerror(i64 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@errno = global i64 0
@EBADF = global i64 0
@.str.2 = [36 x i8] c"Test FAILED: Expect EBADF, get: %s\0A\00"
@str = [21 x i8] c"Got EBADF when fd=-1\00"
@str.3 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = tail call i32 @fsync(i32 -1)
%2 = icmp eq i32 %1, -1
%3 = load i64, i64* @errno
%4 = load i64, i64* @EBADF
%5 = icmp eq i64 %3, %4
%6 = select i1 %2, i1 %5, i1 false
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.3, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = tail call i8* @strerror(i64 %3)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
}