// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x6b87
leaq 0x1cf4(%rip), %rdi      # 0x886a
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x22f0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
callq 0x22c0
movl %eax, -0x34(%rbp)
movq -0x28(%rbp), %rdi
callq 0x22c0
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rsi
leaq 0x1ca2(%rip), %rdi      # 0x887c
movb $0x0, %al
callq 0x2120
movl -0x34(%rbp), %esi
movl -0x38(%rbp), %edx
leaq 0x1cab(%rip), %rdi      # 0x8899
movb $0x0, %al
callq 0x2120
movq -0x30(%rbp), %rsi
leaq 0x1cba(%rip), %rdi      # 0x88ba
movb $0x0, %al
callq 0x2120
movl -0x34(%rbp), %edi
movl -0x38(%rbp), %esi
callq 0x2620
movq %rax, -0x40(%rbp)
movl $0x4000, %edi           # imm = 0x4000
callq 0x2200
movq %rax, -0x48(%rbp)
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x2190
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x6c4e
leaq -0x150(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2090
movq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x4000, %edx           # imm = 0x4000
callq 0x2150
movq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x2150
movq $0x0, -0x158(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1c43(%rip), %rsi      # 0x88c7
callq 0x2280
movq %rax, -0x158(%rbp)
cmpq $0x0, %rax
jne 0x6cae
leaq 0x1c2d(%rip), %rdi      # 0x88ca
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x22f0
movq -0x40(%rbp), %rdi
movq -0x158(%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
callq 0x2300
cmpq $0x1, %rax
je 0x6ce6
leaq 0x1c10(%rip), %rdi      # 0x88e5
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x22f0
movl $0x0, -0x168(%rbp)
movl -0x168(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x6dad
leaq -0x162(%rbp), %rdi
movl -0x168(%rbp), %edx
leaq 0x1bef(%rip), %rsi      # 0x8902
movb $0x0, %al
callq 0x22e0
movq -0x48(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x162(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq -0x162(%rbp), %rdi
callq 0x20f0
movq -0x178(%rbp), %rdi
movq -0x170(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x21d0
movq -0x48(%rbp), %rdi
movq -0x158(%rbp), %rcx
movl $0x4000, %esi           # imm = 0x4000
movl $0x1, %edx
callq 0x2300
cmpq $0x1, %rax
je 0x6d97
movl -0x168(%rbp), %esi
leaq 0x1b85(%rip), %rdi      # 0x890b
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x22f0
jmp 0x6d99
movl -0x168(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x168(%rbp)
jmp 0x6cf0
movl $0x0, -0x168(%rbp)
movl -0x168(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x6e74
leaq -0x162(%rbp), %rdi
movl -0x168(%rbp), %edx
leaq 0x1b49(%rip), %rsi      # 0x8923
movb $0x0, %al
callq 0x22e0
movq -0x48(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x162(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x162(%rbp), %rdi
callq 0x20f0
movq -0x188(%rbp), %rdi
movq -0x180(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x21d0
movq -0x48(%rbp), %rdi
movq -0x158(%rbp), %rcx
movl $0x2000, %esi           # imm = 0x2000
movl $0x1, %edx
callq 0x2300
cmpq $0x1, %rax
je 0x6e5e
movl -0x168(%rbp), %esi
leaq 0x1adf(%rip), %rdi      # 0x892c
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x22f0
jmp 0x6e60
movl -0x168(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x168(%rbp)
jmp 0x6db7
cmpq $0x0, -0x30(%rbp)
je 0x6eb8
leaq -0x150(%rbp), %rdi
movq -0x158(%rbp), %rcx
movl $0xff, %esi
movl $0x1, %edx
callq 0x2300
cmpq $0x1, %rax
je 0x6eb6
leaq 0x1a9f(%rip), %rdi      # 0x8944
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x22f0
jmp 0x6eb8
movq -0x158(%rbp), %rdi
callq 0x20e0
leaq 0x1a91(%rip), %rdi      # 0x895c
movb $0x0, %al
callq 0x2120
xorl %edi, %edi
callq 0x22f0