#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;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int pread(int, char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  int fd, n, seek;
  char buf[4096];
  if ((fd = open("./readme.md", O_RDONLY)) < 0)
    err_sys("open file error!");
  if ((seek = lseek(fd, 0, SEEK_SET)) == -1)
    err_sys("lseek file error!");
  printf("current seek: %d\n", seek);
  if ((n = pread(fd, buf, 100, seek)) != 100)
    err_sys("read file error!");
  if ((seek = lseek(fd, 0, SEEK_CUR)) == -1)
    err_sys("lseek file error!");
  printf("after read seek: %d\n", seek);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    119f <main+0x3f>
lea    0xe72(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x8(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11ca <main+0x6a>
lea    0xe58(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x10(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x10(%rbp),%ecx
mov    $0x64,%edx
call   1070 <pread@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x64,%eax
je     1208 <main+0xa8>
lea    0xe3e(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1233 <main+0xd3>
lea    0xdef(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x10(%rbp),%esi
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1190 <main+0x30>
lea    0xe81(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11b5 <main+0x55>
lea    0xe6d(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xe73(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x64,%edx
mov    %ebp,%ecx
call   1070 <pread@plt>
cmp    $0x64,%eax
je     11e5 <main+0x85>
lea    0xe61(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    120a <main+0xaa>
lea    0xe18(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xe41(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"./readme.md\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"open file error!\00"
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"lseek file error!\00"
@.str.3 = [18 x i8] c"current seek: %d\0A\00"
@.str.4 = [17 x i8] c"read file error!\00"
@SEEK_CUR = global i32 0
@.str.5 = [21 x i8] c"after read seek: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* %2
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @lseek(i32 %12, i32 0, i32 %13)
store i32 %14, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = load i32, i32* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %2
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* %4
%24 = call i32 @pread(i32 %21, i8* %22, i32 100, i32 %23)
store i32 %24, i32* %3
%25 = icmp ne i32 %24, 100
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %18
%29 = load i32, i32* %2
%30 = load i32, i32* @SEEK_CUR
%31 = call i32 @lseek(i32 %29, i32 0, i32 %30)
store i32 %31, i32* %4
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %4
%37 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"./readme.md\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"open file error!\00"
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"lseek file error!\00"
@.str.3 = [18 x i8] c"current seek: %d\0A\00"
@.str.4 = [17 x i8] c"read file error!\00"
@SEEK_CUR = global i32 0
@.str.5 = [21 x i8] c"after read seek: %d\0A\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @lseek(i32 %4, i32 0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%16 = call i32 @pread(i32 %4, i8* %2, i32 100, i32 %10)
%17 = icmp eq i32 %16, 100
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = tail call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load i32, i32* @SEEK_CUR
%22 = tail call i32 @lseek(i32 %4, i32 0, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%28 = tail call i32 @exit(i32 0)
unreachable
}