// File: /workspace/github_cpp_makefile_star2_O2/maplechori[P]stata-reader-writer/main.cpp
// Function: main
int main(int argc, char **argv)
{
  namespace po = boost::program_options;
  Context *ctx = NULL;

  try
  {
    po::options_description desc("General options");
    desc.add_options()
    ("help", "produce help message")
    ("stata-file", po::value< string >(), "stata input file")
    ;

    po::options_description db_export("Database options");
    db_export.add_options()
    ("db-type,d", po::value< string >(), "select database type [sqlite]")
    ("connection-string,c", po::value< string >(), "connection string" )
    ;

    po::positional_options_description p;
    p.add("stata-file", -1);

    po::positional_options_description db;
    db.add("db-type", 1).add("connection-string", 2);
    
    po::options_description all("Allowed options");
    all.add(desc).add(db_export);
    
    po::variables_map args;
    
    po::store (po::command_line_parser(argc, argv).options(all).positional(p).positional(db)
    .style (po::command_line_style::default_style | po::command_line_style::allow_long_disguise)
    .run (), args);

    po::notify(args);

    if (args.count("help") || !args.count("stata-file"))
    {
      cout << "Usage: " << PROGRAM_NAME << " [options] <stata file>\n";
      cout << all << endl;
      return 0;
    }

    ifstream stfs(args["stata-file"].as< string >().c_str(), ios::in | ios::binary);
    std::string stata((std::istreambuf_iterator<char>(stfs)), std::istreambuf_iterator<char>());
    ctx = new Context((char *)stata.c_str(), stata.length());

    if (ctx != NULL) {
      ctx->advance();
    }

    
 
    if (args.count("db-type"))
    {
      DatabaseTypes db_type = SQLITE;

      if (!args.count("connection-string"))
      {
        cout << "Connection string required" << endl;
        return 0;     
      }
     
      if (args["db-type"].as< string >() == "sqlite")
      {
        db_type = SQLITE;
      }
      else
      {
        cout << "Database type not supported" << endl;
        return 0;
      }
    
      cout << "Exporting to " << args["connection-string"].as< string >().c_str() << endl;
      ctx->exportToDB( db_type, (char *)args["connection-string"].as< string >().c_str()); 
      return 1;
       
        
      
    }
    else
    {
        // Show the metadata of the file and exit 
         cout << "Information on Stata file: " << args["stata-file"].as< string >() << endl;
         cout << ctx->hdr.showHeader() << endl;
         return 1;
    }

  }
  catch(std::exception& e) {
      cout << e.what() << "\n";
  }
    

  
  

  //  ctx->exportToDB((char *)"export_context.db");
  

  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rsi, %r14
movl %edi, %r15d
leaq 0x76e7(%rip), %rsi      # 0xf004
leaq -0x390(%rbp), %rdi
leaq -0x188(%rbp), %rdx
callq 0x8190
movq 0x10669(%rip), %rax     # 0x17fa0
movl (%rax), %r12d
movl %r12d, %r13d
shrl %r13d
leaq -0x510(%rbp), %rdi
leaq -0x390(%rbp), %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x7460
leaq -0x390(%rbp), %rdi
callq 0x7150
leaq -0x510(%rbp), %rdi
callq 0x7390
leaq -0x390(%rbp), %rdi
movq %rax, (%rdi)
leaq 0x7692(%rip), %rsi      # 0xf014
leaq 0x7690(%rip), %rdx      # 0xf019
callq 0x75e0
movq %rax, %rbx
callq 0x81cb
leaq 0x7691(%rip), %rsi      # 0xf02e
leaq 0x7695(%rip), %rcx      # 0xf039
movq %rbx, %rdi
movq %rax, %rdx
callq 0x7620
leaq 0x7694(%rip), %rsi      # 0xf04a
leaq -0x390(%rbp), %rdi
leaq -0x188(%rbp), %rdx
callq 0x8190
leaq -0x490(%rbp), %rdi
leaq -0x390(%rbp), %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x7460
leaq -0x390(%rbp), %rdi
callq 0x7150
leaq -0x490(%rbp), %rdi
callq 0x7390
movq %rax, -0x390(%rbp)
callq 0x81cb
leaq 0x764e(%rip), %rsi      # 0xf05b
leaq 0x7651(%rip), %rcx      # 0xf065
leaq -0x390(%rbp), %rdi
movq %rax, %rdx
callq 0x7620
movq %rax, %rbx
callq 0x81cb
leaq 0x7651(%rip), %rsi      # 0xf083
leaq 0x765e(%rip), %rcx      # 0xf097
movq %rbx, %rdi
movq %rax, %rdx
callq 0x7620
leaq -0xe8(%rbp), %rdi
callq 0x7400
leaq 0x75d7(%rip), %rsi      # 0xf02e
leaq -0xe8(%rbp), %rdi
pushq $-0x1
popq %rdx
callq 0x7350
leaq -0xb0(%rbp), %rdi
callq 0x7400
leaq 0x7630(%rip), %rsi      # 0xf0a9
leaq -0xb0(%rbp), %rdi
pushq $0x1
popq %rbx
movl %ebx, %edx
callq 0x7350
leaq 0x7620(%rip), %rsi      # 0xf0b1
pushq $0x2
popq %rdx
movq %rax, %rdi
callq 0x7350
leaq 0x7620(%rip), %rsi      # 0xf0c3
leaq -0x390(%rbp), %rdi
leaq -0x188(%rbp), %rdx
callq 0x8190
leaq -0x410(%rbp), %rdi
leaq -0x390(%rbp), %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x7460
leaq -0x390(%rbp), %rdi
callq 0x7150
leaq -0x410(%rbp), %rdi
leaq -0x510(%rbp), %rsi
callq 0x7330
leaq -0x490(%rbp), %rsi
movq %rax, %rdi
callq 0x7330
leaq -0x188(%rbp), %rdi
callq 0x7130
leaq -0x390(%rbp), %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x81d8
leaq -0x390(%rbp), %rdi
leaq -0x410(%rbp), %rsi
callq 0x8266
movq %rax, %r14
leaq -0xe8(%rbp), %rsi
movq %rax, %rdi
callq 0x7560
leaq -0xb0(%rbp), %rsi
movq %r14, %rdi
callq 0x7560
movq %r14, %rdi
movl $0x13f7, %esi           # imm = 0x13F7
callq 0x7640
leaq -0x78(%rbp), %rdi
movq %r14, %rsi
callq 0x8284
leaq -0x78(%rbp), %rdi
leaq -0x188(%rbp), %rsi
xorl %edx, %edx
callq 0x75a0
leaq -0x78(%rbp), %rdi
callq 0x8402
leaq -0x390(%rbp), %rdi
callq 0x8316
leaq -0x188(%rbp), %rdi
callq 0x71e0
leaq 0x7472(%rip), %rsi      # 0xf014
leaq -0x390(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x8190
leaq -0x178(%rbp), %r14
leaq -0x390(%rbp), %rsi
movq %r14, %rdi
callq 0x8340
testq %rax, %rax
je 0x7c2c
leaq -0x390(%rbp), %rdi
callq 0x7150
movq 0x103e0(%rip), %rdi     # 0x17fc0
leaq 0x74ec(%rip), %rsi      # 0xf0d3
callq 0x7300
leaq 0x74e8(%rip), %rsi      # 0xf0db
movq %rax, %rdi
callq 0x7300
leaq 0x74e9(%rip), %rsi      # 0xf0eb
movq %rax, %rdi
callq 0x7300
movq 0x103af(%rip), %rdi     # 0x17fc0
leaq -0x410(%rbp), %rsi
callq 0x7080
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x7160
jmp 0x7f26
leaq 0x73fb(%rip), %rsi      # 0xf02e
leaq -0x78(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x8190
leaq -0x78(%rbp), %rsi
movq %r14, %rdi
callq 0x8340
movq %rax, %r15
leaq -0x78(%rbp), %rdi
callq 0x7150
leaq -0x390(%rbp), %rdi
callq 0x7150
testq %r15, %r15
je 0x7bd9
leaq 0x73ba(%rip), %rsi      # 0xf02e
leaq -0x78(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x8190
leaq -0x188(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x7370
movq %rax, %rdi
callq 0x9c86
movq (%rax), %rsi
leaq -0x390(%rbp), %rdi
pushq $0xc
popq %rdx
callq 0x75d0
leaq -0x78(%rbp), %rdi
callq 0x7150
movq -0x390(%rbp), %rax
movq -0x18(%rax), %rax
movq -0x2a8(%rbp,%rax), %rsi
leaq -0x68(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x78(%rbp), %rdi
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
movl %edx, %r8d
callq 0x9f26
movl $0x118, %edi            # imm = 0x118
callq 0x7310
movq %rax, %r15
movq -0x78(%rbp), %rsi
movl -0x70(%rbp), %edx
movq %rax, %rdi
callq 0xa508
movq %r15, %rdi
callq 0xa876
leaq 0x739e(%rip), %rsi      # 0xf0a9
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x8190
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x8340
movq %rax, %r12
leaq -0x50(%rbp), %rdi
callq 0x7150
testq %r12, %r12
je 0x7e5a
leaq 0x7371(%rip), %rsi      # 0xf0b1
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x8190
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x8340
movq %rax, %r14
leaq -0x50(%rbp), %rdi
callq 0x7150
testq %r14, %r14
je 0x7eeb
leaq 0x7334(%rip), %rsi      # 0xf0a9
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x8190
leaq -0x188(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x7370
movq %rax, %rdi
callq 0x9c86
leaq 0x737e(%rip), %rsi      # 0xf11f
movq %rax, %rdi
callq 0x8364
movl %eax, %r14d
leaq -0x50(%rbp), %rdi
callq 0x7150
testb %r14b, %r14b
je 0x7ef4
movq 0x101fb(%rip), %rdi     # 0x17fc0
leaq 0x7376(%rip), %rsi      # 0xf142
callq 0x7300
movq %rax, %r14
leaq 0x72d6(%rip), %rsi      # 0xf0b1
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x8190
leaq -0x188(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x7370
movq %rax, %rdi
callq 0x9c86
movq (%rax), %rsi
movq %r14, %rdi
callq 0x7300
movq %rax, %rdi
callq 0x7160
leaq -0x50(%rbp), %rdi
callq 0x7150
leaq 0x728e(%rip), %rsi      # 0xf0b1
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x8190
leaq -0x188(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x7370
movq %rax, %rdi
callq 0x9c86
movq (%rax), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0xa69e
jmp 0x7ee0
movq 0x1015f(%rip), %rdi     # 0x17fc0
leaq 0x72e8(%rip), %rsi      # 0xf150
callq 0x7300
movq %rax, %r14
leaq 0x71b7(%rip), %rsi      # 0xf02e
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x8190
leaq -0x188(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x7370
movq %rax, %rdi
callq 0x9c86
movq %r14, %rdi
movq %rax, %rsi
callq 0x72c0
movq %rax, %rdi
callq 0x7160
leaq -0x50(%rbp), %rdi
callq 0x7150
addq $0x8, %r15
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0xa1a4
movq 0x100f1(%rip), %rdi     # 0x17fc0
leaq -0x50(%rbp), %rsi
callq 0x72c0
movq %rax, %rdi
callq 0x7160
leaq -0x50(%rbp), %rdi
callq 0x7150
jmp 0x7f11
leaq 0x7212(%rip), %rsi      # 0xf104
jmp 0x7efb
leaq 0x722b(%rip), %rsi      # 0xf126
movq 0x100be(%rip), %rdi     # 0x17fc0
callq 0x7300
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x7160
leaq -0x78(%rbp), %rdi
callq 0x7150
leaq -0x390(%rbp), %rdi
callq 0x70b0
leaq -0x188(%rbp), %rdi
callq 0x8374
leaq -0x410(%rbp), %rdi
callq 0x83b0
leaq -0xb0(%rbp), %rdi
callq 0x83e2
leaq -0xe8(%rbp), %rdi
callq 0x83e2
leaq -0x490(%rbp), %rdi
callq 0x83b0
leaq -0x510(%rbp), %rdi
callq 0x83b0
movl %ebx, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7fe4
jmp 0x7fe4
jmp 0x7fe4
jmp 0x7fe4
jmp 0x7fe4
jmp 0x7fc0
jmp 0x7fc0
jmp 0x7fe4
jmp 0x7fc0
jmp 0x7fc0
jmp 0x7fc0
jmp 0x7fc0
jmp 0x7fe4
movq %rdx, %rbx
movq %rax, %r14
movl $0x118, %esi            # imm = 0x118
movq %r15, %rdi
callq 0x7320
jmp 0x7fea
movq %rdx, %rbx
movq %rax, %r14
jmp 0x7ff3
jmp 0x80bf
jmp 0x7fe4
movq %rdx, %rbx
movq %rax, %r14
leaq -0x50(%rbp), %rdi
callq 0x7150
jmp 0x7fea
movq %rdx, %rbx
movq %rax, %r14
leaq -0x78(%rbp), %rdi
callq 0x7150
jmp 0x8016
jmp 0x8010
movq %rdx, %rbx
movq %rax, %r14
leaq -0x78(%rbp), %rdi
callq 0x7150
leaq -0x390(%rbp), %rdi
callq 0x70b0
jmp 0x80d9
movq %rdx, %rbx
movq %rax, %r14
leaq -0x78(%rbp), %rdi
jmp 0x801d
movq %rdx, %rbx
movq %rax, %r14
leaq -0x390(%rbp), %rdi
callq 0x7150
jmp 0x80d9
jmp 0x80bf
movq %rdx, %rbx
movq %rax, %r14
leaq -0x78(%rbp), %rdi
callq 0x8402
jmp 0x80cd
jmp 0x80bf
jmp 0x80a7
movq %rdx, %rbx
movq %rax, %r14
leaq -0x390(%rbp), %rdi
callq 0x7150
jmp 0x80f1
jmp 0x80af
jmp 0x805f
movq %rdx, %rbx
movq %rax, %r14
jmp 0x80fd
jmp 0x8179
movq %rdx, %rbx
movq %rax, %r14
leaq -0x390(%rbp), %rdi
callq 0x7150
jmp 0x8115
jmp 0x80b7
movq %rdx, %rbx
movq %rax, %r14
leaq -0x390(%rbp), %rdi
callq 0x7150
jmp 0x8121
movq %rdx, %rbx
movq %rax, %r14
jmp 0x8121
movq %rdx, %rbx
movq %rax, %r14
jmp 0x80e5
movq %rdx, %rbx
movq %rax, %r14
jmp 0x80f1
movq %rdx, %rbx
movq %rax, %r14
jmp 0x8115
movq %rdx, %rbx
movq %rax, %r14
jmp 0x80d9
movq %rdx, %rbx
movq %rax, %r14
leaq -0x390(%rbp), %rdi
callq 0x8316
leaq -0x188(%rbp), %rdi
callq 0x8374
leaq -0x410(%rbp), %rdi
callq 0x83b0
leaq -0xb0(%rbp), %rdi
callq 0x83e2
leaq -0xe8(%rbp), %rdi
callq 0x83e2
leaq -0x490(%rbp), %rdi
callq 0x83b0
leaq -0x510(%rbp), %rdi
callq 0x83b0
cmpl $0x1, %ebx
jne 0x8169
movq %r14, %rdi
callq 0x7120
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0xfe82(%rip), %rdi      # 0x17fc0
movq %rax, %rsi
callq 0x7300
leaq 0x6fb5(%rip), %rsi      # 0xf102
movq %rax, %rdi
callq 0x7300
callq 0x7540
xorl %ebx, %ebx
jmp 0x7f6e
movq %rax, %r14
callq 0x7540
movq %r14, %rdi
callq 0x75b0
movq %rax, %rdi
callq 0x8181
movq %rdx, %rbx
movq %rax, %r14
jmp 0x8109