#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 ftruncate(int, size_t);
int handle_zng(int);
int /*<<< orphan*/ printf(char *, ...);
int ftruncate_test(int fd, size_t size) {
  int ret, save_errno;
  int passed;
  printf("ftruncate(): fd=%d, size=%ld\n", fd, size);
  fflush(NULL);
  passed = handle_zng(ret = ftruncate(fd, size));
  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):
<ftruncate_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xea3(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <ftruncate@plt>
mov    %eax,%edi
mov    %edi,-0x14(%rbp)
call   1030 <handle_zng@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xfffffffe,%eax
je     11eb <ftruncate_test+0xab>
jmp    11aa <ftruncate_test+0x6a>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     120a <ftruncate_test+0xca>
jmp    11be <ftruncate_test+0x7e>
mov    -0x14(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    1224 <ftruncate_test+0xe4>
mov    -0x14(%rbp),%esi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    1224 <ftruncate_test+0xe4>
mov    -0x1c(%rbp),%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x18(%rbp),%ecx
mov    0x2db2(%rip),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftruncate_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xea0(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <ftruncate@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <handle_zng@plt>
mov    0x2e53(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11b6 <ftruncate_test+0x66>
lea    0xe7f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xedb(%rip),%rdi
call   1030 <puts@plt>
jmp    11d6 <ftruncate_test+0x86>
cmp    $0xfffffffe,%eax
jne    11c6 <ftruncate_test+0x76>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
jmp    11cf <ftruncate_test+0x7f>
lea    0xe92(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"ftruncate(): fd=%d, size=%ld\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 @ftruncate_test(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%8 = load i32, i32* %3
%9 = load i64, i64* %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %8, i64 %9)
%11 = call i32 @fflush(i32* null)
%12 = load i32, i32* %3
%13 = load i64, i64* %4
%14 = call i32 @ftruncate(i32 %12, i64 %13)
store i32 %14, i32* %5
%15 = call i32 @handle_zng(i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* @errno
store i32 %16, i32* %6
%17 = load i32, i32* %7
switch i32 %17, label %27 [
i32 -1, label %18
i32 0, label %18
i32 -2, label %23
]
18: ; preds = %2, %2
%19 = load i32, i32* %5
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @fflush(i32* null)
br label %31
23: ; preds = %2
%24 = load i32, i32* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = call i32 @fflush(i32* null)
br label %31
27: ; preds = %2
%28 = load i32, i32* %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = call i32 @fflush(i32* null)
br label %31
31: ; preds = %27, %23, %18
%32 = load i32, i32* %6
store i32 %32, i32* @errno
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@.str = [30 x i8] c"ftruncate(): fd=%d, size=%ld\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 @ftruncate_test(i32 %0, i64 %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %0, i64 %1)
%4 = tail call i32 @fflush(i32* null)
%5 = tail call i32 @ftruncate(i32 %0, i64 %1)
%6 = tail call i32 @handle_zng(i32 %5)
%7 = load i32, i32* @errno
switch i32 %6, label %13 [
i32 -1, label %8
i32 0, label %8
i32 -2, label %11
]
8: ; preds = %2, %2
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %15
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %5)
br label %15
13: ; preds = %2
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %6)
br label %15
15: ; preds = %13, %11, %8
%16 = tail call i32 @fflush(i32* null)
store i32 %7, i32* @errno
ret i32 %5
}