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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
long ftell(int /*<<< orphan*/ *);
int handle_lng(long);
int /*<<< orphan*/ printf(char *, ...);
long ftell_test(FILE *fp) {
  long ret;
  int save_errno;
  int passed;
  printf("ftell(): fp=0x%p\n", fp);
  fflush(NULL);
  passed = handle_lng(ret = ftell(fp));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    printf("\t\tret = %ld\n", ret);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %ld\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):
<ftell_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xea9(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x8(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,%rdi
mov    %rdi,-0x10(%rbp)
call   1030 <handle_lng@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xfffffffe,%eax
je     11e5 <ftell_test+0xa5>
jmp    11a3 <ftell_test+0x63>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     1205 <ftell_test+0xc5>
jmp    11b7 <ftell_test+0x77>
mov    -0x10(%rbp),%rsi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    121f <ftell_test+0xdf>
mov    -0x10(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    121f <ftell_test+0xdf>
mov    -0x18(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x14(%rbp),%ecx
mov    0x2db7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftell_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xea2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <handle_lng@plt>
mov    0x2e57(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11b3 <ftell_test+0x63>
lea    0xe77(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xed4(%rip),%rdi
call   1030 <puts@plt>
jmp    11db <ftell_test+0x8b>
cmp    $0xfffffffe,%eax
jne    11cb <ftell_test+0x7b>
lea    0xe60(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11db <ftell_test+0x8b>
lea    0xe83(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"ftell(): fp=0x%p\0A\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"\09\09ret = %ld\0A\00"
@.str.2 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.3 = [54 x i8] c"\09Failed Phase 1: returned undefined value! ret = %ld\0A\00"
@.str.4 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i64 @ftell_test(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
%6 = load i32*, i32** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32* %6)
%8 = call i32 @fflush(i32* null)
%9 = load i32*, i32** %2
%10 = call i64 @ftell(i32* %9)
store i64 %10, i64* %3
%11 = call i32 @handle_lng(i64 %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 i64, i64* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64 %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 i64, i64* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %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 i64, i64* %3
ret i64 %29
}

LLVM IR (-O2):
@.str = [18 x i8] c"ftell(): fp=0x%p\0A\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"\09\09ret = %ld\0A\00"
@.str.3 = [54 x i8] c"\09Failed Phase 1: returned undefined value! ret = %ld\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 i64 @ftell_test(i32* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32* %0)
%3 = tail call i32 @fflush(i32* null)
%4 = tail call i64 @ftell(i32* %0)
%5 = tail call i32 @handle_lng(i64 %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 ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64 %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 ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %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 i64 %4
}