#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 */
char EOF;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ atoi(char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  char buf;
  char pathname[9] = "test.txt";
  int fd = 0, read_ret;
  if ((fd = open(pathname, O_RDONLY, 00777)) == -1) {
    printf("\n something went bad. \n");
    perror("premature fin. ");
    return -1;
  }
  perror("check .");
  if (argc < 2) {
    printf("\n no argument. \n");
    return -1;
  }
  if (lseek(fd, atoi(argv[1]), SEEK_SET) == -1)
    perror("meh ... ");
  do {
    read_ret = read(fd, &buf, 1);
    printf("%x ", buf);
  } while ((buf != EOF) && (read_ret > 0));
  perror("fin. ");
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe83(%rip),%rax
mov    %rax,-0x1a(%rbp)
mov    0xe81(%rip),%al
mov    %al,-0x12(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x1a(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11da <main+0x7a>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <main+0x161>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
cmpl   $0x2,-0x8(%rbp)
jge    120a <main+0xaa>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <main+0x161>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1245 <main+0xe5>
lea    0xe0a(%rip),%rdi
call   1070 <perror@plt>
jmp    124a <main+0xea>
mov    -0x20(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
movsbl -0x11(%rbp),%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x11(%rbp),%ecx
mov    0x2d65(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x29(%rbp)
je     1295 <main+0x135>
cmpl   $0x0,-0x24(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    124a <main+0xea>
lea    0xd84(%rip),%rdi
call   1070 <perror@plt>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0x5e>
lea    0xe73(%rip),%rdi
call   1040 <puts@plt>
lea    0xe32(%rip),%rdi
call   1090 <perror@plt>
jmp    11fe <main+0x7e>
mov    %eax,%r13d
lea    0xe31(%rip),%rdi
call   1090 <perror@plt>
cmp    $0x1,%ebp
jg     1212 <main+0x92>
lea    0xe35(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r14),%rdi
call   10a0 <atoi@plt>
mov    0x2dae(%rip),%rcx
mov    (%rcx),%edx
mov    %r13d,%edi
mov    %eax,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    123f <main+0xbf>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
lea    0x7(%rsp),%r14
lea    0xddf(%rip),%r15
mov    0x2d8e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,%ebp
movzbl 0x7(%rsp),%ebx
movsbl %bl,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
jle    128e <main+0x10e>
cmp    (%r12),%bl
jne    1260 <main+0xe0>
lea    0xd7e(%rip),%rdi
call   1090 <perror@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    1203 <main+0x83>

LLVM IR (-O0):
@__const.main.pathname = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str = [24 x i8] c"\0A something went bad. \0A\00"
@.str.1 = [16 x i8] c"premature fin. \00"
@.str.2 = [8 x i8] c"check .\00"
@.str.3 = [17 x i8] c"\0A no argument. \0A\00"
@SEEK_SET = global i32 0
@.str.4 = [9 x i8] c"meh ... \00"
@.str.5 = [4 x i8] c"%x \00"
@EOF = global i8 0
@.str.6 = [6 x i8] c"fin. \00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca [9 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [9 x i8]* %7 to i8*
store i32 0, i32* %8
%11 = getelementptr inbounds [9 x i8], [9 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12, i32 511)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %56
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 2
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %56
24: ; preds = %18
%25 = load i32, i32* %8
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %25, i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %24
%34 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %24
br label %36
36: ; preds = %51, %35
%37 = load i32, i32* %8
%38 = call i32 @read(i32 %37, i8* %6, i32 1)
store i32 %38, i32* %9
%39 = load i8, i8* %6
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %36
%43 = load i8, i8* %6
%44 = sext i8 %43 to i32
%45 = load i8, i8* @EOF
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %44, %46
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = load i32, i32* %9
%50 = icmp sgt i32 %49, 0
br label %51
51: ; preds = %48, %42
%52 = phi i1 [ false, %42 ], [ %50, %48 ]
br i1 %52, label %36, label %53
53: ; preds = %51
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %56
56: ; preds = %53, %22, %15
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@__const.main.pathname = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"premature fin. \00"
@.str.2 = [8 x i8] c"check .\00"
@SEEK_SET = global i32 0
@.str.4 = [9 x i8] c"meh ... \00"
@.str.5 = [4 x i8] c"%x \00"
@EOF = global i8 0
@.str.6 = [6 x i8] c"fin. \00"
@str = [16 x i8] c"\0A no argument. \00"
@str.8 = [23 x i8] c"\0A something went bad. \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca [9 x i8]
%5 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6, i32 511)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
%11 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %39
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp slt i32 %0, 2
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %39
17: ; preds = %12
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %7, i32 %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
br label %27
27: ; preds = %26, %27
%28 = call i32 @read(i32 %7, i8* %3, i32 1)
%29 = load i8, i8* %3
%30 = sext i8 %29 to i32
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = load i8, i8* @EOF
%33 = icmp ne i8 %29, %32
%34 = icmp sgt i32 %28, 0
%35 = and i1 %34, %33
br i1 %35, label %27, label %36
36: ; preds = %27
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%38 = tail call i32 @putchar(i32 10)
br label %39
39: ; preds = %36, %15, %9
%40 = phi i32 [ -1, %9 ], [ -1, %15 ], [ 0, %36 ]
ret i32 %40
}