// File: /workspace/github_c_makefile_star2_O1/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 0x5bf2
leaq 0x1d5e(%rip), %rdi      # 0x794b
jmp 0x5dd9
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 0x1c4d(%rip), %rdi      # 0x7868
movq %r15, %rsi
xorl %eax, %eax
callq 0x2140
leaq 0x1c59(%rip), %rdi      # 0x7885
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2140
leaq 0x1c67(%rip), %rdi      # 0x78a6
movq %r13, %rsi
xorl %eax, %eax
callq 0x2140
movl %r12d, %edi
movl %ebp, %esi
callq 0x2a64
movq %rax, %rbx
movl $0x4000, %edi           # imm = 0x4000
callq 0x2220
movq %rax, %r14
testq %r13, %r13
je 0x5c75
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x20a0
movl $0x4000, %edx           # imm = 0x4000
movq %r14, %rdi
xorl %esi, %esi
callq 0x2170
leaq 0x1c28(%rip), %rsi      # 0x78b3
movq %r15, %rdi
callq 0x22a0
testq %rax, %rax
jne 0x5ca4
leaq 0x1c59(%rip), %rdi      # 0x78f8
jmp 0x5dd9
movq %rax, %r15
movl $0x10, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x5dd2
movl %ebp, 0x4(%rsp)
movq %r13, 0x8(%rsp)
testl %r12d, %r12d
jle 0x5d31
leaq 0x1bdb(%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 0x5de8
incl %r13d
cmpl %r13d, %r12d
jne 0x5ce3
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
jle 0x5d92
leaq 0x1b97(%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 0x5df4
incl %r12d
cmpl %r12d, %ebp
jne 0x5d48
cmpq $0x0, 0x8(%rsp)
je 0x5db7
leaq 0x20(%rsp), %rdi
movl $0xff, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x5e07
movq %r15, %rdi
callq 0x2100
leaq 0x1b4c(%rip), %rdi      # 0x7912
callq 0x20b0
xorl %edi, %edi
callq 0x2310
leaq 0x1b56(%rip), %rdi      # 0x792f
callq 0x20b0
movl $0x1, %edi
callq 0x2310
leaq 0x1ad0(%rip), %rdi      # 0x78bf
movl %r13d, %esi
jmp 0x5dfe
leaq 0x1ae5(%rip), %rdi      # 0x78e0
movl %r12d, %esi
xorl %eax, %eax
callq 0x2140
jmp 0x5dde
leaq 0x1b0a(%rip), %rdi      # 0x7918
jmp 0x5dd9