#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 */
typedef int /*<<< orphan*/ read_buffer;
typedef int /*<<< orphan*/ file_name;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  int my_file_desc;
  char file_name[4096];
  char read_buffer[4096];
  int i;
  int r;
  if (argc < 2) {
    printf("need to specify a file name as the first argument\n");
    exit(1);
  }
  for (i = 0; i < sizeof(file_name); i++) {
    file_name[i] = 0;
  }
  strncpy(file_name, argv[1], sizeof(file_name));
  file_name[sizeof(file_name) - 1] = 0;
  my_file_desc = open(file_name, O_RDONLY, 0);
  if (my_file_desc < 0) {
    printf("failed to open %s for reading\n", file_name);
    exit(1);
  }
  for (i = 0; i < sizeof(read_buffer); i++) {
    read_buffer[i] = 0;
  }
  r = read(my_file_desc, read_buffer, sizeof(read_buffer) - 1);
  while (r > 0) {
    printf("%s", read_buffer);
    for (i = 0; i < sizeof(read_buffer); i++) {
      read_buffer[i] = 0;
    }
    r = read(my_file_desc, read_buffer, sizeof(read_buffer) - 1);
  }
  close(my_file_desc);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119b <main+0x3b>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x2024(%rbp)
movslq -0x2024(%rbp),%rax
cmp    $0x1000,%rax
jae    11db <main+0x7b>
movslq -0x2024(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x2024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2024(%rbp)
jmp    11a5 <main+0x45>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x1020(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    123b <main+0xdb>
lea    -0x1020(%rbp),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x2024(%rbp)
movslq -0x2024(%rbp),%rax
cmp    $0x1000,%rax
jae    127b <main+0x11b>
movslq -0x2024(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
mov    -0x2024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2024(%rbp)
jmp    1245 <main+0xe5>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %eax,-0x2028(%rbp)
cmpl   $0x0,-0x2028(%rbp)
jle    1316 <main+0x1b6>
lea    -0x2020(%rbp),%rsi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2024(%rbp)
movslq -0x2024(%rbp),%rax
cmp    $0x1000,%rax
jae    12f7 <main+0x197>
movslq -0x2024(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
mov    -0x2024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2024(%rbp)
jmp    12c1 <main+0x161>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %eax,-0x2028(%rbp)
jmp    1295 <main+0x135>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
cmp    $0x2,%edi
jl     1264 <main+0xe4>
mov    %rsi,%rbx
mov    %rsp,%rbp
mov    $0x1000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x8(%rbx),%rsi
mov    %rbp,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xfff(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     127a <main+0xfa>
mov    %eax,%ebp
lea    0x1000(%rsp),%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xfff,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    124f <main+0xcf>
lea    0xe0f(%rip),%r14
lea    0x1000(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xfff,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1220 <main+0xa0>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd7f(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [51 x i8] c"need to specify a file name as the first argument\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"failed to open %s for reading\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
store i32 0, i32* %9
br label %17
17: ; preds = %25, %16
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = icmp ult i64 %19, 4096
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %21
%26 = load i32, i32* %9
%27 = add nsw i32 %26, 1
store i32 %27, i32* %9
br label %17
28: ; preds = %17
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @strncpy(i8* %29, i8* %32, i32 4096)
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 4095
store i8 0, i8* %34
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %35, i32 %36, i32 0)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %28
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %28
store i32 0, i32* %9
br label %45
45: ; preds = %53, %44
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = icmp ult i64 %47, 4096
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %49
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %45
56: ; preds = %45
%57 = load i32, i32* %6
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 4095)
store i32 %59, i32* %10
br label %60
60: ; preds = %77, %56
%61 = load i32, i32* %10
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %81
63: ; preds = %60
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %64)
store i32 0, i32* %9
br label %66
66: ; preds = %74, %63
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = icmp ult i64 %68, 4096
br i1 %69, label %70, label %77
70: ; preds = %66
%71 = load i32, i32* %9
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %72
store i8 0, i8* %73
br label %74
74: ; preds = %70
%75 = load i32, i32* %9
%76 = add nsw i32 %75, 1
store i32 %76, i32* %9
br label %66
77: ; preds = %66
%78 = load i32, i32* %6
%79 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%80 = call i32 @read(i32 %78, i8* %79, i32 4095)
store i32 %80, i32* %10
br label %60
81: ; preds = %60
%82 = load i32, i32* %6
%83 = call i32 @close(i32 %82)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"failed to open %s for reading\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@str = [50 x i8] c"need to specify a file name as the first argument\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = icmp slt i32 %0, 2
br i1 %8, label %18, label %9
9: ; preds = %2
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @strncpy(i8* %6, i8* %12, i32 4096)
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4095
store i8 0, i8* %14
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %6, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %24, label %21
18: ; preds = %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %9
%22 = call i32 @read(i32 %16, i8* %7, i32 4095)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %27, label %31
24: ; preds = %9
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21, %27
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%29 = call i32 @read(i32 %16, i8* %7, i32 4095)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %27, label %31
31: ; preds = %27, %21
%32 = call i32 @close(i32 %16)
ret i32 0
}