// File: /workspace/github_c_makefile_star2_O3/sadistech[P]nesromtool2/./src/rombuilder_main.c
// Function: main
int main(int argc, char** argv) {
  if (argc < 3) {
    printf("Not enough args.\n");
    exit(EXIT_FAILURE);
  }

  char *rompath = argv[1];
  char *in_prg_count = argv[2];
  char *in_chr_count = argv[3];
  char *in_title = argv[4];

  int prg_count = atoi(in_prg_count);
  int chr_count = atoi(in_chr_count);

  printf("Will create a new ROM @ %s:\n", rompath);
  printf("  %d PRG banks.\n  %d CHR banks.\n", prg_count, chr_count);
  printf("  Title: %s\n", in_title);

  nrt_header* header = nrt_header_create(prg_count, chr_count);
  nrt_prgbank* prg = NRT_PRG_ALLOC;
  nrt_chrbank* chr = NRT_CHR_ALLOC;
  char title[NRT_TITLE_MAX_LENGTH];

  if (in_title) {
    strcpy(title, in_title);
  }

  bzero(prg->data, NRT_PRG_BANK_SIZE);
  bzero(chr->tile, NRT_CHR_BANK_SIZE);

  // open the file

  FILE* rom = NULL;

  if (!(rom = fopen(rompath, "w+"))) {
    printf("Failed to write the file.\n");
    exit(EXIT_FAILURE);
  }

  // write the header
  if (fwrite(header, NRT_HEADER_SIZE, 1, rom) != 1) {
    printf("Failed to write the header.\n");
    exit(EXIT_FAILURE);
  }

  // write the banks
  char memo[10];

  int i;
  for (i = 0; i < prg_count; i++ ) {
    sprintf(memo, "PRG %02d", i);
    memcpy(prg->data, memo, strlen(memo) + 1);

    if (fwrite(prg, NRT_PRG_BANK_SIZE, 1, rom) != 1) {
      printf("Failed to write PRG %d\n", i);
      exit(EXIT_FAILURE);
    }
  }

  for (i = 0; i < chr_count; i++ ) {
    sprintf(memo, "CHR %02d", i);
    memcpy(prg->data, memo, strlen(memo) + 1);

    if (fwrite(prg, NRT_CHR_BANK_SIZE, 1, rom) != 1) {
      printf("Failed to write CHR %d\n", i);
      exit(EXIT_FAILURE);
    }

  }

  // write the title
  if (in_title) {
    if (fwrite(title, NRT_TITLE_MAX_LENGTH, 1, rom) != 1) {
      printf("Failed to write title.\n");
      exit(EXIT_FAILURE);
    }
  }

  fclose(rom);

  printf("Done!\n");
  exit(EXIT_SUCCESS);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
cmpl $0x2, %edi
jg 0x5b86
leaq 0x1dca(%rip), %rdi      # 0x794b
jmp 0x5d6d
movq 0x8(%rsi), %r15
movq 0x10(%rsi), %rdi
movq 0x18(%rsi), %rbx
movq 0x20(%rsi), %r13
callq 0x22e0
movl %eax, %r12d
movq %rbx, %rdi
callq 0x22e0
movl %eax, %ebp
leaq 0x1cb9(%rip), %rdi      # 0x7868
movq %r15, %rsi
xorl %eax, %eax
callq 0x2140
leaq 0x1cc5(%rip), %rdi      # 0x7885
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2140
leaq 0x1cd3(%rip), %rdi      # 0x78a6
movq %r13, %rsi
xorl %eax, %eax
callq 0x2140
movl %r12d, %edi
movl %ebp, %esi
callq 0x2a74
movq %rax, %rbx
movl $0x4000, %edi           # imm = 0x4000
callq 0x2220
movq %rax, %r14
testq %r13, %r13
je 0x5c09
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x20a0
movl $0x4000, %edx           # imm = 0x4000
movq %r14, %rdi
xorl %esi, %esi
callq 0x2170
leaq 0x1c94(%rip), %rsi      # 0x78b3
movq %r15, %rdi
callq 0x22a0
testq %rax, %rax
jne 0x5c38
leaq 0x1cc5(%rip), %rdi      # 0x78f8
jmp 0x5d6d
movq %rax, %r15
movl $0x10, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x5d66
movl %ebp, 0x4(%rsp)
movq %r13, 0x8(%rsp)
testl %r12d, %r12d
jle 0x5cc5
leaq 0x1c47(%rip), %rbx      # 0x78b6
leaq 0x16(%rsp), %rbp
xorl %r13d, %r13d
movq %rbp, %rdi
movq %rbx, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2300
movq %rbp, %rdi
callq 0x2110
leaq 0x1(%rax), %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x21f0
movl $0x4000, %esi           # imm = 0x4000
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x5d7c
incl %r13d
cmpl %r13d, %r12d
jne 0x5c77
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
jle 0x5d26
leaq 0x1c03(%rip), %rbx      # 0x78d7
leaq 0x16(%rsp), %r13
xorl %r12d, %r12d
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2300
movq %r13, %rdi
callq 0x2110
leaq 0x1(%rax), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x21f0
movl $0x2000, %esi           # imm = 0x2000
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x5d88
incl %r12d
cmpl %r12d, %ebp
jne 0x5cdc
cmpq $0x0, 0x8(%rsp)
je 0x5d4b
leaq 0x20(%rsp), %rdi
movl $0xff, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x5d9b
movq %r15, %rdi
callq 0x2100
leaq 0x1bb8(%rip), %rdi      # 0x7912
callq 0x20b0
xorl %edi, %edi
callq 0x2310
leaq 0x1bc2(%rip), %rdi      # 0x792f
callq 0x20b0
movl $0x1, %edi
callq 0x2310
leaq 0x1b3c(%rip), %rdi      # 0x78bf
movl %r13d, %esi
jmp 0x5d92
leaq 0x1b51(%rip), %rdi      # 0x78e0
movl %r12d, %esi
xorl %eax, %eax
callq 0x2140
jmp 0x5d72
leaq 0x1b76(%rip), %rdi      # 0x7918
jmp 0x5d6d