#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 */
char EOF;
int calcConstSize();
int calcExecSize();
unsigned char *createBinary();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(unsigned char, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memmap(int /*<<< orphan*/, int, unsigned char *);
int /*<<< orphan*/ parseCode(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ realloc(char *, int);
int main(int argc, char *argv[]) {
  if (argc <= 1) {
    printf("pCPU Assembler\n");
    printf("Usage: %s [source] [binary]\n", argv[0]);
    return 0;
  }
  FILE *fp = fopen(argv[1], "r");
  if (!fp) {
    printf("Error: Cannot open file: %s\n", argv[1]);
    return 1;
  }
  int size = 0;
  char *s = (char *)malloc(1);
  char c;
  c = fgetc(fp);
  while (c != EOF) {
    size++;
    s = (char *)realloc(s, size);
    s[size - 1] = c;
    c = fgetc(fp);
  }
  if (!parseCode(s)) {
    printf("Fatal: Code parsing failed.\n");
  }
  unsigned char *binary = createBinary();
  memmap(0, 0x50, binary);
  FILE *fp2 = fopen(argv[2], "wb+");
  int d = calcExecSize() + calcConstSize() + 1;
  for (int i = 0; i < d; i++) {
    fputc(binary[i], fp2);
  }
  printf("[STATS]\tExec stack: %d bytes\tConstants stack: %d bytes\n",
         calcExecSize(), calcConstSize());
  fclose(fp);
  fclose(fp2);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     120f <main+0x4f>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a7 <main+0x1e7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0f(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1254 <main+0x94>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a7 <main+0x1e7>
movl   $0x0,-0x1c(%rbp)
mov    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
mov    0x2d68(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     12c8 <main+0x108>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10b0 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x29(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0x29(%rbp)
jmp    1275 <main+0xb5>
mov    -0x28(%rbp),%rdi
call   10d0 <parseCode@plt>
cmp    $0x0,%eax
jne    12e8 <main+0x128>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10a0 <createBinary@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
xor    %edi,%edi
mov    $0x50,%esi
call   10e0 <memmap@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd57(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <calcExecSize@plt>
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   1030 <calcConstSize@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    1371 <main+0x1b1>
mov    -0x38(%rbp),%rax
movslq -0x48(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x40(%rbp),%rsi
movzbl %al,%edi
call   1080 <fputc@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1340 <main+0x180>
mov    $0x0,%al
call   1040 <calcExecSize@plt>
mov    %eax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <calcConstSize@plt>
mov    -0x50(%rbp),%esi
mov    %eax,%edx
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%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,%rbp
cmp    $0x1,%edi
jg     1208 <main+0x38>
lea    0xea8(%rip),%rdi
call   1040 <puts@plt>
mov    0x0(%rbp),%rsi
lea    0xe06(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1343 <main+0x173>
mov    0x8(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     132c <main+0x15c>
mov    %rax,%r12
mov    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1080 <fgetc@plt>
mov    0x2da8(%rip),%r13
cmp    0x0(%r13),%al
je     127c <main+0xac>
mov    %eax,%ebx
mov    %rbp,(%rsp)
xor    %ebp,%ebp
xchg   %ax,%ax
lea    0x1(%rbp),%r14
mov    %r15,%rdi
mov    %r14d,%esi
call   10c0 <realloc@plt>
mov    %rax,%r15
mov    %bl,(%rax,%rbp,1)
mov    %r12,%rdi
call   1080 <fgetc@plt>
mov    %eax,%ebx
mov    %r14,%rbp
cmp    0x0(%r13),%al
jne    1250 <main+0x80>
mov    (%rsp),%rbp
mov    %r15,%rdi
call   10e0 <parseCode@plt>
test   %eax,%eax
jne    1294 <main+0xc4>
lea    0xde7(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10b0 <createBinary@plt>
mov    %rax,%r15
xor    %edi,%edi
mov    $0x50,%esi
mov    %rax,%rdx
call   10f0 <memmap@plt>
mov    0x10(%rbp),%rdi
lea    0xd82(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <calcExecSize@plt>
mov    %eax,%r13d
xor    %eax,%eax
call   1030 <calcConstSize@plt>
mov    %eax,%ebp
add    %r13d,%ebp
js     12f6 <main+0x126>
add    $0x1,%ebp
xor    %ebx,%ebx
nopl   (%rax)
movzbl (%r15,%rbx,1),%edi
mov    %r14,%rsi
call   1090 <fputc@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12e0 <main+0x110>
xor    %eax,%eax
call   1050 <calcExecSize@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <calcConstSize@plt>
lea    0xd31(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
xor    %ebx,%ebx
jmp    1343 <main+0x173>
mov    0x8(%rbp),%rsi
lea    0xce6(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"pCPU Assembler\0A\00"
@.str.1 = [29 x i8] c"Usage: %s [source] [binary]\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [29 x i8] c"Error: Cannot open file: %s\0A\00"
@EOF = global i8 0
@.str.4 = [29 x i8] c"Fatal: Code parsing failed.\0A\00"
@.str.5 = [4 x i8] c"wb+\00"
@.str.6 = [56 x i8] c"[STATS]\09Exec stack: %d bytes\09Constants stack: %d bytes\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 i8*
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sle i32 %14, 1
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 0, i32* %3
br label %101
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %6
%27 = load i32*, i32** %6
%28 = icmp ne i32* %27, null
br i1 %28, label %34, label %29
29: ; preds = %22
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %32)
store i32 1, i32* %3
br label %101
34: ; preds = %22
store i32 0, i32* %7
%35 = call i64 @malloc(i32 1)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %8
%37 = load i32*, i32** %6
%38 = call signext i8 @fgetc(i32* %37)
store i8 %38, i8* %9
br label %39
39: ; preds = %45, %34
%40 = load i8, i8* %9
%41 = sext i8 %40 to i32
%42 = load i8, i8* @EOF
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %41, %43
br i1 %44, label %45, label %60
45: ; preds = %39
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
%48 = load i8*, i8** %8
%49 = load i32, i32* %7
%50 = call i64 @realloc(i8* %48, i32 %49)
%51 = inttoptr i64 %50 to i8*
store i8* %51, i8** %8
%52 = load i8, i8* %9
%53 = load i8*, i8** %8
%54 = load i32, i32* %7
%55 = sub nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
store i8 %52, i8* %57
%58 = load i32*, i32** %6
%59 = call signext i8 @fgetc(i32* %58)
store i8 %59, i8* %9
br label %39
60: ; preds = %39
%61 = load i8*, i8** %8
%62 = call i32 @parseCode(i8* %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %66, label %64
64: ; preds = %60
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %60
%67 = call i8* (...) @createBinary()
store i8* %67, i8** %10
%68 = load i8*, i8** %10
%69 = call i32 @memmap(i32 0, i32 80, i8* %68)
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 2
%72 = load i8*, i8** %71
%73 = call i32* @fopen(i8* %72, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
store i32* %73, i32** %11
%74 = call i32 (...) @calcExecSize()
%75 = call i32 (...) @calcConstSize()
%76 = add nsw i32 %74, %75
%77 = add nsw i32 %76, 1
store i32 %77, i32* %12
store i32 0, i32* %13
br label %78
78: ; preds = %90, %66
%79 = load i32, i32* %13
%80 = load i32, i32* %12
%81 = icmp slt i32 %79, %80
br i1 %81, label %82, label %93
82: ; preds = %78
%83 = load i8*, i8** %10
%84 = load i32, i32* %13
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
%87 = load i8, i8* %86
%88 = load i32*, i32** %11
%89 = call i32 @fputc(i8 zeroext %87, i32* %88)
br label %90
90: ; preds = %82
%91 = load i32, i32* %13
%92 = add nsw i32 %91, 1
store i32 %92, i32* %13
br label %78
93: ; preds = %78
%94 = call i32 (...) @calcExecSize()
%95 = call i32 (...) @calcConstSize()
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i32 %94, i32 %95)
%97 = load i32*, i32** %6
%98 = call i32 @fclose(i32* %97)
%99 = load i32*, i32** %11
%100 = call i32 @fclose(i32* %99)
br label %101
101: ; preds = %93, %29, %16
%102 = load i32, i32* %3
ret i32 %102
}

LLVM IR (-O2):
@.str.1 = [29 x i8] c"Usage: %s [source] [binary]\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [29 x i8] c"Error: Cannot open file: %s\0A\00"
@EOF = global i8 0
@.str.5 = [4 x i8] c"wb+\00"
@.str.6 = [56 x i8] c"[STATS]\09Exec stack: %d bytes\09Constants stack: %d bytes\0A\00"
@str = [28 x i8] c"Fatal: Code parsing failed.\00"
@str.7 = [15 x i8] c"pCPU Assembler\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0))
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %68
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i8*, i8** %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %68
16: ; preds = %8
%17 = tail call i64 @malloc(i32 1)
%18 = inttoptr i64 %17 to i8*
%19 = tail call signext i8 @fgetc(i32* %11)
%20 = load i8, i8* @EOF
%21 = icmp eq i8 %19, %20
br i1 %21, label %36, label %22
22: ; preds = %16, %22
%23 = phi i64 [ %26, %22 ], [ 0, %16 ]
%24 = phi i8 [ %31, %22 ], [ %19, %16 ]
%25 = phi i8* [ %29, %22 ], [ %18, %16 ]
%26 = add nuw i64 %23, 1
%27 = trunc i64 %26 to i32
%28 = tail call i64 @realloc(i8* %25, i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = getelementptr inbounds i8, i8* %29, i64 %23
store i8 %24, i8* %30
%31 = tail call signext i8 @fgetc(i32* %11)
%32 = load i8, i8* @EOF
%33 = icmp eq i8 %31, %32
br i1 %33, label %34, label %22
34: ; preds = %22
%35 = inttoptr i64 %28 to i8*
br label %36
36: ; preds = %34, %16
%37 = phi i8* [ %18, %16 ], [ %35, %34 ]
%38 = tail call i32 @parseCode(i8* %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = tail call i8* (...) @createBinary()
%44 = tail call i32 @memmap(i32 0, i32 80, i8* %43)
%45 = getelementptr inbounds i8*, i8** %1, i64 2
%46 = load i8*, i8** %45
%47 = tail call i32* @fopen(i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%48 = tail call i32 (...) @calcExecSize()
%49 = tail call i32 (...) @calcConstSize()
%50 = add i32 %49, %48
%51 = icmp slt i32 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %42
%53 = add nuw i32 %50, 1
%54 = zext i32 %53 to i64
br label %61
55: ; preds = %61, %42
%56 = tail call i32 (...) @calcExecSize()
%57 = tail call i32 (...) @calcConstSize()
%58 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i32 %56, i32 %57)
%59 = tail call i32 @fclose(i32* %11)
%60 = tail call i32 @fclose(i32* %47)
br label %68
61: ; preds = %52, %61
%62 = phi i64 [ 0, %52 ], [ %66, %61 ]
%63 = getelementptr inbounds i8, i8* %43, i64 %62
%64 = load i8, i8* %63
%65 = tail call i32 @fputc(i8 zeroext %64, i32* %47)
%66 = add nuw nsw i64 %62, 1
%67 = icmp eq i64 %66, %54
br i1 %67, label %55, label %61
68: ; preds = %13, %55, %4
%69 = phi i32 [ 0, %4 ], [ 0, %55 ], [ 1, %13 ]
ret i32 %69
}