#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 */
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fsync(int);
int handle_zng(int);
int /*<<< orphan*/ printf(char *, ...);
int fsync_test(int fd) {
  int ret, save_errno;
  int passed;
  printf("fsync(): fd=%d\n", fd);
  fflush(NULL);
  passed = handle_zng(ret = fsync(fd));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    printf("\t\tret = %d\n", ret);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %d\n",
           ret);
    fflush(NULL);
    break;
  default:
    printf("Internal syscalltester error: passed = %d\n", passed);
    fflush(NULL);
    break;
  }
  errno = save_errno;
  return ret;
}


x86 assembly (clang, -O0):
<fsync_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xeab(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x18(%rbp)
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x4(%rbp),%edi
call   1060 <fsync@plt>
mov    %eax,%edi
mov    %edi,-0x8(%rbp)
call   1030 <handle_zng@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xfffffffe,%eax
je     11df <fsync_test+0x9f>
jmp    119e <fsync_test+0x5e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     11fe <fsync_test+0xbe>
jmp    11b2 <fsync_test+0x72>
mov    -0x8(%rbp),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    1218 <fsync_test+0xd8>
mov    -0x8(%rbp),%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    1218 <fsync_test+0xd8>
mov    -0x10(%rbp),%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0xc(%rbp),%ecx
mov    0x2dbe(%rip),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsync_test>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
lea    0xea3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebx,%edi
call   1070 <fsync@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <handle_zng@plt>
mov    0x2e5c(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11ad <fsync_test+0x5d>
lea    0xe7a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
jmp    11cd <fsync_test+0x7d>
cmp    $0xfffffffe,%eax
jne    11bd <fsync_test+0x6d>
lea    0xe63(%rip),%rdi
mov    %ebx,%esi
jmp    11c6 <fsync_test+0x76>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"fsync(): fd=%d\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.2 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.3 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.4 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @fsync_test(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 @fflush(i32* null)
%9 = load i32, i32* %2
%10 = call i32 @fsync(i32 %9)
store i32 %10, i32* %3
%11 = call i32 @handle_zng(i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* @errno
store i32 %12, i32* %4
%13 = load i32, i32* %5
switch i32 %13, label %23 [
i32 -1, label %14
i32 0, label %14
i32 -2, label %19
]
14: ; preds = %1, %1
%15 = load i32, i32* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @fflush(i32* null)
br label %27
19: ; preds = %1
%20 = load i32, i32* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = call i32 @fflush(i32* null)
br label %27
23: ; preds = %1
%24 = load i32, i32* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = call i32 @fflush(i32* null)
br label %27
27: ; preds = %23, %19, %14
%28 = load i32, i32* %4
store i32 %28, i32* @errno
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [16 x i8] c"fsync(): fd=%d\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.3 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.4 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str = [19 x i8] c"\09Succeeded Phase 1\00"
define i32 @fsync_test(i32 %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call i32 @fflush(i32* null)
%4 = tail call i32 @fsync(i32 %0)
%5 = tail call i32 @handle_zng(i32 %4)
%6 = load i32, i32* @errno
switch i32 %5, label %12 [
i32 -1, label %7
i32 0, label %7
i32 -2, label %10
]
7: ; preds = %1, %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %14
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %4)
br label %14
12: ; preds = %1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %5)
br label %14
14: ; preds = %12, %10, %7
%15 = tail call i32 @fflush(i32* null)
store i32 %6, i32* @errno
ret i32 %4
}