#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);
  printf("file: %s contains the string: %s\n", file_name, read_buffer);
  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)
lea    -0x1020(%rbp),%rsi
lea    -0x2020(%rbp),%rdx
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x2008,%rsp
cmp    $0x2,%edi
jl     1228 <main+0xa8>
mov    %rsi,%rbx
mov    %rsp,%r14
mov    $0x1000,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xfff(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     123e <main+0xbe>
mov    %eax,%ebx
lea    0x1000(%rsp),%r14
mov    $0x1000,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xfff,%edx
call   1080 <read@plt>
lea    0xe18(%rip),%rdi
mov    %rsp,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdbb(%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 = [34 x i8] c"file: %s contains the string: %s\0A\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
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %60, i8* %61)
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
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 = [34 x i8] c"file: %s contains the string: %s\0A\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]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %17, label %8
8: ; preds = %2
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @strncpy(i8* %5, i8* %11, i32 4096)
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4095
store i8 0, i8* %13
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %5, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %25, label %20
17: ; preds = %2
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%22 = call i32 @read(i32 %15, i8* %6, i32 4095)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %6)
%24 = call i32 @close(i32 %15)
ret i32 0
25: ; preds = %8
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%27 = call i32 @exit(i32 1)
unreachable
}