// File: /workspace/github_c_makefile_star2_O2/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 0x53d2
leaq 0x257e(%rip), %rdi      # 0x794b
jmp 0x5517
movq 0x8(%rsi), %r15
movq 0x10(%rsi), %rdi
movq 0x18(%rsi), %rbx
movq 0x20(%rsi), %r12
callq 0x22e0
movl %eax, %ebp
movq %rbx, %rdi
callq 0x22e0
movl %eax, %ebx
leaq 0x246e(%rip), %rdi      # 0x7868
movq %r15, %rsi
xorl %eax, %eax
callq 0x2140
leaq 0x247a(%rip), %rdi      # 0x7885
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2140
leaq 0x2489(%rip), %rdi      # 0x78a6
movq %r12, %rsi
xorl %eax, %eax
callq 0x2140
movl %ebp, %r13d
movl %ebp, %edi
movl %ebx, 0x4(%rsp)
movl %ebx, %esi
callq 0x25c4
movq %rax, %rbx
movl $0x4000, %edi           # imm = 0x4000
callq 0x2220
movq %rax, %r14
testq %r12, %r12
je 0x5459
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x20a0
movl $0x4000, %edx           # imm = 0x4000
movq %r14, %rdi
xorl %esi, %esi
callq 0x2170
leaq 0x2444(%rip), %rsi      # 0x78b3
movq %r15, %rdi
callq 0x22a0
testq %rax, %rax
jne 0x5488
leaq 0x2475(%rip), %rdi      # 0x78f8
jmp 0x5517
movq %rax, %r15
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x5510
movq %r12, 0x8(%rsp)
xorl %eax, %eax
movl %r13d, %ebx
testl %r13d, %r13d
cmovlel %eax, %ebx
leaq 0x16(%rsp), %rbp
pushq $0x1
popq %r12
xorl %r13d, %r13d
cmpl %r13d, %ebx
je 0x5524
movq %rbp, %rdi
leaq 0x23e9(%rip), %rsi      # 0x78b6
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
movq %r14, %rdi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x558b
incl %r13d
jmp 0x54be
leaq 0x2418(%rip), %rdi      # 0x792f
callq 0x20b0
pushq $0x1
popq %rdi
callq 0x2310
xorl %eax, %eax
movl 0x4(%rsp), %r13d
testl %r13d, %r13d
cmovlel %eax, %r13d
leaq 0x16(%rsp), %rbx
pushq $0x1
popq %rbp
xorl %r12d, %r12d
cmpl %r12d, %r13d
je 0x5597
movq %rbx, %rdi
leaq 0x238b(%rip), %rsi      # 0x78d7
movl %r12d, %edx
xorl %eax, %eax
callq 0x2300
movq %rbx, %rdi
callq 0x2110
leaq 0x1(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21f0
movl $0x2000, %esi           # imm = 0x2000
movq %r14, %rdi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x55d5
incl %r12d
jmp 0x553d
leaq 0x232d(%rip), %rdi      # 0x78bf
movl %r13d, %esi
jmp 0x55df
cmpq $0x0, 0x8(%rsp)
je 0x55ba
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rdx
movl $0xff, %esi
movq %r15, %rcx
callq 0x2320
cmpq $0x1, %rax
jne 0x55eb
movq %r15, %rdi
callq 0x2100
leaq 0x2349(%rip), %rdi      # 0x7912
callq 0x20b0
xorl %edi, %edi
callq 0x2310
leaq 0x2304(%rip), %rdi      # 0x78e0
movl %r12d, %esi
xorl %eax, %eax
callq 0x2140
jmp 0x551c
leaq 0x2326(%rip), %rdi      # 0x7918
jmp 0x5517