#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__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int main(void) {
  int fd;
  int ret;
  int offset;
  char buf[10] = {};
  if ((fd = open("test.txt", O_RDONLY)) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day02/lseek.c",
              22, "open", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  while ((ret = read(fd, buf, 5)) <= 0) {
    if (errno == EINTR)
      continue;
    break;
  }
  if (ret == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day02/lseek.c",
              32, "read", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  offset = lseek(fd, 0, SEEK_CUR);
  if (offset == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day02/lseek.c",
              36, "lseek", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  printf("offsert=%d\n", offset);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe41(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1223 <main+0x93>
jmp    11d5 <main+0x45>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%r9
lea    0xe0c(%rip),%rsi
lea    0xe15(%rip),%rdx
mov    $0x16,%ecx
lea    0xe9d(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1228 <main+0x98>
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x5,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     1269 <main+0xd9>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rax
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%rax
jne    1264 <main+0xd4>
jmp    1228 <main+0x98>
jmp    1269 <main+0xd9>
cmpl   $0xffffffff,-0xc(%rbp)
jne    12c6 <main+0x136>
jmp    1278 <main+0xe8>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%r9
lea    0xd69(%rip),%rsi
lea    0xd72(%rip),%rdx
mov    $0x20,%ecx
lea    0xdff(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1339 <main+0x1a9>
jmp    12eb <main+0x15b>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cca(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%r9
lea    0xcf6(%rip),%rsi
lea    0xcff(%rip),%rdx
mov    $0x24,%ecx
lea    0xd91(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%esi
lea    0xd7a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movw   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122f <main+0xaf>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    0x2e05(%rip),%r15
mov    0x2dee(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x5,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11f5 <main+0x75>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     11d0 <main+0x50>
cmp    $0xffffffff,%eax
je     1294 <main+0x114>
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     1265 <main+0xe5>
lea    0xeaa(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d9a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d89(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xdbb(%rip),%rsi
lea    0xdc4(%rip),%rdx
lea    0xe51(%rip),%r8
mov    %ebx,%edi
mov    $0x16,%ecx
jmp    12be <main+0x13e>
mov    0x2d64(%rip),%rax
mov    (%rax),%ebx
mov    (%r15),%rdi
call   10a0 <strerror@plt>
lea    0xd8c(%rip),%rsi
lea    0xd95(%rip),%rdx
lea    0xe2c(%rip),%r8
mov    %ebx,%edi
mov    $0x24,%ecx
jmp    12be <main+0x13e>
mov    0x2d35(%rip),%rax
mov    (%rax),%ebx
call   10a0 <strerror@plt>
lea    0xd60(%rip),%rsi
lea    0xd69(%rip),%rdx
lea    0xdfb(%rip),%r8
mov    %ebx,%edi
mov    $0x20,%ecx
mov    %rax,%r9
call   1070 <fprintf@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day02/lseek.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@EINTR = global i64 0
@.str.4 = [5 x i8] c"read\00"
@SEEK_CUR = global i32 0
@.str.5 = [6 x i8] c"lseek\00"
@.str.6 = [12 x i8] c"offsert=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [10 x i8]
store i32 0, i32* %1
%6 = bitcast [10 x i8]* %5 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %19
10: ; preds = %0
br label %11
11: ; preds = %10
%12 = load i32, i32* @stderr
%13 = load i64, i64* @errno
%14 = call i8* @strerror(i64 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.2, i64 0, i64 0), i32 22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; No predecessors!
br label %19
19: ; preds = %18, %0
br label %20
20: ; preds = %29, %19
%21 = load i32, i32* %2
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 5)
store i32 %23, i32* %3
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
br label %20
30: ; preds = %25
br label %31
31: ; preds = %30, %20
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %43
34: ; preds = %31
br label %35
35: ; preds = %34
%36 = load i32, i32* @stderr
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.2, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; No predecessors!
br label %43
43: ; preds = %42, %31
%44 = load i32, i32* %2
%45 = load i32, i32* @SEEK_CUR
%46 = call i32 @lseek(i32 %44, i32 0, i32 %45)
store i32 %46, i32* %4
%47 = load i32, i32* %4
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %58
49: ; preds = %43
br label %50
50: ; preds = %49
%51 = load i32, i32* @stderr
%52 = load i64, i64* @errno
%53 = call i8* @strerror(i64 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.2, i64 0, i64 0), i32 36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; No predecessors!
br label %58
58: ; preds = %57, %43
%59 = load i32, i32* %4
%60 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %2
%62 = call i32 @close(i32 %61)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day02/lseek.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@EINTR = global i64 0
@.str.4 = [5 x i8] c"read\00"
@SEEK_CUR = global i32 0
@.str.5 = [6 x i8] c"lseek\00"
@.str.6 = [12 x i8] c"offsert=%d\0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i64, i64* @errno
%9 = tail call i8* @strerror(i64 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.2, i64 0, i64 0), i32 22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %0, %16
%14 = call i32 @read(i32 %4, i8* %2, i32 5)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %13, label %20
20: ; preds = %16
%21 = icmp eq i32 %14, -1
br i1 %21, label %22, label %28
22: ; preds = %20
%23 = load i32, i32* @stderr
%24 = tail call i8* @strerror(i64 %17)
%25 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.2, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %13, %20
%29 = load i32, i32* @SEEK_CUR
%30 = tail call i32 @lseek(i32 %4, i32 0, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i64, i64* @errno
%35 = tail call i8* @strerror(i64 %34)
%36 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.2, i64 0, i64 0), i32 36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %28
%40 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%41 = tail call i32 @close(i32 %4)
ret i32 0
}