#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_SET;
int /*<<< orphan*/ close(int);
int lseek(int, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int main() {
  const char *file_name = "./hw1.txt";
  int fd = open(file_name, O_RDONLY);
  if (fd < 0) {
    printf("error occured while opening the file - %s\n", file_name);
    return -1;
  }
  int offset = lseek(fd, 4, SEEK_SET);
  if (offset < 0) {
    printf("error occured while seeking the file - %s\n", file_name);
    return -2;
  }
  char buf;
  while (read(fd, &buf, 1) > 0)
    printf("%c", buf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a7 <main+0x57>
mov    -0x10(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <main+0xde>
mov    -0x14(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1040 <lseek@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e8 <main+0x98>
mov    -0x10(%rbp),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    122e <main+0xde>
jmp    11ed <main+0x9d>
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    121f <main+0xcf>
movsbl -0x19(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ed <main+0x9d>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e9 <main+0x89>
mov    %eax,%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x4,%esi
call   1050 <lseek@plt>
test   %eax,%eax
js     1205 <main+0xa5>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    11de <main+0x7e>
lea    0x7(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x7(%rsp),%edi
call   1030 <putchar@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     11c0 <main+0x60>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    121f <main+0xbf>
lea    0xe1a(%rip),%rdi
lea    0xe09(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    121f <main+0xbf>
lea    0xe29(%rip),%rdi
lea    0xded(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xfffffffe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"./hw1.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [43 x i8] c"error occured while opening the file - %s\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [43 x i8] c"error occured while seeking the file - %s\0A\00"
@.str.3 = [3 x i8] c"%c\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 0, i32* %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 -1, i32* %1
br label %35
14: ; preds = %0
%15 = load i32, i32* %3
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @lseek(i32 %15, i32 4, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i8*, i8** %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %21)
store i32 -2, i32* %1
br label %35
23: ; preds = %14
br label %24
24: ; preds = %28, %23
%25 = load i32, i32* %3
%26 = call i64 @read(i32 %25, i8* %5, i32 1)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i8, i8* %5
%30 = sext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %24
32: ; preds = %24
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
store i32 0, i32* %1
br label %35
35: ; preds = %32, %20, %11
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [10 x i8] c"./hw1.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [43 x i8] c"error occured while opening the file - %s\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [43 x i8] c"error occured while seeking the file - %s\0A\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %24
7: ; preds = %0
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @lseek(i32 %3, i32 4, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %24
13: ; preds = %7
%14 = call i64 @read(i32 %3, i8* %1, i32 1)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13, %16
%17 = load i8, i8* %1
%18 = sext i8 %17 to i32
%19 = tail call i32 @putchar(i32 %18)
%20 = call i64 @read(i32 %3, i8* %1, i32 1)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %16, label %22
22: ; preds = %16, %13
%23 = tail call i32 @close(i32 %3)
br label %24
24: ; preds = %11, %22, %5
%25 = phi i32 [ -1, %5 ], [ -2, %11 ], [ 0, %22 ]
ret i32 %25
}