#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 __off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_IRUSR;
int /*<<< orphan*/ close(int);
int fSize(char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ write(int, char *, size_t);
void concatenate(char *fileName) {
  int fileDesc;
  char *buffer;
  fileDesc = open(fileName, O_RDONLY, S_IRUSR);
  if (fileDesc == -1) {
    printf("ERROR: Invalid file\n");
    return;
  }
  __off_t fileSize;
  if ((fileSize = fSize(fileName)) != -1) {
    buffer = malloc((size_t)fileSize);
    if (buffer == NULL) {
      printf("ERROR: Could not allocate enough memory to the buffer\n");
      return;
    }
    while (read(fileDesc, buffer, (size_t)fileSize) > 0) {
      write(1, buffer, (size_t)fileSize);
    }
    if (read(fileDesc, buffer, (size_t)fileSize) < 0) {
      printf("ERROR: Something went wrong while reading the file.\n");
    } else {
      close(fileDesc);
      free(buffer);
    }
  } else {
    printf("ERROR: Could not determine file size\n");
  }
}


x86 assembly (clang, -O0):
<concatenate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c5 <concatenate+0x45>
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
jmp    1286 <concatenate+0x106>
mov    -0x8(%rbp),%rdi
call   1090 <fSize@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     127a <concatenate+0xfa>
movslq -0x1c(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1203 <concatenate+0x83>
lea    0xe1c(%rip),%rdi
call   1050 <printf@plt>
jmp    1286 <concatenate+0x106>
jmp    1208 <concatenate+0x88>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rdx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1239 <concatenate+0xb9>
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rdx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    1208 <concatenate+0x88>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rdx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    1264 <concatenate+0xe4>
lea    0xdf2(%rip),%rdi
call   1050 <printf@plt>
jmp    1275 <concatenate+0xf5>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    1286 <concatenate+0x106>
lea    0xe00(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concatenate>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     11f5 <concatenate+0x75>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1090 <fSize@plt>
cmp    $0xffffffff,%eax
je     1225 <concatenate+0xa5>
movslq %eax,%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     122e <concatenate+0xae>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <read@plt>
test   %rax,%rax
jle    11fe <concatenate+0x7e>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <write@plt>
jmp    11d0 <concatenate+0x50>
lea    0xe93(%rip),%rdi
jmp    123e <concatenate+0xbe>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <read@plt>
test   %rax,%rax
js     1237 <concatenate+0xb7>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <free@plt>
lea    0xdd4(%rip),%rdi
jmp    123e <concatenate+0xbe>
lea    0xe24(%rip),%rdi
jmp    123e <concatenate+0xbe>
lea    0xde7(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <puts@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str = [21 x i8] c"ERROR: Invalid file\0A\00"
@.str.1 = [55 x i8] c"ERROR: Could not allocate enough memory to the buffer\0A\00"
@.str.2 = [53 x i8] c"ERROR: Something went wrong while reading the file.\0A\00"
@.str.3 = [38 x i8] c"ERROR: Could not determine file size\0A\00"
define void @concatenate(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @S_IRUSR
%9 = call i32 @open(i8* %6, i32 %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %56
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 @fSize(i8* %15)
store i32 %16, i32* %5
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %54
18: ; preds = %14
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
br label %56
26: ; preds = %18
br label %27
27: ; preds = %34, %26
%28 = load i32, i32* %3
%29 = load i8*, i8** %4
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = call i64 @read(i32 %28, i8* %29, i64 %31)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %27
%35 = load i8*, i8** %4
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = call i32 @write(i32 1, i8* %35, i64 %37)
br label %27
39: ; preds = %27
%40 = load i32, i32* %3
%41 = load i8*, i8** %4
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = call i64 @read(i32 %40, i8* %41, i64 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
br label %53
48: ; preds = %39
%49 = load i32, i32* %3
%50 = call i32 @close(i32 %49)
%51 = load i8*, i8** %4
%52 = call i32 @free(i8* %51)
br label %53
53: ; preds = %48, %46
br label %56
54: ; preds = %14
%55 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %12, %24, %54, %53
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@str = [37 x i8] c"ERROR: Could not determine file size\00"
@str.4 = [52 x i8] c"ERROR: Something went wrong while reading the file.\00"
@str.5 = [54 x i8] c"ERROR: Could not allocate enough memory to the buffer\00"
@str.6 = [20 x i8] c"ERROR: Invalid file\00"
define void @concatenate(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @S_IRUSR
%4 = tail call i32 @open(i8* %0, i32 %2, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %34
8: ; preds = %1
%9 = tail call i32 @fSize(i8* %0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %32, label %11
11: ; preds = %8
%12 = sext i32 %9 to i64
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = tail call i64 @read(i32 %4, i8* %13, i64 %12)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %20, label %24
18: ; preds = %11
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.5, i64 0, i64 0))
br label %34
20: ; preds = %15, %20
%21 = tail call i32 @write(i32 1, i8* %13, i64 %12)
%22 = tail call i64 @read(i32 %4, i8* %13, i64 %12)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %20, label %24
24: ; preds = %20, %15
%25 = tail call i64 @read(i32 %4, i8* %13, i64 %12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.4, i64 0, i64 0))
br label %34
29: ; preds = %24
%30 = tail call i32 @close(i32 %4)
%31 = tail call i32 @free(i8* %13)
br label %34
32: ; preds = %8
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %18, %27, %29, %32, %6
ret void
}