#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_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int main(int argc, char *argv[]) {
  char *filename = NULL;
  int fd = 0;
  int count = 0;
  char *buffer = malloc(1000);
  if (argc < 2) {
    printf("wrong usage, %s filename [count]\n", argv[0]);
    return -1;
  }
  filename = argv[1];
  count = (argc == 3) ? atoi(argv[2]) : 100;
  fd = open(filename, O_RDWR);
  if (fd == -1) {
    printf("couldnt open the file\n");
    return -2;
  }
  if (read(fd, buffer, count) < 0) {
    printf("couldnt read anything m8\n");
    return -3;
  }
  printf("%s\n", buffer);
  close(fd);
  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)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x3e8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c5 <main+0x65>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <main+0x130>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11f0 <main+0x90>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x2c(%rbp)
jmp    11fd <main+0x9d>
mov    $0x64,%eax
mov    %eax,-0x2c(%rbp)
jmp    11fd <main+0x9d>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    123c <main+0xdc>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1290 <main+0x130>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    126f <main+0x10f>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1290 <main+0x130>
mov    -0x28(%rbp),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@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   %r12
push   %rbx
mov    %rsi,%rbp
mov    %edi,%ebx
mov    $0x3e8,%edi
call   1070 <malloc@plt>
cmp    $0x1,%ebx
jg     11a5 <main+0x35>
mov    0x0(%rbp),%rsi
lea    0xe69(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    1224 <main+0xb4>
mov    %rax,%r15
mov    0x8(%rbp),%r12
mov    $0x64,%r14d
cmp    $0x3,%ebx
jne    11c3 <main+0x53>
mov    0x10(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1200 <main+0x90>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
test   %rax,%rax
js     1213 <main+0xa3>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1224 <main+0xb4>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%eax
jmp    1224 <main+0xb4>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"wrong usage, %s filename [count]\0A\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"couldnt open the file\0A\00"
@.str.2 = [26 x i8] c"couldnt read anything m8\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = call i8* @malloc(i32 1000)
store i8* %10, i8** %9
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %52
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 3
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
br label %30
29: ; preds = %18
br label %30
30: ; preds = %29, %24
%31 = phi i32 [ %28, %24 ], [ 100, %29 ]
store i32 %31, i32* %8
%32 = load i8*, i8** %6
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %52
39: ; preds = %30
%40 = load i32, i32* %7
%41 = load i8*, i8** %9
%42 = load i32, i32* %8
%43 = call i64 @read(i32 %40, i8* %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %3
br label %52
47: ; preds = %39
%48 = load i8*, i8** %9
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 0, i32* %3
br label %52
52: ; preds = %47, %45, %37, %13
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [34 x i8] c"wrong usage, %s filename [count]\0A\00"
@O_RDWR = global i32 0
@str = [25 x i8] c"couldnt read anything m8\00"
@str.4 = [22 x i8] c"couldnt open the file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(1000) i8* @malloc(i32 1000)
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %31
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i32 %0, 3
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
br label %16
16: ; preds = %8, %12
%17 = phi i32 [ %15, %12 ], [ 100, %8 ]
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* %10, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.4, i64 0, i64 0))
br label %31
23: ; preds = %16
%24 = tail call i64 @read(i32 %19, i8* %3, i32 %17)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %31
28: ; preds = %23
%29 = tail call i32 @puts(i8* dereferenceable(1) %3)
%30 = tail call i32 @close(i32 %19)
br label %31
31: ; preds = %28, %26, %21, %5
%32 = phi i32 [ -1, %5 ], [ -2, %21 ], [ -3, %26 ], [ 0, %28 ]
ret i32 %32
}