#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 /*<<< orphan*/ _fsizehandle(int);
long nc_lseek(int, long);
long nc_ltell(int);
long lseek(int fd, long posn, int whence) {
  switch (whence) {
  case 1:
    posn += nc_ltell(fd);
  case 2:
    posn = _fsizehandle(fd) - posn;
  }
  if (posn < 0 || posn > 0xFFFF)
    return -1L;
  return nc_lseek(fd, posn & 0xFFFF);
}


x86 assembly (clang, -O0):
<lseek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x1,%eax
je     1167 <lseek+0x37>
jmp    1156 <lseek+0x26>
mov    -0x20(%rbp),%eax
sub    $0x2,%eax
je     1177 <lseek+0x47>
jmp    1189 <lseek+0x59>
mov    -0xc(%rbp),%edi
call   1040 <nc_ltell@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <_fsizehandle@plt>
cltq
sub    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jl     11a2 <lseek+0x72>
cmpq   $0xffff,-0x18(%rbp)
jle    11af <lseek+0x7f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c6 <lseek+0x96>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
and    $0xffff,%rsi
call   1030 <nc_lseek@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lseek>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x2,%edx
je     114c <lseek+0x1c>
cmp    $0x1,%edx
jne    115b <lseek+0x2b>
mov    %ebp,%edi
call   1040 <nc_ltell@plt>
add    %rax,%rbx
mov    %ebp,%edi
call   1050 <_fsizehandle@plt>
cltq
sub    %rbx,%rax
mov    %rax,%rbx
cmp    $0xffff,%rbx
jbe    1172 <lseek+0x42>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %ebp,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <nc_lseek@plt>

LLVM IR (-O0):
define i64 @lseek(i32 %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%8 = load i32, i32* %7
switch i32 %8, label %20 [
i32 1, label %9
i32 2, label %14
]
9: ; preds = %3
%10 = load i32, i32* %5
%11 = call i64 @nc_ltell(i32 %10)
%12 = load i64, i64* %6
%13 = add nsw i64 %12, %11
store i64 %13, i64* %6
br label %14
14: ; preds = %3, %9
%15 = load i32, i32* %5
%16 = call i32 @_fsizehandle(i32 %15)
%17 = sext i32 %16 to i64
%18 = load i64, i64* %6
%19 = sub nsw i64 %17, %18
store i64 %19, i64* %6
br label %20
20: ; preds = %14, %3
%21 = load i64, i64* %6
%22 = icmp slt i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i64, i64* %6
%25 = icmp sgt i64 %24, 65535
br i1 %25, label %26, label %27
26: ; preds = %23, %20
store i64 -1, i64* %4
br label %32
27: ; preds = %23
%28 = load i32, i32* %5
%29 = load i64, i64* %6
%30 = and i64 %29, 65535
%31 = call i64 @nc_lseek(i32 %28, i64 %30)
store i64 %31, i64* %4
br label %32
32: ; preds = %27, %26
%33 = load i64, i64* %4
ret i64 %33
}

LLVM IR (-O2):
define i64 @lseek(i32 %0, i64 %1, i32 %2) {
switch i32 %2, label %12 [
i32 1, label %4
i32 2, label %7
]
4: ; preds = %3
%5 = tail call i64 @nc_ltell(i32 %0)
%6 = add nsw i64 %5, %1
br label %7
7: ; preds = %3, %4
%8 = phi i64 [ %1, %3 ], [ %6, %4 ]
%9 = tail call i32 @_fsizehandle(i32 %0)
%10 = sext i32 %9 to i64
%11 = sub nsw i64 %10, %8
br label %12
12: ; preds = %7, %3
%13 = phi i64 [ %1, %3 ], [ %11, %7 ]
%14 = icmp ugt i64 %13, 65535
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i64 @nc_lseek(i32 %0, i64 %13)
br label %17
17: ; preds = %12, %15
%18 = phi i64 [ %16, %15 ], [ -1, %12 ]
ret i64 %18
}