#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 */
size_t assemble(int /*<<< orphan*/ *, unsigned char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ output(int /*<<< orphan*/ *, unsigned char *, size_t);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  if (argc >= 2) {
    FILE *infile;
    infile = fopen(argv[1], "r");
    size_t outlen;
    unsigned char *out;
    outlen = assemble(infile, &out);
    fclose(infile);
    FILE *outfile;
    char outfilename[64];
    char ext[8];
    sscanf(argv[1], "%[^.].%s", outfilename, ext);
    strcpy(ext, "hack");
    sprintf(outfilename, "%s.%s", outfilename, ext);
    outfile = fopen(outfilename, "w");
    output(outfile, out, outlen);
    fclose(outfile);
    free(out);
  } else {
    printf("Usage: %s [filename]\n", argv[0]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1265 <main+0xd5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <assemble@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x70(%rbp),%rdx
lea    -0x78(%rbp),%rcx
lea    0xe06(%rip),%rsi
call   1090 <sscanf@plt>
lea    -0x78(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x70(%rbp),%rdi
lea    -0x70(%rbp),%rdx
lea    -0x78(%rbp),%rcx
lea    0xdec(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <output@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1278 <main+0xe8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xda5(%rip),%rdi
call   1070 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jl     1246 <main+0xb6>
mov    0x8(%rbx),%rdi
lea    0xe50(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
mov    %rsp,%rsi
mov    %rax,%rdi
call   1080 <assemble@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    0x8(%rbx),%rdi
lea    0xe29(%rip),%rsi
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r15
mov    %rbx,%rdx
mov    %r15,%rcx
call   1090 <sscanf@plt>
lea    0xe16(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
call   10b0 <sprintf@plt>
lea    0xdfd(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    (%rsp),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1050 <output@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    1255 <main+0xc5>
mov    (%rbx),%rsi
lea    0xdc8(%rip),%rdi
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [9 x i8] c"%[^.].%s\00"
@.str.2 = [5 x i8] c"hack\00"
@.str.3 = [6 x i8] c"%s.%s\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [22 x i8] c"Usage: %s [filename]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca [64 x i8]
%11 = alloca [8 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 2
br i1 %13, label %14, label %45
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = call i64 @assemble(i32* %19, i8** %8)
store i64 %20, i64* %7
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%28 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %27)
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%30 = call i32 @strcpy(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%34 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* %33)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %36, i32** %9
%37 = load i32*, i32** %9
%38 = load i8*, i8** %8
%39 = load i64, i64* %7
%40 = call i32 @output(i32* %37, i8* %38, i64 %39)
%41 = load i32*, i32** %9
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %8
%44 = call i32 @free(i8* %43)
br label %50
45: ; preds = %2
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %45, %14
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [9 x i8] c"%[^.].%s\00"
@.str.2 = [5 x i8] c"hack\00"
@.str.3 = [6 x i8] c"%s.%s\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [22 x i8] c"Usage: %s [filename]\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca [64 x i8]
%5 = alloca [8 x i8]
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %26
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = bitcast i8** %3 to i8*
%12 = call i64 @assemble(i32* %10, i8** %3)
%13 = call i32 @fclose(i32* %10)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%16 = load i8*, i8** %8
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15)
%18 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %14, i8* %15)
%20 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%21 = load i8*, i8** %3
%22 = call i32 @output(i32* %20, i8* %21, i64 %12)
%23 = call i32 @fclose(i32* %20)
%24 = load i8*, i8** %3
%25 = call i32 @free(i8* %24)
br label %29
26: ; preds = %2
%27 = load i8*, i8** %1
%28 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %7
ret i32 0
}