#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(void *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
unsigned char *read_file(char *, long *);
int main(int argc, char *argv[]) {
  char *array_name = "array";
  FILE *fp;
  long size, size2;
  unsigned char *buf, *buf2;
  long i, j;
  if (argc != 4) {
    printf("CONCAT by Cyber Warrior X\n");
    printf("usage: concat <source binary file 1> <source binary file 2> "
           "<destination source file>");
    exit(1);
  }
  buf = read_file(argv[1], &size);
  buf2 = read_file(argv[2], &size2);
  if ((fp = fopen(argv[3], "wb")) == NULL) {
    printf("Error opening %s for writing", argv[3]);
    exit(1);
  }
  fwrite((void *)buf, 1, size, fp);
  fwrite((void *)buf2, 1, size2, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x4,-0x4(%rbp)
je     11aa <main+0x4a>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <read_file@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <read_file@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1216 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x4,%edi
jne    11ff <main+0x8f>
mov    %rsi,%r12
mov    0x8(%rsi),%rdi
lea    0x10(%rsp),%rsi
call   1060 <read_file@plt>
mov    %rax,%r14
mov    0x10(%r12),%rdi
lea    0x8(%rsp),%rsi
call   1060 <read_file@plt>
mov    %rax,%r15
mov    0x18(%r12),%rdi
lea    0xea4(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1223 <main+0xb3>
mov    %rax,%rbx
mov    0x10(%rsp),%rdx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
lea    0xdee(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x18(%r12),%rsi
lea    0xe2a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"array\00"
@.str.1 = [27 x i8] c"CONCAT by Cyber Warrior X\0A\00"
@.str.2 = [86 x i8] c"usage: concat <source binary file 1> <source binary file 2> <destination source file>\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [29 x i8] c"Error opening %s for writing\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %5
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %4
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i8* @read_file(i8* %22, i64* %7)
store i8* %23, i8** %9
%24 = load i8**, i8*** %4
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i8* @read_file(i8* %26, i64* %8)
store i8* %27, i8** %10
%28 = load i8**, i8*** %4
%29 = getelementptr inbounds i8*, i8** %28, i64 3
%30 = load i8*, i8** %29
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %31, i32** %6
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %39
33: ; preds = %19
%34 = load i8**, i8*** %4
%35 = getelementptr inbounds i8*, i8** %34, i64 3
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %19
%40 = load i8*, i8** %9
%41 = load i64, i64* %7
%42 = load i32*, i32** %6
%43 = call i32 @fwrite(i8* %40, i32 1, i64 %41, i32* %42)
%44 = load i8*, i8** %10
%45 = load i64, i64* %8
%46 = load i32*, i32** %6
%47 = call i32 @fwrite(i8* %44, i32 1, i64 %45, i32* %46)
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [86 x i8] c"usage: concat <source binary file 1> <source binary file 2> <destination source file>\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [29 x i8] c"Error opening %s for writing\00"
@str = [26 x i8] c"CONCAT by Cyber Warrior X\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = icmp eq i32 %0, 4
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i8* @read_file(i8* %14, i64* %3)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = call i8* @read_file(i8* %17, i64* %4)
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %12
%24 = load i8*, i8** %19
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %12
%28 = load i64, i64* %3
%29 = call i32 @fwrite(i8* %15, i32 1, i64 %28, i32* %21)
%30 = load i64, i64* %4
%31 = call i32 @fwrite(i8* %18, i32 1, i64 %30, i32* %21)
%32 = call i32 @fclose(i32* %21)
ret i32 0
}