#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*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd, rd, i;
  char *buf;
  int bufsize = 4096;
  if (argc < 2) {
    printf("\nUsage: %s filename [,filename2 [,...] ] ]\n\n", argv[0]);
    return 0;
  }
  buf = malloc(bufsize);
  if (buf == 0) {
    printf("Malloc failed on %d\n", bufsize);
    return 0;
  }
  for (i = 1; i < argc; i++) {
    fd = open(argv[i], O_RDONLY);
    if (fd == -1) {
      printf("Open failed (%s)\n", strerror(errno));
      return 0;
    }
    while ((rd = read(fd, buf, bufsize)) == bufsize)
      ;
    if (rd == -1) {
      printf("Read failed (%s)\n", strerror(errno));
      return 0;
    }
    close(fd);
  }
  free(buf);
  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)
movl   $0x1000,-0x2c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d1 <main+0x161>
mov    -0x2c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ec <main+0x7c>
mov    -0x2c(%rbp),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d1 <main+0x161>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12c1 <main+0x151>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1251 <main+0xe1>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d1 <main+0x161>
jmp    1256 <main+0xe6>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    -0x2c(%rbp),%eax
jne    1276 <main+0x106>
jmp    1256 <main+0xe6>
cmpl   $0xffffffff,-0x18(%rbp)
jne    12ab <main+0x13b>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d1 <main+0x161>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f3 <main+0x83>
mov    -0x28(%rbp),%rdi
call   1030 <free@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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     1192 <main+0x22>
mov    (%r14),%rsi
lea    0xe73(%rip),%rdi
jmp    122d <main+0xbd>
mov    %edi,%ebp
mov    $0x1000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1245 <main+0xd5>
mov    %rax,%rbx
mov    %ebp,%r15d
mov    $0x1,%r12d
mov    0x2e2e(%rip),%r13
mov    (%r14,%r12,8),%rdi
mov    0x0(%r13),%esi
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     125a <main+0xea>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
cmp    $0x1000,%eax
je     11e0 <main+0x70>
cmp    $0xffffffff,%eax
je     1215 <main+0xa5>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%r12
cmp    %r15,%r12
jne    11ba <main+0x4a>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1234 <main+0xc4>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde1(%rip),%rdi
mov    $0x1000,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1234 <main+0xc4>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd3(%rip),%rdi
jmp    122a <main+0xba>

LLVM IR (-O0):
@.str = [45 x i8] c"\0AUsage: %s filename [,filename2 [,...] ] ]\0A\0A\00"
@.str.1 = [21 x i8] c"Malloc failed on %d\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [18 x i8] c"Open failed (%s)\0A\00"
@errno = global i32 0
@.str.3 = [18 x i8] c"Read failed (%s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4096, i32* %10
%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 ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 0, i32* %3
br label %70
18: ; preds = %2
%19 = load i32, i32* %10
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %10
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 0, i32* %3
br label %70
26: ; preds = %18
store i32 1, i32* %8
br label %27
27: ; preds = %64, %26
%28 = load i32, i32* %8
%29 = load i32, i32* %4
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %67
31: ; preds = %27
%32 = load i8**, i8*** %5
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_RDONLY
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %31
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %43)
store i32 0, i32* %3
br label %70
45: ; preds = %31
br label %46
46: ; preds = %53, %45
%47 = load i32, i32* %6
%48 = load i8*, i8** %9
%49 = load i32, i32* %10
%50 = call i32 @read(i32 %47, i8* %48, i32 %49)
store i32 %50, i32* %7
%51 = load i32, i32* %10
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %46
br label %46
54: ; preds = %46
%55 = load i32, i32* %7
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %59)
store i32 0, i32* %3
br label %70
61: ; preds = %54
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %27
67: ; preds = %27
%68 = load i8*, i8** %9
%69 = call i32 @free(i8* %68)
store i32 0, i32* %3
br label %70
70: ; preds = %67, %57, %41, %23, %13
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [45 x i8] c"\0AUsage: %s filename [,filename2 [,...] ] ]\0A\0A\00"
@.str.1 = [21 x i8] c"Malloc failed on %d\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [18 x i8] c"Open failed (%s)\0A\00"
@errno = global i32 0
@.str.3 = [18 x i8] c"Read failed (%s)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %37
7: ; preds = %2
%8 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = zext i32 %0 to i64
br label %14
12: ; preds = %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 4096)
br label %37
14: ; preds = %10, %31
%15 = phi i64 [ 1, %10 ], [ %33, %31 ]
%16 = getelementptr inbounds i8*, i8** %1, i64 %15
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %37
25: ; preds = %14, %25
%26 = tail call i32 @read(i32 %19, i8* %8, i32 4096)
switch i32 %26, label %31 [
i32 4096, label %25
i32 -1, label %27
]
27: ; preds = %25
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %37
31: ; preds = %25
%32 = tail call i32 @close(i32 %19)
%33 = add nuw nsw i64 %15, 1
%34 = icmp eq i64 %33, %11
br i1 %34, label %35, label %14
35: ; preds = %31
%36 = tail call i32 @free(i8* %8)
br label %37
37: ; preds = %35, %27, %21, %12, %4
ret i32 0
}