#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int, ...);
int test_lseek() {
  int fd;
  off_t ret = 0;
  errno = 0;
  fd = open("foo.fileio.test", O_RDONLY);
  if (fd >= 0) {
    errno = 0;
    ret = lseek(fd, 0, SEEK_CUR);
    printf("lseek 1: ret = %ld, errno = %d, %s\n", (long)ret, errno,
           ret == 0 ? "OK" : "");
    errno = 0;
    ret = lseek(fd, 0, SEEK_END);
    printf("lseek 2: ret = %ld, errno = %d, %s\n", (long)ret, errno,
           ret == 11 ? "OK" : "");
    errno = 0;
    ret = lseek(fd, 3, SEEK_SET);
    printf("lseek 3: ret = %ld, errno = %d, %s\n", (long)ret, errno,
           ret == 3 ? "OK" : "");
    close(fd);
  } else {
    printf("lseek 1: ret = %d, errno = %d\n", ret, errno);
    printf("lseek 2: ret = %d, errno = %d\n", ret, errno);
    printf("lseek 3: ret = %d, errno = %d\n", ret, errno);
  }
}


x86 assembly (clang, -O0):
<test_lseek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0xc(%rbp)
mov    0x2e72(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1296 <test_lseek+0x156>
mov    0x2e43(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    %eax,%esi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xee3(%rip),%rcx
lea    0xe73(%rip),%rax
cmp    $0x0,%edi
cmove  %rax,%rcx
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2deb(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    %eax,%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xe8b(%rip),%rcx
lea    0xe1b(%rip),%rax
cmp    $0xb,%edi
cmove  %rax,%rcx
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d93(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
mov    $0x3,%esi
call   1040 <lseek@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    %eax,%esi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xe30(%rip),%rcx
lea    0xdc0(%rip),%rax
cmp    $0x3,%edi
cmove  %rax,%rcx
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    12e4 <test_lseek+0x1a4>
mov    -0xc(%rbp),%esi
mov    0x2d28(%rip),%rax
mov    (%rax),%edx
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
mov    0x2d0e(%rip),%rax
mov    (%rax),%edx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
mov    0x2cf4(%rip),%rax
mov    (%rax),%edx
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_lseek>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7a(%rip),%r14
movl   $0x0,(%r14)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     122f <test_lseek+0xef>
mov    %eax,%ebp
movl   $0x0,(%r14)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    (%r14),%edx
test   %eax,%eax
lea    0xe9b(%rip),%r15
lea    0xefd(%rip),%rbx
mov    %rbx,%rcx
cmove  %r15,%rcx
lea    0xe62(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%r14)
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    (%r14),%edx
cmp    $0xb,%eax
mov    %rbx,%rcx
cmove  %r15,%rcx
lea    0xe53(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x0,(%r14)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x3,%esi
call   1040 <lseek@plt>
mov    (%r14),%edx
cmp    $0x3,%eax
cmove  %r15,%rbx
lea    0xe41(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1268 <test_lseek+0x128>
mov    (%r14),%edx
lea    0xe46(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r14),%edx
lea    0xe52(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r14),%edx
lea    0xe5e(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [36 x i8] c"lseek 1: ret = %ld, errno = %d, %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@.str.4 = [36 x i8] c"lseek 2: ret = %ld, errno = %d, %s\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [36 x i8] c"lseek 3: ret = %ld, errno = %d, %s\0A\00"
@.str.6 = [31 x i8] c"lseek 1: ret = %d, errno = %d\0A\00"
@.str.7 = [31 x i8] c"lseek 2: ret = %d, errno = %d\0A\00"
@.str.8 = [31 x i8] c"lseek 3: ret = %d, errno = %d\0A\00"
define i32 @test_lseek() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %3
store i32 0, i32* @errno
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %47
8: ; preds = %0
store i32 0, i32* @errno
%9 = load i32, i32* %2
%10 = load i32, i32* @SEEK_CUR
%11 = call i32 @lseek(i32 %9, i32 0, i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = trunc i64 %13 to i32
%15 = load i32, i32* @errno
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%20 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15, i8* %19)
store i32 0, i32* @errno
%21 = load i32, i32* %2
%22 = load i32, i32* @SEEK_END
%23 = call i32 @lseek(i32 %21, i32 0, i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = trunc i64 %25 to i32
%27 = load i32, i32* @errno
%28 = load i32, i32* %3
%29 = icmp eq i32 %28, 11
%30 = zext i1 %29 to i64
%31 = select i1 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%32 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %27, i8* %31)
store i32 0, i32* @errno
%33 = load i32, i32* %2
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @lseek(i32 %33, i32 3, i32 %34)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = sext i32 %36 to i64
%38 = trunc i64 %37 to i32
%39 = load i32, i32* @errno
%40 = load i32, i32* %3
%41 = icmp eq i32 %40, 3
%42 = zext i1 %41 to i64
%43 = select i1 %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%44 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %39, i8* %43)
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
br label %57
47: ; preds = %0
%48 = load i32, i32* %3
%49 = load i32, i32* @errno
%50 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %48, i32 %49)
%51 = load i32, i32* %3
%52 = load i32, i32* @errno
%53 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %51, i32 %52)
%54 = load i32, i32* %3
%55 = load i32, i32* @errno
%56 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 %54, i32 %55)
br label %57
57: ; preds = %47, %8
%58 = load i32, i32* %1
ret i32 %58
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [36 x i8] c"lseek 1: ret = %ld, errno = %d, %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@.str.4 = [36 x i8] c"lseek 2: ret = %ld, errno = %d, %s\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [36 x i8] c"lseek 3: ret = %ld, errno = %d, %s\0A\00"
@.str.6 = [31 x i8] c"lseek 1: ret = %d, errno = %d\0A\00"
@.str.7 = [31 x i8] c"lseek 2: ret = %d, errno = %d\0A\00"
@.str.8 = [31 x i8] c"lseek 3: ret = %d, errno = %d\0A\00"
define i32 @test_lseek() {
store i32 0, i32* @errno
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %24
4: ; preds = %0
store i32 0, i32* @errno
%5 = load i32, i32* @SEEK_CUR
%6 = tail call i32 @lseek(i32 %2, i32 0, i32 %5)
%7 = load i32, i32* @errno
%8 = icmp eq i32 %6, 0
%9 = select i1 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%10 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %7, i8* %9)
store i32 0, i32* @errno
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @lseek(i32 %2, i32 0, i32 %11)
%13 = load i32, i32* @errno
%14 = icmp eq i32 %12, 11
%15 = select i1 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%16 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %12, i32 %13, i8* %15)
store i32 0, i32* @errno
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @lseek(i32 %2, i32 3, i32 %17)
%19 = load i32, i32* @errno
%20 = icmp eq i32 %18, 3
%21 = select i1 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%22 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %18, i32 %19, i8* %21)
%23 = tail call i32 @close(i32 %2)
br label %31
24: ; preds = %0
%25 = load i32, i32* @errno
%26 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 0, i32 %25)
%27 = load i32, i32* @errno
%28 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 0, i32 %27)
%29 = load i32, i32* @errno
%30 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 0, i32 %29)
br label %31
31: ; preds = %24, %4
ret i32 undef
}