#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 */
scalar_t__ EOF;
int SEEK_END;
scalar_t__ readbyte(int);
long lseek(int handle, long posn, int whence) {
  long position;
  if (whence == SEEK_END) {
    position = 0;
    while (readbyte(handle) != EOF)
      position++;
  } else {
    for (position = 0; (position != posn) && (readbyte(handle) != EOF);
         position++)
      ;
  }
  return (position);
}


x86 assembly (clang, -O0):
<lseek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2ebc(%rip),%rcx
cmp    (%rcx),%eax
jne    116a <lseek+0x5a>
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <readbyte@plt>
mov    0x2e95(%rip),%rcx
cmp    (%rcx),%rax
je     1165 <lseek+0x55>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    113c <lseek+0x2c>
jmp    11c8 <lseek+0xb8>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x21(%rbp)
je     119d <lseek+0x8d>
mov    -0x4(%rbp),%edi
call   1030 <readbyte@plt>
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11ad <lseek+0x9d>
jmp    11c3 <lseek+0xb3>
jmp    11b2 <lseek+0xa2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1172 <lseek+0x62>
jmp    11c8 <lseek+0xb8>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lseek>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2ec8(%rip),%rax
cmp    %edx,(%rax)
jne    1152 <lseek+0x42>
mov    $0xffffffffffffffff,%rbx
mov    0x2eae(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
call   1030 <readbyte@plt>
add    $0x1,%rbx
cmp    (%r14),%rax
jne    1140 <lseek+0x30>
jmp    1188 <lseek+0x78>
mov    %rsi,%r14
xor    %ebx,%ebx
test   %rsi,%rsi
je     1188 <lseek+0x78>
mov    0x2e7d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
call   1030 <readbyte@plt>
cmp    (%r15),%rax
je     1188 <lseek+0x78>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1170 <lseek+0x60>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@EOF = global i64 0
define i64 @lseek(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%8 = load i32, i32* %6
%9 = load i32, i32* @SEEK_END
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %21
11: ; preds = %3
store i64 0, i64* %7
br label %12
12: ; preds = %17, %11
%13 = load i32, i32* %4
%14 = call i64 @readbyte(i32 %13)
%15 = load i64, i64* @EOF
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i64, i64* %7
%19 = add nsw i64 %18, 1
store i64 %19, i64* %7
br label %12
20: ; preds = %12
br label %38
21: ; preds = %3
store i64 0, i64* %7
br label %22
22: ; preds = %34, %21
%23 = load i64, i64* %7
%24 = load i64, i64* %5
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* %4
%28 = call i64 @readbyte(i32 %27)
%29 = load i64, i64* @EOF
%30 = icmp ne i64 %28, %29
br label %31
31: ; preds = %26, %22
%32 = phi i1 [ false, %22 ], [ %30, %26 ]
br i1 %32, label %33, label %37
33: ; preds = %31
br label %34
34: ; preds = %33
%35 = load i64, i64* %7
%36 = add nsw i64 %35, 1
store i64 %36, i64* %7
br label %22
37: ; preds = %31
br label %38
38: ; preds = %37, %20
%39 = load i64, i64* %7
ret i64 %39
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@EOF = global i64 0
define i64 @lseek(i32 %0, i64 %1, i32 %2) {
%4 = load i32, i32* @SEEK_END
%5 = icmp eq i32 %4, %2
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = icmp eq i64 %1, 0
br i1 %7, label %22, label %14
8: ; preds = %3, %8
%9 = phi i64 [ %13, %8 ], [ 0, %3 ]
%10 = tail call i64 @readbyte(i32 %0)
%11 = load i64, i64* @EOF
%12 = icmp eq i64 %10, %11
%13 = add nuw nsw i64 %9, 1
br i1 %12, label %22, label %8
14: ; preds = %6, %19
%15 = phi i64 [ %20, %19 ], [ 0, %6 ]
%16 = tail call i64 @readbyte(i32 %0)
%17 = load i64, i64* @EOF
%18 = icmp eq i64 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = add nuw nsw i64 %15, 1
%21 = icmp eq i64 %20, %1
br i1 %21, label %22, label %14
22: ; preds = %19, %14, %8, %6
%23 = phi i64 [ 0, %6 ], [ %9, %8 ], [ %1, %19 ], [ %15, %14 ]
ret i64 %23
}