#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, char *);
int /*<<< orphan*/ stop();
char *strerrno(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" : "");
    stop();
    errno = 0;
    ret = lseek(fd, 0, SEEK_END);
    printf("lseek 2: ret = %ld, errno = %d, %s\n", (long)ret, errno,
           ret == 11 ? "OK" : "");
    stop();
    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 %s\n", ret, errno, strerrno(errno));
    stop();
    printf("lseek 2: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
    stop();
    printf("lseek 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  }
  stop();
}


x86 assembly (clang, -O0):
<test_lseek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0xc(%rbp)
mov    0x2e52(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     12be <test_lseek+0x15e>
mov    0x2e23(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    %eax,%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xe59(%rip),%rcx
lea    0xe53(%rip),%rax
cmp    $0x0,%edi
cmove  %rax,%rcx
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <stop@plt>
mov    0x2dc6(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    %eax,%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xdfc(%rip),%rcx
lea    0xdf6(%rip),%rax
cmp    $0xb,%edi
cmove  %rax,%rcx
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <stop@plt>
mov    0x2d69(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
mov    $0x3,%esi
call   1050 <lseek@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    %eax,%esi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xd9c(%rip),%rcx
lea    0xd96(%rip),%rax
cmp    $0x3,%edi
cmove  %rax,%rcx
lea    0xdaf(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    136b <test_lseek+0x20b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xd91(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <stop@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2cc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    %rax,%rcx
lea    0xd77(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <stop@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2c85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    %rax,%rcx
lea    0xd5d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <stop@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_lseek>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e5a(%rip),%r14
movl   $0x0,(%r14)
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1257 <test_lseek+0xf7>
mov    %eax,%ebp
movl   $0x0,(%r14)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    (%r14),%edx
test   %eax,%eax
lea    0xe7b(%rip),%r15
lea    0xe73(%rip),%rbx
mov    %rbx,%rcx
cmove  %r15,%rcx
lea    0xe42(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <stop@plt>
movl   $0x0,(%r14)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    (%r14),%edx
cmp    $0xb,%eax
mov    %rbx,%rcx
cmove  %r15,%rcx
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <stop@plt>
movl   $0x0,(%r14)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x3,%esi
call   1050 <lseek@plt>
mov    (%r14),%edx
cmp    $0x3,%eax
cmove  %r15,%rbx
lea    0xe17(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rcx
call   1040 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12bc <test_lseek+0x15c>
mov    (%r14),%ebx
mov    %ebx,%edi
call   1030 <strerrno@plt>
lea    0xe17(%rip),%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %rax,%rcx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <stop@plt>
mov    (%r14),%ebx
mov    %ebx,%edi
call   1030 <strerrno@plt>
lea    0xe15(%rip),%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %rax,%rcx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <stop@plt>
mov    (%r14),%ebx
mov    %ebx,%edi
call   1030 <strerrno@plt>
lea    0xe13(%rip),%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %rax,%rcx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <stop@plt>

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 = [34 x i8] c"lseek 1: ret = %d, errno = %d %s\0A\00"
@.str.7 = [34 x i8] c"lseek 2: ret = %d, errno = %d %s\0A\00"
@.str.8 = [34 x i8] c"lseek 3: ret = %d, errno = %d %s\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 %49
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 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15, i8* %19)
%21 = call i32 (...) @stop()
store i32 0, i32* @errno
%22 = load i32, i32* %2
%23 = load i32, i32* @SEEK_END
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = trunc i64 %26 to i32
%28 = load i32, i32* @errno
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, 11
%31 = zext i1 %30 to i64
%32 = select i1 %30, 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)
%33 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %27, i32 %28, i8* %32)
%34 = call i32 (...) @stop()
store i32 0, i32* @errno
%35 = load i32, i32* %2
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @lseek(i32 %35, i32 3, i32 %36)
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = trunc i64 %39 to i32
%41 = load i32, i32* @errno
%42 = load i32, i32* %3
%43 = icmp eq i32 %42, 3
%44 = zext i1 %43 to i64
%45 = select i1 %43, 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)
%46 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %40, i32 %41, i8* %45)
%47 = load i32, i32* %2
%48 = call i32 @close(i32 %47)
br label %67
49: ; preds = %0
%50 = load i32, i32* %3
%51 = load i32, i32* @errno
%52 = load i32, i32* @errno
%53 = call i8* @strerrno(i32 %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %50, i32 %51, i8* %53)
%55 = call i32 (...) @stop()
%56 = load i32, i32* %3
%57 = load i32, i32* @errno
%58 = load i32, i32* @errno
%59 = call i8* @strerrno(i32 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %56, i32 %57, i8* %59)
%61 = call i32 (...) @stop()
%62 = load i32, i32* %3
%63 = load i32, i32* @errno
%64 = load i32, i32* @errno
%65 = call i8* @strerrno(i32 %64)
%66 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 %62, i32 %63, i8* %65)
br label %67
67: ; preds = %49, %8
%68 = call i32 (...) @stop()
%69 = load i32, i32* %1
ret i32 %69
}

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 = [34 x i8] c"lseek 1: ret = %d, errno = %d %s\0A\00"
@.str.7 = [34 x i8] c"lseek 2: ret = %d, errno = %d %s\0A\00"
@.str.8 = [34 x i8] c"lseek 3: ret = %d, errno = %d %s\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 %26
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 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %7, i8* %9)
%11 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @lseek(i32 %2, i32 0, i32 %12)
%14 = load i32, i32* @errno
%15 = icmp eq i32 %13, 11
%16 = select i1 %15, 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)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %13, i32 %14, i8* %16)
%18 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @lseek(i32 %2, i32 3, i32 %19)
%21 = load i32, i32* @errno
%22 = icmp eq i32 %20, 3
%23 = select i1 %22, 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)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %20, i32 %21, i8* %23)
%25 = tail call i32 @close(i32 %2)
br label %38
26: ; preds = %0
%27 = load i32, i32* @errno
%28 = tail call i8* @strerrno(i32 %27)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 0, i32 %27, i8* %28)
%30 = tail call i32 (...) @stop()
%31 = load i32, i32* @errno
%32 = tail call i8* @strerrno(i32 %31)
%33 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 0, i32 %31, i8* %32)
%34 = tail call i32 (...) @stop()
%35 = load i32, i32* @errno
%36 = tail call i8* @strerrno(i32 %35)
%37 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 0, i32 %35, i8* %36)
br label %38
38: ; preds = %26, %4
%39 = tail call i32 (...) @stop()
ret i32 undef
}