#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*/ 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 /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int my_file_desc;
  char file_name[4096];
  int i;
  int r;
  char *string;
  if (argc < 2) {
    printf("need to specify a file name\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);
  }
  printf("my_file_desc: %d\n", my_file_desc);
  string = "a string written to standard out\n";
  write(1, string, strlen(string));
  close(my_file_desc);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ab <main+0x3b>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x1024(%rbp)
movslq -0x1024(%rbp),%rax
cmp    $0x1000,%rax
jae    11eb <main+0x7b>
movslq -0x1024(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
jmp    11b5 <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    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124b <main+0xdb>
lea    -0x1020(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdeb(%rip),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x1030(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1038(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
cmp    $0x2,%edi
jl     122c <main+0x9c>
mov    %rsi,%rbx
mov    %rsp,%r14
mov    $0x1000,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xfff(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1242 <main+0xb2>
mov    %eax,%ebx
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe30(%rip),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdb7(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"need to specify a file name\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"failed to open %s for reading\0A\00"
@.str.2 = [18 x i8] c"my_file_desc: %d\0A\00"
@.str.3 = [34 x i8] c"a string written to standard out\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 i32
%9 = alloca i32
%10 = alloca i8*
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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
store i32 0, i32* %8
br label %17
17: ; preds = %25, %16
%18 = load i32, i32* %8
%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* %8
%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* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
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
%45 = load i32, i32* %6
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %45)
store i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8** %10
%47 = load i8*, i8** %10
%48 = load i8*, i8** %10
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @write(i32 1, i8* %47, i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
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 = [18 x i8] c"my_file_desc: %d\0A\00"
@.str.3 = [34 x i8] c"a string written to standard out\0A\00"
@str = [28 x i8] c"need to specify a file name\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @strncpy(i8* %4, i8* %9, i32 4096)
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4095
store i8 0, i8* %11
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %4, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %21
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %6
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %6
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%23 = call i32 @strlen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @write(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i32 @close(i32 %13)
ret i32 0
}