// File: /workspace/github_c_makefile_star2_O2/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, 0x5c26(%rip)      # 0x9270
leaq 0x2f8d(%rip), %r15      # 0x65de
movq 0x5970(%rip), %rbx      # 0x8fc8
leaq 0x2a05(%rip), %r12      # 0x6064
pushq $0xb
popq %r13
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2090
leal -0x50(%rax), %ecx
cmpl $0x28, %ecx
ja 0x3692
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5bb0(%rip)      # 0x9240
jmp 0x3663
cmpl $0x43, %eax
je 0x3800
cmpl $0x46, %eax
je 0x3812
cmpl $-0x1, %eax
je 0x382c
cmpl $0x3a, %eax
je 0x3893
cmpl $0x3f, %eax
jne 0x3663
jmp 0x38a9
cmpl $0x0, 0x5b51(%rip)      # 0x9218
jne 0x381e
movl $0x1, 0x5b3d(%rip)      # 0x9214
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5b68(%rip)      # 0x9250
jmp 0x3663
movq (%rbx), %rsi
leaq 0x5b79(%rip), %rdi      # 0x9270
movq %r13, %rdx
callq 0x2110
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5b55(%rip)      # 0x9268
jmp 0x3663
movl $0x1, 0x5b0e(%rip)      # 0x9230
jmp 0x3663
movl $0x1, 0x5af7(%rip)      # 0x9228
jmp 0x3663
incl 0x5ae0(%rip)            # 0x921c
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5ab0(%rip)      # 0x9200
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5a94(%rip)      # 0x91f8
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5ae8(%rip)      # 0x9260
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5abc(%rip)      # 0x9248
jmp 0x3663
incl 0x5a79(%rip)            # 0x9210
jmp 0x3663
movl $0x1, 0x5a7a(%rip)      # 0x9220
jmp 0x3663
movl $0x1, 0x5a77(%rip)      # 0x922c
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5a6f(%rip)      # 0x9238
jmp 0x3663
movl $0x1, 0x5a4c(%rip)      # 0x9224
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x5a6c(%rip)      # 0x9258
jmp 0x3663
movl $0x1, 0x5a1d(%rip)      # 0x9218
jmp 0x3663
movq (%rbx), %rdi
callq 0x2380
movq %rax, 0x18(%rsp)
jmp 0x3663
andl $0x0, 0x59d7(%rip)      # 0x91f0
jmp 0x3663
callq 0x2508
leaq 0x31de(%rip), %rdi      # 0x6a08
jmp 0x389f
movq 0x57ad(%rip), %rax      # 0x8fe0
movslq (%rax), %rbx
subl %ebx, %ebp
cmpl $0x1, %ebp
jne 0x38c4
cmpl $0x3, 0x59c8(%rip)      # 0x9210
jl 0x385c
movq (%r14,%rbx,8), %rsi
leaq 0x2dc0(%rip), %rdi      # 0x6615
xorl %eax, %eax
callq 0x2050
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x3c4a
movq %rax, 0x10(%rsp)
cmpq $-0x1, %rax
jne 0x38ce
movq 0x575f(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
movq (%r14,%rbx,8), %rdx
leaq 0x2da3(%rip), %rsi      # 0x662a
xorl %eax, %eax
callq 0x22c0
jmp 0x3b92
callq 0x2508
leaq 0x3157(%rip), %rdi      # 0x69f6
callq 0x22f0
jmp 0x3b92
callq 0x2508
movq (%r14), %rsi
leaq 0x2d47(%rip), %rdi      # 0x65ff
xorl %eax, %eax
callq 0x2050
jmp 0x3b92
callq 0x2508
jmp 0x3b92
cmpl $0x0, 0x593b(%rip)      # 0x9210
je 0x398b
movq %rax, %r15
movq (%r14,%rbx,8), %rsi
movq 0x48(%rax), %rdx
movq 0x590b(%rip), %rcx      # 0x91f8
movq 0x590c(%rip), %r8       # 0x9200
leaq 0x2d4c(%rip), %rdi      # 0x6647
xorl %eax, %eax
callq 0x2050
movq 0x20(%r15), %rsi
leaq 0x2d6f(%rip), %rdi      # 0x667c
xorl %eax, %eax
callq 0x2050
cmpl $0x2, 0x58f5(%rip)      # 0x9210
jl 0x398b
leaq 0x2ff9(%rip), %rdi      # 0x691d
callq 0x22f0
leaq 0x3009(%rip), %rdi      # 0x6939
callq 0x22f0
leaq 0x2d58(%rip), %r14      # 0x6694
xorl %ebx, %ebx
xorl %r12d, %r12d
movzbl 0x32(%r15), %eax
cmpq %rax, %r12
jae 0x3976
movq 0x40(%r15), %rax
leaq (%rax,%rbx), %rsi
movzbl 0xc(%rax,%rbx), %edx
movzbl 0xd(%rax,%rbx), %ecx
movzbl 0xb(%rax,%rbx), %r8d
movq %r14, %rdi
xorl %eax, %eax
callq 0x2050
incq %r12
addq $0xf, %rbx
jmp 0x3941
cmpl $0x3, 0x5893(%rip)      # 0x9210
jl 0x398b
leaq 0x2fd9(%rip), %rdi      # 0x695f
callq 0x22f0
leaq 0x20(%rsp), %rdi
callq 0x20e0
testq %rax, %rax
jne 0x39bb
movq 0x5637(%rip), %rax      # 0x8fd8
movq (%rax), %rcx
leaq 0x2cfc(%rip), %rdi      # 0x66a7
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x23a0
jmp 0x3b88
movq 0x5876(%rip), %rsi      # 0x9238
movq 0x587f(%rip), %rdx      # 0x9248
movq 0x5870(%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 0x3a46
movq 0x55e5(%rip), %r14      # 0x8fd8
movq (%r14), %rcx
leaq 0x2cd1(%rip), %rdi      # 0x66ce
pushq $0x23
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x23a0
movq (%r14), %rcx
leaq 0x2cdd(%rip), %rdi      # 0x66f2
pushq $0x11
popq %rsi
movq %rbx, %rdx
callq 0x23a0
movq (%r14), %rbx
leaq 0x20(%rsp), %rdi
callq 0x20c0
leaq 0x292c(%rip), %rsi      # 0x6360
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x3b88
movq %rax, %r14
cmpl $0x3, 0x57c0(%rip)      # 0x9210
jl 0x3a67
movq 0x579f(%rip), %rsi      # 0x91f8
leaq 0x2ca4(%rip), %rdi      # 0x6704
xorl %eax, %eax
callq 0x2050
movq 0x578a(%rip), %rsi      # 0x91f8
movq %r14, %rdi
callq 0x2190
cmpl $-0x1, %eax
jne 0x3abe
movq 0x5556(%rip), %rbx      # 0x8fd8
movq (%rbx), %rdi
movq 0x576c(%rip), %rdx      # 0x91f8
leaq 0x2c8a(%rip), %rsi      # 0x671d
xorl %eax, %eax
callq 0x22c0
movq (%rbx), %rbx
movq %r14, %rdi
callq 0x20c0
leaq 0x28b4(%rip), %rsi      # 0x6360
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x3b80
cmpq $0x0, 0x18(%rsp)
je 0x3af4
leaq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x2270
testl %eax, %eax
jne 0x3af4
leaq 0x20(%rsp), %rdi
callq 0x22e0
leaq 0x2c51(%rip), %rdi      # 0x673b
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movq 0x575d(%rip), %rdi      # 0x9258
movq 0x10(%rsp), %rsi
callq 0x255b
movq 0x5744(%rip), %rdi      # 0x9250
movq 0x10(%rsp), %rsi
callq 0x26db
cmpl $0x0, 0x56ff(%rip)      # 0x921c
jne 0x3b42
movq 0x56da(%rip), %rsi      # 0x9200
movq %r14, %rdi
callq 0x24bc
testl %eax, %eax
jne 0x3bdd
leaq 0x2e44(%rip), %rdi      # 0x6981
jmp 0x389f
cmpl $0x3, 0x56c7(%rip)      # 0x9210
jl 0x3b57
leaq 0x2e45(%rip), %rdi      # 0x6997
callq 0x22f0
movq 0x56a2(%rip), %r15      # 0x9200
movq %r15, %rdi
callq 0x20d0
leaq 0xc(%rax), %rdi
callq 0x2280
testq %rax, %rax
jne 0x3b9a
leaq 0x2e44(%rip), %rdi      # 0x69bf
callq 0x22f0
movq %r14, %rdi
callq 0x2290
leaq 0x10(%rsp), %rdi
callq 0x4310
pushq $0x1
popq %rdi
callq 0x2250
movq %rax, %r12
leaq 0x2bb5(%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 0x5635(%rip), %rsi      # 0x9200
movq 0x10(%rsp), %rdx
movq %r14, %rdi
movq 0x18(%rsp), %rcx
callq 0x2832
cmpl $0x1, 0x5638(%rip)      # 0x921c
jg 0x3bfa
movq 0x5613(%rip), %rsi      # 0x9200
movq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x2d94
cmpl $0x3, 0x560f(%rip)      # 0x9210
jl 0x3c0f
leaq 0x2ddd(%rip), %rdi      # 0x69e7
callq 0x22f0
movq %r14, %rdi
callq 0x2290
leaq 0x10(%rsp), %rdi
callq 0x4310
xorl %edi, %edi
callq 0x2250