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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
void concatenateFiles(char *file, char **otherFiles, int numFiles) {
  FILE *f = fopen(file, "a");
  int i;
  for (i = 0; i < numFiles; i++) {
    FILE *other = fopen(otherFiles[i], "r");
    int val = 0;
    while ((val = fgetc(other)) != EOF) {
      fputc(val, f);
    }
    fclose(other);
    unlink(otherFiles[i]);
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<concatenateFiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1201 <concatenateFiles+0xb1>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe65(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x34(%rbp)
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%eax
je     11d9 <concatenateFiles+0x89>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
jmp    11ad <concatenateFiles+0x5d>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <unlink@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117e <concatenateFiles+0x2e>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concatenateFiles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
test   %ebx,%ebx
jle    11d9 <concatenateFiles+0x89>
mov    %ebx,%r13d
xor    %ebp,%ebp
mov    0x2e69(%rip),%r15
jmp    11aa <concatenateFiles+0x5a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%r14,%rbp,8),%rdi
call   1030 <unlink@plt>
add    $0x1,%rbp
cmp    %r13,%rbp
je     11d9 <concatenateFiles+0x89>
mov    (%r14,%rbp,8),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%eax
je     1190 <concatenateFiles+0x40>
mov    %eax,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
mov    %rbx,%rdi
jmp    11c0 <concatenateFiles+0x70>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define void @concatenateFiles(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
store i32 0, i32* %8
br label %13
13: ; preds = %42, %3
%14 = load i32, i32* %8
%15 = load i32, i32* %6
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %45
17: ; preds = %13
%18 = load i8**, i8*** %5
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %9
store i32 0, i32* %10
br label %24
24: ; preds = %29, %17
%25 = load i32*, i32** %9
%26 = call i32 @fgetc(i32* %25)
store i32 %26, i32* %10
%27 = load i32, i32* @EOF
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* %10
%31 = load i32*, i32** %7
%32 = call i32 @fputc(i32 %30, i32* %31)
br label %24
33: ; preds = %24
%34 = load i32*, i32** %9
%35 = call i32 @fclose(i32* %34)
%36 = load i8**, i8*** %5
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 @unlink(i8* %40)
br label %42
42: ; preds = %33
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %13
45: ; preds = %13
%46 = load i32*, i32** %7
%47 = call i32 @fclose(i32* %46)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"a\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define void @concatenateFiles(i8* nocapture %0, i8** nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %28
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %22
%9 = phi i64 [ 0, %6 ], [ %26, %22 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %9
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @fgetc(i32* %12)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
br i1 %15, label %22, label %16
16: ; preds = %8, %16
%17 = phi i32 [ %19, %16 ], [ %13, %8 ]
%18 = tail call i32 @fputc(i32 %17, i32* %4)
%19 = tail call i32 @fgetc(i32* %12)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %16
22: ; preds = %16, %8
%23 = tail call i32 @fclose(i32* %12)
%24 = load i8*, i8** %10
%25 = tail call i32 @unlink(i8* %24)
%26 = add nuw nsw i64 %9, 1
%27 = icmp eq i64 %26, %7
br i1 %27, label %28, label %8
28: ; preds = %22, %3
%29 = tail call i32 @fclose(i32* %4)
ret void
}