// File: /workspace/github_cpp_makefile_star2_O2/bigtit[P]ps3patcher/patcher.cc
// Function: patcher::do_patch(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
void patcher::do_patch(string name, bool swap){
  string fileout = get_dest_name(name);
  std::ifstream freader(name.c_str(), std::ios::binary);
  freader >> std::noskipws;
  std::ofstream fwriter(fileout.c_str(), std::ios::binary);
  if(swap) status("Go swap");
  status("Creating output file...");
  fwriter << freader.rdbuf(); // copy
  freader.seekg(0, std::ios::end);
  auto pos = freader.tellg();
  freader.close();
  get_ros_pdata("patch.bin", swap);
  status("input size: "+std::to_string(pos)+" bytes");
  if(pos!=0x1000000L && pos!=0x10000000L){
    remove(fileout.c_str());
    error("size error, exits");
    return;
  }
  else if(0x1000000L == pos){ // nor
    status("Go NOR");
    write_patch(0xc0010, fwriter);
    write_patch(0x7c0010, fwriter);
    if(chk_flag(0x1)){
      status("Go trvkpatches");
      get_pdata("nor_rvk.bin", swap);
      write_patch(0x40000, fwriter);
    }
    if(chk_flag(0x2)){
      status("Go forcepatch && rosheader");
      get_pdata("ros_head.bin", swap);
      write_patch(0xc0000, fwriter);
      write_patch(0x7c0000, fwriter);
    }
    status("Done NOR");
  }else{ // nand
    status("Go NAND");
    write_patch(0xc0030, fwriter);
    write_patch(0x7c0020, fwriter);
    if(chk_flag(0x1)){
      status("Go trvkpatches");
      get_pdata("nand_rvk.bin", swap);
      write_patch(0x40000, fwriter);
    }
    if(chk_flag(0x2)){
      status("Go forcepatch && rosheader");
      get_pdata("ros_head.bin", swap);
      write_patch(0xc0020, fwriter);
      write_patch(0x7c0010, fwriter);
    }
    status("Done NAND");
  }

  if(chk_flag(0x4)); // autoexit, reserved
  cout << "Done\n";
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x688, %rsp            # imm = 0x688
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r14
leaq -0x2a8(%rbp), %r13
movq %r13, %rdi
callq 0x21a0
leaq 0x16d5(%rip), %rdx      # 0x410a
leaq -0x288(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x31b9
leaq -0x2a8(%rbp), %rdi
callq 0x2090
movq (%r12), %rsi
leaq -0x6b0(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2220
movq -0x6b0(%rbp), %rax
movq -0x18(%rax), %rax
andl $0xffffefff, -0x698(%rbp,%rax) # imm = 0xFFFFEFFF
movq -0x288(%rbp), %rsi
leaq -0x4a8(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2140
testb %r15b, %r15b
je 0x2ac9
leaq 0x1689(%rip), %rsi      # 0x4127
leaq -0x268(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x268(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x268(%rbp), %rdi
callq 0x2090
leaq 0x165f(%rip), %rsi      # 0x412f
leaq -0x248(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x248(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x248(%rbp), %rdi
callq 0x2090
leaq -0x6a0(%rbp), %rsi
leaq -0x4a8(%rbp), %rdi
callq 0x2040
leaq -0x6b0(%rbp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x2190
leaq -0x6b0(%rbp), %rdi
callq 0x21f0
movq %rax, %r12
leaq -0x6b0(%rbp), %rdi
callq 0x2030
leaq 0x1606(%rip), %rsi      # 0x4147
leaq -0x228(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
movzbl %r15b, %r15d
leaq -0x228(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x28be
leaq -0x228(%rbp), %rdi
callq 0x2090
leaq -0x1e8(%rbp), %rdi
movq %r12, %rsi
callq 0x328a
leaq 0x15c8(%rip), %rsi      # 0x4151
leaq -0x48(%rbp), %rdi
leaq -0x1e8(%rbp), %rdx
callq 0x321a
leaq 0x1580(%rip), %rdx      # 0x4120
leaq -0x208(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x31f1
leaq -0x208(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x208(%rbp), %rdi
callq 0x2090
leaq -0x48(%rbp), %rdi
callq 0x2090
leaq -0x1e8(%rbp), %rdi
callq 0x2090
cmpq $0x1000000, %r12        # imm = 0x1000000
je 0x2d9b
cmpq $0x10000000, %r12       # imm = 0x10000000
jne 0x2f5a
leaq 0x15d0(%rip), %rsi      # 0x41d1
leaq -0x108(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x108(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x108(%rbp), %rdi
callq 0x2090
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0xc0030, %esi          # imm = 0xC0030
callq 0x2938
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0x7c0020, %esi         # imm = 0x7C0020
callq 0x2938
movb 0x18(%r14), %al
testb $0x1, %al
je 0x2cdb
leaq 0x1522(%rip), %rsi      # 0x4185
leaq -0xe8(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0xe8(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0xe8(%rbp), %rdi
callq 0x2090
leaq 0x1544(%rip), %rsi      # 0x41d9
leaq -0xc8(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0xc8(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x27dc
leaq -0xc8(%rbp), %rdi
callq 0x2090
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0x40000, %esi          # imm = 0x40000
callq 0x2938
movb 0x18(%r14), %al
testb $0x2, %al
je 0x2d72
leaq 0x14b6(%rip), %rsi      # 0x41a0
leaq -0xa8(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0xa8(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0xa8(%rbp), %rdi
callq 0x2090
leaq 0x149f(%rip), %rsi      # 0x41bb
leaq -0x88(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x88(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x27dc
leaq -0x88(%rbp), %rdi
callq 0x2090
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0xc0020, %esi          # imm = 0xC0020
callq 0x2938
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0x7c0010, %esi         # imm = 0x7C0010
callq 0x2938
leaq 0x146d(%rip), %rsi      # 0x41e6
leaq -0x68(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x68(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x68(%rbp), %rdi
jmp 0x2f40
leaq 0x13dc(%rip), %rsi      # 0x417e
leaq -0x1c8(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x1c8(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x1c8(%rbp), %rdi
callq 0x2090
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0xc0010, %esi          # imm = 0xC0010
callq 0x2938
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0x7c0010, %esi         # imm = 0x7C0010
callq 0x2938
movb 0x18(%r14), %al
testb $0x1, %al
je 0x2e7c
leaq 0x1381(%rip), %rsi      # 0x4185
leaq -0x1a8(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x1a8(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x1a8(%rbp), %rdi
callq 0x2090
leaq 0x135e(%rip), %rsi      # 0x4194
leaq -0x188(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x188(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x27dc
leaq -0x188(%rbp), %rdi
callq 0x2090
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0x40000, %esi          # imm = 0x40000
callq 0x2938
movb 0x18(%r14), %al
testb $0x2, %al
je 0x2f13
leaq 0x1315(%rip), %rsi      # 0x41a0
leaq -0x168(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x168(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x168(%rbp), %rdi
callq 0x2090
leaq 0x12fe(%rip), %rsi      # 0x41bb
leaq -0x148(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x148(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x27dc
leaq -0x148(%rbp), %rdi
callq 0x2090
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0xc0000, %esi          # imm = 0xC0000
callq 0x2938
leaq -0x4a8(%rbp), %rdx
movq %r14, %rdi
movl $0x7c0000, %esi         # imm = 0x7C0000
callq 0x2938
leaq 0x12ae(%rip), %rsi      # 0x41c8
leaq -0x128(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2080
leaq -0x128(%rbp), %rsi
movq %r14, %rdi
callq 0x27bc
leaq -0x128(%rbp), %rdi
callq 0x2090
movq 0x3084(%rip), %rdi      # 0x5fd0
leaq 0x129d(%rip), %rsi      # 0x41f0
callq 0x2110
jmp 0x2fb9
movq -0x288(%rbp), %rdi
callq 0x2210
movq 0x3063(%rip), %rdi      # 0x5fd0
leaq 0x11ea(%rip), %rsi      # 0x415e
callq 0x2110
leaq 0x11e9(%rip), %rsi      # 0x4169
movq %rax, %rdi
callq 0x2110
pushq $0x44
popq %rsi
movq %rax, %rdi
callq 0x21e0
leaq 0x106f(%rip), %rsi      # 0x4009
movq %rax, %rdi
callq 0x2110
leaq 0x11c3(%rip), %rsi      # 0x416c
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x20a0
leaq -0x4a8(%rbp), %rdi
callq 0x21b0
leaq -0x6b0(%rbp), %rdi
callq 0x2060
leaq -0x288(%rbp), %rdi
callq 0x2090
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq -0x148(%rbp), %rdi
jmp 0x3120
movq %rax, %r14
leaq -0x88(%rbp), %rdi
jmp 0x3120
jmp 0x313d
jmp 0x313d
movq %rax, %r14
leaq -0x168(%rbp), %rdi
jmp 0x3120
movq %rax, %r14
leaq -0xa8(%rbp), %rdi
jmp 0x3120
movq %rax, %r14
leaq -0x188(%rbp), %rdi
jmp 0x3120
movq %rax, %r14
leaq -0xc8(%rbp), %rdi
jmp 0x3120
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
movq %rax, %r14
leaq -0x1a8(%rbp), %rdi
jmp 0x3120
movq %rax, %r14
leaq -0xe8(%rbp), %rdi
jmp 0x3120
jmp 0x313d
jmp 0x313d
movq %rax, %r14
leaq -0x128(%rbp), %rdi
jmp 0x3120
movq %rax, %r14
leaq -0x68(%rbp), %rdi
jmp 0x3120
jmp 0x313d
jmp 0x313d
movq %rax, %r14
leaq -0x1c8(%rbp), %rdi
jmp 0x3120
movq %rax, %r14
leaq -0x108(%rbp), %rdi
jmp 0x3120
jmp 0x313d
jmp 0x313d
movq %rax, %r14
leaq -0x268(%rbp), %rdi
jmp 0x3120
jmp 0x313d
movq %rax, %r14
leaq -0x208(%rbp), %rdi
callq 0x2090
jmp 0x30ef
movq %rax, %r14
leaq -0x48(%rbp), %rdi
callq 0x2090
jmp 0x30fd
movq %rax, %r14
leaq -0x1e8(%rbp), %rdi
jmp 0x3120
jmp 0x313d
movq %rax, %r14
leaq -0x228(%rbp), %rdi
jmp 0x3120
jmp 0x313d
movq %rax, %r14
leaq -0x248(%rbp), %rdi
callq 0x2090
jmp 0x3140
jmp 0x313d
movq %rax, %r14
jmp 0x314c
movq %rax, %r14
jmp 0x3158
movq %rax, %r14
movq %r13, %rbx
jmp 0x3158
jmp 0x313d
movq %rax, %r14
leaq -0x4a8(%rbp), %rdi
callq 0x21b0
leaq -0x6b0(%rbp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2090
movq %r14, %rdi
callq 0x2200