#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 off_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int handle_off(int);
int lseek(int, int, int);
int /*<<< orphan*/ printf(char *, ...);
off_t lseek_test(int fd, off_t off, int whence) {
  off_t ret;
  int save_errno;
  int passed;
  printf("lseek(): fd=%d, off=%u, whence=%d\n", fd, off, whence);
  fflush(NULL);
  passed = handle_off(ret = lseek(fd, off, whence));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    printf("\t\tret = %u\n", ret);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed: OS returned undefined value! "
           "ret = %u\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):
<lseek_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe9f(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <lseek@plt>
mov    %eax,%edi
mov    %edi,-0x10(%rbp)
call   1060 <handle_off@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xfffffffe,%eax
je     11f1 <lseek_test+0xb1>
jmp    11b0 <lseek_test+0x70>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     1210 <lseek_test+0xd0>
jmp    11c4 <lseek_test+0x84>
mov    -0x10(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    122a <lseek_test+0xea>
mov    -0x10(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    122a <lseek_test+0xea>
mov    -0x18(%rbp),%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x14(%rbp),%ecx
mov    0x2dac(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lseek_test>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
lea    0xe9e(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1050 <lseek@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <handle_off@plt>
mov    0x2e4d(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11bc <lseek_test+0x6c>
lea    0xe7e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed5(%rip),%rdi
call   1030 <puts@plt>
jmp    11dc <lseek_test+0x8c>
cmp    $0xfffffffe,%eax
jne    11cc <lseek_test+0x7c>
lea    0xe67(%rip),%rdi
mov    %ebx,%esi
jmp    11d5 <lseek_test+0x85>
lea    0xe8c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <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 = [35 x i8] c"lseek(): fd=%d, off=%u, whence=%d\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %u\0A\00"
@.str.2 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.3 = [48 x i8] c"\09Failed: OS returned undefined value! ret = %u\0A\00"
@.str.4 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @lseek_test(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %4
%11 = load i32, i32* %5
%12 = load i32, i32* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11, i32 %12)
%14 = call i32 @fflush(i32* null)
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = load i32, i32* %6
%18 = call i32 @lseek(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = call i32 @handle_off(i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* @errno
store i32 %20, i32* %8
%21 = load i32, i32* %9
switch i32 %21, label %31 [
i32 -1, label %22
i32 0, label %22
i32 -2, label %27
]
22: ; preds = %3, %3
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @fflush(i32* null)
br label %35
27: ; preds = %3
%28 = load i32, i32* %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = call i32 @fflush(i32* null)
br label %35
31: ; preds = %3
%32 = load i32, i32* %9
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = call i32 @fflush(i32* null)
br label %35
35: ; preds = %31, %27, %22
%36 = load i32, i32* %8
store i32 %36, i32* @errno
%37 = load i32, i32* %7
ret i32 %37
}

LLVM IR (-O2):
@.str = [35 x i8] c"lseek(): fd=%d, off=%u, whence=%d\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %u\0A\00"
@.str.3 = [48 x i8] c"\09Failed: OS returned undefined value! ret = %u\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 @lseek_test(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1, i32 %2)
%5 = tail call i32 @fflush(i32* null)
%6 = tail call i32 @lseek(i32 %0, i32 %1, i32 %2)
%7 = tail call i32 @handle_off(i32 %6)
%8 = load i32, i32* @errno
switch i32 %7, label %14 [
i32 -1, label %9
i32 0, label %9
i32 -2, label %12
]
9: ; preds = %3, %3
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %16
12: ; preds = %3
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %6)
br label %16
14: ; preds = %3
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %7)
br label %16
16: ; preds = %14, %12, %9
%17 = tail call i32 @fflush(i32* null)
store i32 %8, i32* @errno
ret i32 %6
}