// File: /workspace/github_c_makefile_star2_O3/sonicse[P]dbf2mysql/dbf2mysql.c
// Function: main
int main(int argc, char **argv) {
    int i;
    MYSQL *SQLsock, mysql;
    extern int optind;
    extern char *optarg;
    char *query;
    dbhead *dbh;
    char *charset;

    primary[0] = '\0';

    while ((i = getopt(argc, argv, "xqfFrne:lucvi:h:p:d:t:s:o:U:P:C:")) != EOF) {
        switch (i) {
            case 'P':
                pass = (char *) strdup(optarg);
                break;
            case 'U':
                user = (char *) strdup(optarg);
                break;
            case 'x':
                express = 1;
                break;
            case 'f':
                fieldlow = 1;
                break;
            case 'F':
                var_chars = 0;
                break;
            case 'r':
                trim = 1;
                break;
            case 'n':
                null_fields = 1;
                break;
            case 'v':
                verbose++;
                break;
            case 'c':
                create++;
                break;
            case 'l':
                lower = 1;
                break;
            case 'u':
                if (lower) {
                    usage();
                    printf("Can't use -u and -l at the same time!\n");
                    exit(1);
                }
                upper = 1;
                break;
            case 'e':
                convert = (char *) strdup(optarg);
                break;
            case 'h':
                host = (char *) strdup(optarg);
                break;
            case 'q':
                quick = 1;
                break;
            case 'p':
                strncpy(primary, optarg, 11);
                break;
            case 'd':
                dbase = (char *) strdup(optarg);
                break;
            case 't':
                table = (char *) strdup(optarg);
                break;
            case 'i':
                indexes = (char *) strdup(optarg);
                break;
            case 's':
                subarg = (char *) strdup(optarg);
                break;
            case 'o':
                flist = (char *) strdup(optarg);
                break;
            case 'C':
                charset = (char *) strdup(optarg);
                break;
            case ':':
                usage();
                printf("missing argument!\n");
                exit(1);
            case '?':
                usage();
                printf("unknown argument: %s\n", argv[0]);
                exit(1);
            default:
                break;
        }
    }

    argc -= optind;
    argv = &argv[optind];

    if (argc != 1) {
        usage();
        exit(1);
    }

    if (verbose > 2) {
        printf("Opening dbf-file %s\n", argv[0]);
    }

    if ((dbh = dbf_open(argv[0], O_RDONLY)) == (dbhead *) - 1) {
        fprintf(stderr, "Couldn't open xbase-file %s\n", argv[0]);
        exit(1);
    }

    if (verbose) {
        printf("dbf-file: %s - %s, MySQL-dbase: %s, MySQL-table: %s\n",
                argv[0],
                dbh->db_description,
                dbase,
                table);
        printf("Number of records: %ld\n", dbh->db_records);
    }
    if (verbose > 1) {
        printf("Name\t\t Length\tDisplay\t Type\n");
        printf("-------------------------------------\n");
        for (i = 0; i < dbh->db_nfields; i++) {
            printf("%-12s\t%7d\t%5d\t%3c\n",
                    dbh->db_fields[i].db_name,
                    dbh->db_fields[i].db_flen,
                    dbh->db_fields[i].db_dec,
                    dbh->db_fields[i].db_type);
        }
    }

    if (verbose > 2) {
        printf("Making connection to MySQL-server\n");
    }

    // Init mysql
    if (!(mysql_init(&mysql))) {
        fprintf(stderr, "Can't initialize. Insufficient memory.");
        dbf_close(&dbh);
        exit(1);
    }

    if (!(SQLsock = mysql_real_connect(&mysql, host, user, pass, NULL, 0, NULL, 0))) {
        fprintf(stderr, "Couldn't get a connection with the ");
        fprintf(stderr, "designated host!\n");
        fprintf(stderr, "Detailed report: %s\n", mysql_error(&mysql));
        dbf_close(&dbh);
        exit(1);
    }

    if (verbose > 2) {
        printf("Selecting database '%s'\n", dbase);
    }

    if ((mysql_select_db(SQLsock, dbase)) == -1) {
        fprintf(stderr, "Couldn't select database %s.\n", dbase);
        fprintf(stderr, "Detailed report: %s\n", mysql_error(SQLsock));
        mysql_close(SQLsock);
        dbf_close(&dbh);
        exit(1);
    }

    if (charset && !mysql_set_character_set(&mysql, charset)) {
        printf("New client character set: %s\n",
                mysql_character_set_name(&mysql));
    }

    /* Substitute field names */
    do_onlyfields(flist, dbh);
    do_substitute(subarg, dbh);

    if (!create) {
        if (!check_table(SQLsock, table)) {
            printf("Table does not exist!\n");
            exit(1);
        }
    } else {
        if (verbose > 2) {
            printf("Dropping original table (if one exists)\n");
        }

        if (!(query = (char *) malloc(12 + strlen(table)))) {
            printf("Memory-allocation error in main (drop)!\n");
            mysql_close(SQLsock);
            dbf_close(&dbh);
            exit(1);
        }

        sprintf(query, "DROP TABLE `%s`", table);
        mysql_query(SQLsock, query);
        free(query);

        /* Build a CREATE-clause
         */
        do_create(SQLsock, table, dbh, charset);
    }

    /* Build an INSERT-clause
     */
    if (create < 2)
        do_inserts(SQLsock, table, dbh);

    if (verbose > 2) {
        printf("Closing up....\n");
    }

    mysql_close(SQLsock);
    dbf_close(&dbh);
    exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
movq %rsi, %r14
movl %edi, %ebp
movb $0x0, 0x59cf(%rip)      # 0x9270
leaq 0x2d36(%rip), %r15      # 0x65de
movq 0x5719(%rip), %r13      # 0x8fc8
leaq 0x27ae(%rip), %r12      # 0x6064
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2090
cmpl $0x45, %eax
jle 0x38eb
leal -0x50(%rax), %ecx
cmpl $0x28, %ecx
ja 0x3907
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5957(%rip)      # 0x9240
jmp 0x38b6
cmpl $0x42, %eax
jle 0x3a63
cmpl $0x43, %eax
jne 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, %rbx
jmp 0x38b6
cmpl $0x46, %eax
jne 0x38b6
movl $0x0, 0x58da(%rip)      # 0x91f0
jmp 0x38b6
cmpl $0x0, 0x58f9(%rip)      # 0x9218
jne 0x3b07
movl $0x1, 0x58e5(%rip)      # 0x9214
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x590f(%rip)      # 0x9250
jmp 0x38b6
movq (%r13), %rsi
movl $0xb, %edx
leaq 0x591a(%rip), %rdi      # 0x9270
callq 0x2110
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x58f8(%rip)      # 0x9268
jmp 0x38b6
movl $0x1, 0x58b1(%rip)      # 0x9230
jmp 0x38b6
movl $0x1, 0x589a(%rip)      # 0x9228
jmp 0x38b6
incl 0x5883(%rip)            # 0x921c
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5852(%rip)      # 0x9200
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5835(%rip)      # 0x91f8
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5888(%rip)      # 0x9260
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x585b(%rip)      # 0x9248
jmp 0x38b6
incl 0x5818(%rip)            # 0x9210
jmp 0x38b6
movl $0x1, 0x5819(%rip)      # 0x9220
jmp 0x38b6
movl $0x1, 0x5816(%rip)      # 0x922c
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x580d(%rip)      # 0x9238
jmp 0x38b6
movl $0x1, 0x57ea(%rip)      # 0x9224
jmp 0x38b6
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5809(%rip)      # 0x9258
jmp 0x38b6
movl $0x1, 0x57ba(%rip)      # 0x9218
jmp 0x38b6
cmpl $-0x1, %eax
je 0x3a95
cmpl $0x3a, %eax
je 0x3af9
cmpl $0x3f, %eax
jne 0x38b6
callq 0x286e
movq (%r14), %rsi
leaq 0x2b76(%rip), %rdi      # 0x65ff
xorl %eax, %eax
callq 0x2050
jmp 0x3df7
movq 0x5544(%rip), %rax      # 0x8fe0
movslq (%rax), %r12
subl %r12d, %ebp
cmpl $0x1, %ebp
jne 0x3b1d
cmpl $0x3, 0x5762(%rip)      # 0x9210
jl 0x3ac2
movq (%r14,%r12,8), %rsi
leaq 0x2b5a(%rip), %rdi      # 0x6615
xorl %eax, %eax
callq 0x2050
movq (%r14,%r12,8), %rdi
xorl %esi, %esi
callq 0x3eba
movq %rax, 0x18(%rsp)
cmpq $-0x1, %rax
jne 0x3b27
movq 0x54f9(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
movq (%r14,%r12,8), %rdx
leaq 0x2b3d(%rip), %rsi      # 0x662a
xorl %eax, %eax
callq 0x22c0
jmp 0x3df7
callq 0x286e
leaq 0x2ef1(%rip), %rdi      # 0x69f6
jmp 0x3b13
callq 0x286e
leaq 0x2ef5(%rip), %rdi      # 0x6a08
callq 0x22f0
jmp 0x3df7
callq 0x286e
jmp 0x3df7
cmpl $0x0, 0x56e2(%rip)      # 0x9210
je 0x3bec
movq %rax, %r15
movq (%r14,%r12,8), %rsi
movq 0x48(%rax), %rdx
movq 0x56b2(%rip), %rcx      # 0x91f8
movq 0x56b3(%rip), %r8       # 0x9200
leaq 0x2af3(%rip), %rdi      # 0x6647
xorl %eax, %eax
callq 0x2050
movq 0x20(%r15), %rsi
leaq 0x2b16(%rip), %rdi      # 0x667c
xorl %eax, %eax
callq 0x2050
cmpl $0x2, 0x569c(%rip)      # 0x9210
jl 0x3bec
leaq 0x2da0(%rip), %rdi      # 0x691d
callq 0x22f0
leaq 0x2db0(%rip), %rdi      # 0x6939
callq 0x22f0
cmpb $0x0, 0x32(%r15)
je 0x3bd7
leaq 0x2af8(%rip), %r14      # 0x6694
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x40(%r15), %rax
leaq (%rax,%r12), %rsi
movzbl 0xc(%rax,%r12), %edx
movzbl 0xd(%rax,%r12), %ecx
movzbl 0xb(%rax,%r12), %r8d
movq %r14, %rdi
xorl %eax, %eax
callq 0x2050
incq %r13
movzbl 0x32(%r15), %eax
addq $0xf, %r12
cmpq %rax, %r13
jb 0x3ba2
cmpl $0x3, 0x5632(%rip)      # 0x9210
jl 0x3bec
leaq 0x2d78(%rip), %rdi      # 0x695f
callq 0x22f0
leaq 0x20(%rsp), %rdi
callq 0x20e0
testq %rax, %rax
jne 0x3c20
movq 0x53d6(%rip), %rax      # 0x8fd8
movq (%rax), %rcx
leaq 0x2a9b(%rip), %rdi      # 0x66a7
movl $0x26, %esi
movl $0x1, %edx
callq 0x23a0
jmp 0x3ded
movq 0x5611(%rip), %rsi      # 0x9238
movq 0x561a(%rip), %rdx      # 0x9248
movq 0x560b(%rip), %rcx      # 0x9240
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
leaq 0x20(%rsp), %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2100
testq %rax, %rax
jne 0x3cb0
movq 0x5380(%rip), %rbx      # 0x8fd8
movq (%rbx), %rcx
leaq 0x2a6c(%rip), %rdi      # 0x66ce
movl $0x23, %esi
movl $0x1, %edx
callq 0x23a0
movq (%rbx), %rcx
leaq 0x2a77(%rip), %rdi      # 0x66f2
movl $0x11, %esi
movl $0x1, %edx
callq 0x23a0
movq (%rbx), %rbx
leaq 0x20(%rsp), %rdi
callq 0x20c0
leaq 0x26c2(%rip), %rsi      # 0x6360
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x3ded
movq %rax, %r14
cmpl $0x3, 0x5556(%rip)      # 0x9210
jl 0x3cd1
movq 0x5535(%rip), %rsi      # 0x91f8
leaq 0x2a3a(%rip), %rdi      # 0x6704
xorl %eax, %eax
callq 0x2050
movq 0x5520(%rip), %rsi      # 0x91f8
movq %r14, %rdi
callq 0x2190
cmpl $-0x1, %eax
jne 0x3d28
movq 0x52ec(%rip), %rbx      # 0x8fd8
movq (%rbx), %rdi
movq 0x5502(%rip), %rdx      # 0x91f8
leaq 0x2a20(%rip), %rsi      # 0x671d
xorl %eax, %eax
callq 0x22c0
movq (%rbx), %rbx
movq %r14, %rdi
callq 0x20c0
leaq 0x264a(%rip), %rsi      # 0x6360
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x3de5
testq %rbx, %rbx
je 0x3d59
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x2270
testl %eax, %eax
jne 0x3d59
leaq 0x20(%rsp), %rdi
callq 0x22e0
leaq 0x29ec(%rip), %rdi      # 0x673b
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movq 0x54f8(%rip), %rdi      # 0x9258
movq 0x18(%rsp), %rsi
callq 0x28c1
movq 0x54df(%rip), %rdi      # 0x9250
movq 0x18(%rsp), %rsi
callq 0x2a21
cmpl $0x0, 0x549a(%rip)      # 0x921c
jne 0x3da7
movq 0x5475(%rip), %rsi      # 0x9200
movq %r14, %rdi
callq 0x281c
testl %eax, %eax
jne 0x3e42
leaq 0x2bdf(%rip), %rdi      # 0x6981
jmp 0x3b13
cmpl $0x3, 0x5462(%rip)      # 0x9210
jl 0x3dbc
leaq 0x2be0(%rip), %rdi      # 0x6997
callq 0x22f0
movq 0x543d(%rip), %r15      # 0x9200
movq %r15, %rdi
callq 0x20d0
leaq 0xc(%rax), %rdi
callq 0x2280
testq %rax, %rax
jne 0x3e01
leaq 0x2bdf(%rip), %rdi      # 0x69bf
callq 0x22f0
movq %r14, %rdi
callq 0x2290
leaq 0x18(%rsp), %rdi
callq 0x4524
movl $0x1, %edi
callq 0x2250
movq %rax, %r12
leaq 0x294e(%rip), %rsi      # 0x6759
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
movq %r12, %rsi
callq 0x2130
movq %r12, %rdi
callq 0x2230
movq 0x53ce(%rip), %rsi      # 0x9200
movq 0x18(%rsp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2b4c
cmpl $0x1, 0x53d3(%rip)      # 0x921c
jg 0x3e5f
movq 0x53ae(%rip), %rsi      # 0x9200
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x3046
cmpl $0x3, 0x53aa(%rip)      # 0x9210
jl 0x3e74
leaq 0x2b78(%rip), %rdi      # 0x69e7
callq 0x22f0
movq %r14, %rdi
callq 0x2290
leaq 0x18(%rsp), %rdi
callq 0x4524
xorl %edi, %edi
callq 0x2250
nopl (%rax)