#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*/ O_RDONLY;
scalar_t__ SEEK_SET;
int __NR_lseek;
int /*<<< orphan*/ close(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
long syscall(long, long, long, long);
int main(int argc, char **argv) {
  long ofs = 0x12345678901;
  errno = 0;
  close(0);
  if (open("/etc/passwd", O_RDONLY))
    return 1;
  long r = syscall((long)(__NR_lseek | 0x40000000), (long)(0), (long)(ofs),
                   (long)(SEEK_SET));
  printf("pos:%ld(0x%lx) errno:%m\n", r, r);
  if (!errno)
    printf((r == ofs) ? "64-bit offset used\n" : "Kernel truncated offset\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movabs $0x12345678901,%rax
mov    %rax,-0x18(%rbp)
mov    0x2e6d(%rip),%rax
movq   $0x0,(%rax)
xor    %edi,%edi
call   1040 <close@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1060 <open@plt>
cmp    $0x0,%rax
je     11a4 <main+0x64>
movl   $0x1,-0x4(%rbp)
jmp    1222 <main+0xe2>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
or     $0x40000000,%eax
movslq %eax,%rdi
mov    -0x18(%rbp),%rdx
mov    0x2e10(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <syscall@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2deb(%rip),%rax
cmpq   $0x0,(%rax)
jne    121b <main+0xdb>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rdx
lea    0xe33(%rip),%rdi
lea    0xe18(%rip),%rax
cmp    %rdx,%rcx
cmove  %rax,%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%r15
movq   $0x0,(%r15)
xor    %edi,%edi
call   1040 <close@plt>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    11ea <main+0xaa>
movabs $0x12345678901,%r14
mov    0x2e34(%rip),%rax
movslq (%rax),%rdi
or     $0x40000000,%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%rcx
xor    %esi,%esi
mov    %r14,%rdx
call   1050 <syscall@plt>
mov    %rax,%rbx
lea    0xe58(%rip),%rdi
mov    %rax,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
cmpq   $0x0,(%r15)
mov    $0x0,%ebp
jne    11ea <main+0xaa>
cmp    %r14,%rbx
lea    0xe4f(%rip),%rax
lea    0xe5c(%rip),%rdi
cmove  %rax,%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@__NR_lseek = global i32 0
@SEEK_SET = global i64 0
@.str.1 = [25 x i8] c"pos:%ld(0x%lx) errno:%m\0A\00"
@.str.2 = [20 x i8] c"64-bit offset used\0A\00"
@.str.3 = [25 x i8] c"Kernel truncated offset\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 1250999896321, i64* %6
store i64 0, i64* @errno
%8 = call i32 @close(i32 0)
%9 = load i32, i32* @O_RDONLY
%10 = call i64 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 1, i32* %3
br label %33
13: ; preds = %2
%14 = load i32, i32* @__NR_lseek
%15 = or i32 %14, 1073741824
%16 = sext i32 %15 to i64
%17 = load i64, i64* %6
%18 = load i64, i64* @SEEK_SET
%19 = call i64 @syscall(i64 %16, i64 0, i64 %17, i64 %18)
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = load i64, i64* %7
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %20, i64 %21)
%23 = load i64, i64* @errno
%24 = icmp ne i64 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %13
%26 = load i64, i64* %7
%27 = load i64, i64* %6
%28 = icmp eq i64 %26, %27
%29 = zext i1 %28 to i64
%30 = select i1 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0)
%31 = call i32 (i8*, ...) @printf(i8* %30)
br label %32
32: ; preds = %25, %13
store i32 0, i32* %3
br label %33
33: ; preds = %32, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@__NR_lseek = global i32 0
@SEEK_SET = global i64 0
@.str.1 = [25 x i8] c"pos:%ld(0x%lx) errno:%m\0A\00"
@.str.2 = [20 x i8] c"64-bit offset used\0A\00"
@.str.3 = [25 x i8] c"Kernel truncated offset\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
store i64 0, i64* @errno
%3 = tail call i32 @close(i32 0)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i64 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %2
%8 = load i32, i32* @__NR_lseek
%9 = or i32 %8, 1073741824
%10 = sext i32 %9 to i64
%11 = load i64, i64* @SEEK_SET
%12 = tail call i64 @syscall(i64 %10, i64 0, i64 1250999896321, i64 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %12, i64 %12)
%14 = load i64, i64* @errno
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %7
%17 = icmp eq i64 %12, 1250999896321
%18 = select i1 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %18)
br label %20
20: ; preds = %7, %16, %2
%21 = phi i32 [ 1, %2 ], [ 0, %16 ], [ 0, %7 ]
ret i32 %21
}