#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 *);
scalar_t__ 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 = (char *)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 %71
18: ; preds = %2
%19 = load i32, i32* %10
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* %10
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 0, i32* %3
br label %71
27: ; preds = %18
store i32 1, i32* %8
br label %28
28: ; preds = %65, %27
%29 = load i32, i32* %8
%30 = load i32, i32* %4
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %68
32: ; preds = %28
%33 = load i8**, i8*** %5
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = load i32, i32* @O_RDONLY
%39 = call i32 @open(i8* %37, i32 %38)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %32
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i32 0, i32* %3
br label %71
46: ; preds = %32
br label %47
47: ; preds = %54, %46
%48 = load i32, i32* %6
%49 = load i8*, i8** %9
%50 = load i32, i32* %10
%51 = call i32 @read(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %7
%52 = load i32, i32* %10
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %47
br label %47
55: ; preds = %47
%56 = load i32, i32* %7
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %60)
store i32 0, i32* %3
br label %71
62: ; preds = %55
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %28
68: ; preds = %28
%69 = load i8*, i8** %9
%70 = call i32 @free(i8* %69)
store i32 0, i32* %3
br label %71
71: ; preds = %68, %58, %42, %24, %13
%72 = load i32, i32* %3
ret i32 %72
}

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 %38
7: ; preds = %2
%8 = tail call i64 @malloc(i32 4096)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = zext i32 %0 to i64
br label %15
13: ; preds = %7
%14 = 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 %38
15: ; preds = %11, %32
%16 = phi i64 [ 1, %11 ], [ %34, %32 ]
%17 = getelementptr inbounds i8*, i8** %1, i64 %16
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %38
26: ; preds = %15, %26
%27 = tail call i32 @read(i32 %20, i8* %9, i32 4096)
switch i32 %27, label %32 [
i32 4096, label %26
i32 -1, label %28
]
28: ; preds = %26
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %38
32: ; preds = %26
%33 = tail call i32 @close(i32 %20)
%34 = add nuw nsw i64 %16, 1
%35 = icmp eq i64 %34, %12
br i1 %35, label %36, label %15
36: ; preds = %32
%37 = tail call i32 @free(i8* %9)
br label %38
38: ; preds = %36, %28, %22, %13, %4
ret i32 0
}