// File: /workspace/github_cpp_makefile_star2_O2/sile[P]mphf/mkmphf.cc
// Function: main
int main(int argc, char** argv) {
  if(argc < 3) {
    std::cerr << "Usage: mkmphf [--mapping-loop-limit=<N>] [--hash-code-scale=<D>] [--hash-seed=<N>] <index> <keyset>" << std::endl;
    return 1;
  }

  int param_i=1;
  int loop_limit=32;
  int hash_seed=-1;
  double hash_code_scale=2.09;
  
  for(; param_i < argc; param_i++) {
    if(strncmp(argv[param_i], OPT1"=", strlen(OPT1"="))==0) {
      loop_limit = atoi(argv[param_i]+strlen(OPT1"="));
    } else if (strncmp(argv[param_i], OPT2"=", strlen(OPT2"="))==0) {
      hash_code_scale = atof(argv[param_i]+strlen(OPT2"="));
    } else if (strncmp(argv[param_i], OPT3"=", strlen(OPT3"="))==0) {
      hash_seed = atoi(argv[param_i]+strlen(OPT3"="));
    } else if (strncmp(argv[param_i], "--", 2) != 0) {
      break;
    } else {
      std::cerr << "Usage: mkmphf [--mapping-loop-limit=<N>] [--hash-code-scale=<D>] <index> <keyset>" << std::endl;
      return 1;      
    }
  }

  const char* keyset_filepath = argv[param_i+1];
  const char* index_filepath = argv[param_i];

  std::cerr << "= Initialize" << std::endl;
  MPHF::Generator gen(keyset_filepath, hash_code_scale, hash_seed);
  if(!gen) {
    std::cerr << "Can't open file(or file is empty): " << keyset_filepath << std::endl;
    return 1;    
  }

  std::cerr << "  == key count: " << gen.key_count << std::endl;
  std::cerr << "  == hash value limit: " << gen.hash_value_limit << std::endl;
  std::cerr << "DONE" << std::endl;
  
  std::cerr << "= Mapping Step: " << std::endl;
  int loop_count=0;
  for(; loop_count < loop_limit; loop_count++) {
    std::cerr << " == loop: " << loop_count+1 << '/' << loop_limit << std::endl;
    if(gen.mapping()==true)
      break;
  }
  if(loop_count==loop_limit) {
    std::cerr << "Failure: loop limit exceeded" << std::endl;
    return 1;
  }
  std::cerr << "Done" << std::endl;
    
  std::cerr << "= Assigning Step: " << std::endl;
  gen.assigning();
  std::cerr << "Done" << std::endl;

  std::cerr << "= Ranking Step: " << std::endl;
  gen.ranking();
  std::cerr << "Done" << std::endl;

  std::cerr << "= Save: " << index_filepath << std::endl;
  std::ofstream out(index_filepath);
  if(!out) {
    std::cerr << "Can't open file: " << index_filepath << std::endl;
    return 1;
  }
  gen.save(out);
  std::cerr << "Done" << std::endl;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
pushq $0x1
popq %rbx
cmpl $0x3, %edi
jge 0x23a5
movq 0x4c64(%rip), %rdi      # 0x6ff0
leaq 0x1c7d(%rip), %rsi      # 0x4010
callq 0x2150
movq %rax, %rdi
callq 0x20d0
jmp 0x2791
movq %rsi, %r14
movl %edi, %r15d
movsd 0x1c55(%rip), %xmm0     # 0x4008
movsd %xmm0, 0x10(%rsp)
pushq $-0x1
popq %rax
movl %eax, (%rsp)
pushq $0x20
popq %rax
movl %eax, 0x4(%rsp)
pushq $0x1
popq %rbp
leaq 0x1ca4(%rip), %r12      # 0x4074
pushq $0x15
popq %r13
cmpq %rbp, %r15
je 0x2460
movq (%r14,%rbp,8), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x20c0
testl %eax, %eax
je 0x2434
movq %rbx, %rdi
leaq 0x1c8d(%rip), %rsi      # 0x408a
pushq $0x12
popq %rdx
callq 0x20c0
testl %eax, %eax
je 0x2446
movq %rbx, %rdi
leaq 0x1c8a(%rip), %rsi      # 0x409d
pushq $0xc
popq %rdx
callq 0x20c0
testl %eax, %eax
jne 0x271e
addq $0xc, %rbx
movq %rbx, %rdi
callq 0x21f0
movl %eax, (%rsp)
jmp 0x2458
addq $0x15, %rbx
movq %rbx, %rdi
callq 0x21f0
movl %eax, 0x4(%rsp)
jmp 0x2458
addq $0x12, %rbx
movq %rbx, %rdi
callq 0x21a0
movsd %xmm0, 0x10(%rsp)
incq %rbp
jmp 0x23d4
movl %r15d, %ebp
movl %ebp, %eax
movq (%r14,%rax,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x8(%r14,%rax,8), %r14
movq 0x4b76(%rip), %rdi      # 0x6ff0
leaq 0x1c7b(%rip), %rsi      # 0x40fc
callq 0x2150
movq %rax, %rdi
callq 0x20d0
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movsd 0x10(%rsp), %xmm0
movl (%rsp), %edx
callq 0x27d0
cmpl $0x0, 0x28(%r12)
je 0x25b8
movq 0x4b36(%rip), %rdi      # 0x6ff0
leaq 0x1c6c(%rip), %rsi      # 0x412d
callq 0x2150
movl 0x40(%rsp), %esi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20d0
movq 0x4b0f(%rip), %rdi      # 0x6ff0
leaq 0x1c56(%rip), %rsi      # 0x413e
callq 0x2150
movl 0x44(%rsp), %esi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20d0
movq 0x4ae8(%rip), %rdi      # 0x6ff0
leaq 0x1c47(%rip), %rsi      # 0x4156
callq 0x2150
movq %rax, %rdi
callq 0x20d0
movq 0x4acd(%rip), %rdi      # 0x6ff0
leaq 0x1c31(%rip), %rsi      # 0x415b
callq 0x2150
movq %rax, %rdi
callq 0x20d0
xorl %ecx, %ecx
movl 0x4(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %ecx
movl %ecx, (%rsp)
movl %ecx, %ebx
negl %ebx
pushq $0x1
popq %r14
movq 0x4a9c(%rip), %r15      # 0x6ff0
leaq 0x1c11(%rip), %r12      # 0x416c
pushq $0x2f
popq %rbp
leaq 0x18(%rsp), %r13
leal (%rbx,%r14), %eax
cmpl $0x1, %eax
je 0x25e0
movq %r15, %rdi
movq %r12, %rsi
callq 0x2150
movq %rax, %rdi
movl %r14d, %esi
callq 0x2210
movq %rax, %rdi
movl %ebp, %esi
callq 0x2190
movq %rax, %rdi
movl 0x4(%rsp), %esi
callq 0x2210
movq %rax, %rdi
callq 0x20d0
movq %r13, %rdi
callq 0x283c
incl %r14d
testb %al, %al
je 0x2563
addl $-0x2, %r14d
movl %r14d, %eax
jmp 0x25e3
movq 0x4a31(%rip), %rdi      # 0x6ff0
leaq 0x1b43(%rip), %rsi      # 0x4109
callq 0x2150
movq %rax, %rdi
movq %r14, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20d0
jmp 0x2604
movl (%rsp), %eax
cmpl 0x4(%rsp), %eax
jne 0x260c
movq 0x4a00(%rip), %rdi      # 0x6ff0
leaq 0x1b80(%rip), %rsi      # 0x4177
callq 0x2150
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbx
jmp 0x2787
movq 0x49dd(%rip), %rdi      # 0x6ff0
leaq 0x1b7a(%rip), %rsi      # 0x4194
callq 0x2150
movq %rax, %rdi
callq 0x20d0
movq 0x49c2(%rip), %rdi      # 0x6ff0
leaq 0x1b64(%rip), %rsi      # 0x4199
callq 0x2150
movq %rax, %rdi
callq 0x20d0
leaq 0x18(%rsp), %rdi
callq 0x2856
movq 0x499d(%rip), %rdi      # 0x6ff0
leaq 0x1b3a(%rip), %rsi      # 0x4194
callq 0x2150
movq %rax, %rdi
callq 0x20d0
movq 0x4982(%rip), %rdi      # 0x6ff0
leaq 0x1b37(%rip), %rsi      # 0x41ac
callq 0x2150
movq %rax, %rdi
callq 0x20d0
movq 0x30(%rsp), %rdi
callq 0x368e
movq 0x495d(%rip), %rdi      # 0x6ff0
leaq 0x1afa(%rip), %rsi      # 0x4194
callq 0x2150
movq %rax, %rdi
callq 0x20d0
movq 0x4942(%rip), %rdi      # 0x6ff0
leaq 0x1b08(%rip), %rsi      # 0x41bd
callq 0x2150
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
pushq $0x10
popq %rdx
movq 0x8(%rsp), %rsi
callq 0x2180
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x68(%rsp,%rax)
je 0x2751
movq 0x48f8(%rip), %rdi      # 0x6ff0
leaq 0x1ac7(%rip), %rsi      # 0x41c6
callq 0x2150
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbx
jmp 0x277d
cmpb $0x2d, (%rbx)
jne 0x2463
cmpb $0x2d, 0x1(%rbx)
jne 0x2463
movq 0x48b8(%rip), %rdi      # 0x6ff0
leaq 0x196b(%rip), %rsi      # 0x40aa
callq 0x2150
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbx
jmp 0x2791
leaq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x2a14
movq 0x4889(%rip), %rdi      # 0x6ff0
leaq 0x1a26(%rip), %rsi      # 0x4194
callq 0x2150
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
callq 0x21d0
leaq 0x18(%rsp), %rdi
callq 0x2ae6
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27ba
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x21d0
jmp 0x27bd
jmp 0x27ba
jmp 0x27ba
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2ae6
movq %rbx, %rdi
callq 0x2220
nop