// File: /workspace/github_c_makefile_star2_O1/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, 0x5a0a(%rip)      # 0x9270
leaq 0x2d71(%rip), %r15      # 0x65de
movq 0x5754(%rip), %r13      # 0x8fc8
leaq 0x27e9(%rip), %r12      # 0x6064
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2090
cmpl $0x45, %eax
jle 0x38b0
leal -0x50(%rax), %ecx
cmpl $0x28, %ecx
ja 0x38cc
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5992(%rip)      # 0x9240
jmp 0x387b
cmpl $0x42, %eax
jle 0x3a28
cmpl $0x43, %eax
jne 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, %rbx
jmp 0x387b
cmpl $0x46, %eax
jne 0x387b
movl $0x0, 0x5915(%rip)      # 0x91f0
jmp 0x387b
cmpl $0x0, 0x5934(%rip)      # 0x9218
jne 0x3acc
movl $0x1, 0x5920(%rip)      # 0x9214
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x594a(%rip)      # 0x9250
jmp 0x387b
movq (%r13), %rsi
movl $0xb, %edx
leaq 0x5955(%rip), %rdi      # 0x9270
callq 0x2110
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5933(%rip)      # 0x9268
jmp 0x387b
movl $0x1, 0x58ec(%rip)      # 0x9230
jmp 0x387b
movl $0x1, 0x58d5(%rip)      # 0x9228
jmp 0x387b
incl 0x58be(%rip)            # 0x921c
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x588d(%rip)      # 0x9200
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5870(%rip)      # 0x91f8
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x58c3(%rip)      # 0x9260
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5896(%rip)      # 0x9248
jmp 0x387b
incl 0x5853(%rip)            # 0x9210
jmp 0x387b
movl $0x1, 0x5854(%rip)      # 0x9220
jmp 0x387b
movl $0x1, 0x5851(%rip)      # 0x922c
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5848(%rip)      # 0x9238
jmp 0x387b
movl $0x1, 0x5825(%rip)      # 0x9224
jmp 0x387b
movq (%r13), %rdi
callq 0x2380
movq %rax, 0x5844(%rip)      # 0x9258
jmp 0x387b
movl $0x1, 0x57f5(%rip)      # 0x9218
jmp 0x387b
cmpl $-0x1, %eax
je 0x3a5a
cmpl $0x3a, %eax
je 0x3abe
cmpl $0x3f, %eax
jne 0x387b
callq 0x282e
movq (%r14), %rsi
leaq 0x2bb1(%rip), %rdi      # 0x65ff
xorl %eax, %eax
callq 0x2050
jmp 0x3dc2
movq 0x557f(%rip), %rax      # 0x8fe0
movslq (%rax), %r15
subl %r15d, %ebp
cmpl $0x1, %ebp
jne 0x3ae2
cmpl $0x3, 0x579d(%rip)      # 0x9210
jl 0x3a87
movq (%r14,%r15,8), %rsi
leaq 0x2b95(%rip), %rdi      # 0x6615
xorl %eax, %eax
callq 0x2050
movq (%r14,%r15,8), %rdi
xorl %esi, %esi
callq 0x3e82
movq %rax, 0x18(%rsp)
cmpq $-0x1, %rax
jne 0x3aec
movq 0x5534(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
movq (%r14,%r15,8), %rdx
leaq 0x2b78(%rip), %rsi      # 0x662a
xorl %eax, %eax
callq 0x22c0
jmp 0x3dc2
callq 0x282e
leaq 0x2f2c(%rip), %rdi      # 0x69f6
jmp 0x3ad8
callq 0x282e
leaq 0x2f30(%rip), %rdi      # 0x6a08
callq 0x22f0
jmp 0x3dc2
callq 0x282e
jmp 0x3dc2
cmpl $0x0, 0x571d(%rip)      # 0x9210
je 0x3b30
movq (%r14,%r15,8), %rsi
movq 0x48(%rax), %rdx
movq 0x56f4(%rip), %rcx      # 0x91f8
movq 0x56f5(%rip), %r8       # 0x9200
leaq 0x2b35(%rip), %rdi      # 0x6647
xorl %eax, %eax
callq 0x2050
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x2b53(%rip), %rdi      # 0x667c
xorl %eax, %eax
callq 0x2050
cmpl $0x2, 0x56d9(%rip)      # 0x9210
jl 0x3ba2
leaq 0x2ddd(%rip), %rdi      # 0x691d
callq 0x22f0
leaq 0x2ded(%rip), %rdi      # 0x6939
callq 0x22f0
movq 0x18(%rsp), %rax
cmpb $0x0, 0x32(%rax)
je 0x3ba2
leaq 0x2b31(%rip), %r14      # 0x6694
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x40(%rax), %rax
leaq (%rax,%r15), %rsi
movzbl 0xc(%rax,%r15), %edx
movzbl 0xd(%rax,%r15), %ecx
movzbl 0xb(%rax,%r15), %r8d
movq %r14, %rdi
xorl %eax, %eax
callq 0x2050
incq %r12
movq 0x18(%rsp), %rax
movzbl 0x32(%rax), %ecx
addq $0xf, %r15
cmpq %rcx, %r12
jb 0x3b69
cmpl $0x3, 0x5667(%rip)      # 0x9210
jl 0x3bb7
leaq 0x2dad(%rip), %rdi      # 0x695f
callq 0x22f0
leaq 0x20(%rsp), %rdi
callq 0x20e0
testq %rax, %rax
jne 0x3beb
movq 0x540b(%rip), %rax      # 0x8fd8
movq (%rax), %rcx
leaq 0x2ad0(%rip), %rdi      # 0x66a7
movl $0x26, %esi
movl $0x1, %edx
callq 0x23a0
jmp 0x3db8
movq 0x5646(%rip), %rsi      # 0x9238
movq 0x564f(%rip), %rdx      # 0x9248
movq 0x5640(%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 0x3c7b
movq 0x53b5(%rip), %rbx      # 0x8fd8
movq (%rbx), %rcx
leaq 0x2aa1(%rip), %rdi      # 0x66ce
movl $0x23, %esi
movl $0x1, %edx
callq 0x23a0
movq (%rbx), %rcx
leaq 0x2aac(%rip), %rdi      # 0x66f2
movl $0x11, %esi
movl $0x1, %edx
callq 0x23a0
movq (%rbx), %rbx
leaq 0x20(%rsp), %rdi
callq 0x20c0
leaq 0x26f7(%rip), %rsi      # 0x6360
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x3db8
movq %rax, %r14
cmpl $0x3, 0x558b(%rip)      # 0x9210
jl 0x3c9c
movq 0x556a(%rip), %rsi      # 0x91f8
leaq 0x2a6f(%rip), %rdi      # 0x6704
xorl %eax, %eax
callq 0x2050
movq 0x5555(%rip), %rsi      # 0x91f8
movq %r14, %rdi
callq 0x2190
cmpl $-0x1, %eax
jne 0x3cf3
movq 0x5321(%rip), %rbx      # 0x8fd8
movq (%rbx), %rdi
movq 0x5537(%rip), %rdx      # 0x91f8
leaq 0x2a55(%rip), %rsi      # 0x671d
xorl %eax, %eax
callq 0x22c0
movq (%rbx), %rbx
movq %r14, %rdi
callq 0x20c0
leaq 0x267f(%rip), %rsi      # 0x6360
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x3db0
testq %rbx, %rbx
je 0x3d24
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x2270
testl %eax, %eax
jne 0x3d24
leaq 0x20(%rsp), %rdi
callq 0x22e0
leaq 0x2a21(%rip), %rdi      # 0x673b
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movq 0x552d(%rip), %rdi      # 0x9258
movq 0x18(%rsp), %rsi
callq 0x2881
movq 0x5514(%rip), %rdi      # 0x9250
movq 0x18(%rsp), %rsi
callq 0x29fb
cmpl $0x0, 0x54cf(%rip)      # 0x921c
jne 0x3d72
movq 0x54aa(%rip), %rsi      # 0x9200
movq %r14, %rdi
callq 0x27dc
testl %eax, %eax
jne 0x3e0d
leaq 0x2c14(%rip), %rdi      # 0x6981
jmp 0x3ad8
cmpl $0x3, 0x5497(%rip)      # 0x9210
jl 0x3d87
leaq 0x2c15(%rip), %rdi      # 0x6997
callq 0x22f0
movq 0x5472(%rip), %r15      # 0x9200
movq %r15, %rdi
callq 0x20d0
leaq 0xc(%rax), %rdi
callq 0x2280
testq %rax, %rax
jne 0x3dcc
leaq 0x2c14(%rip), %rdi      # 0x69bf
callq 0x22f0
movq %r14, %rdi
callq 0x2290
leaq 0x18(%rsp), %rdi
callq 0x4578
movl $0x1, %edi
callq 0x2250
movq %rax, %r12
leaq 0x2983(%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 0x5403(%rip), %rsi      # 0x9200
movq 0x18(%rsp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2b26
cmpl $0x1, 0x5408(%rip)      # 0x921c
jg 0x3e2a
movq 0x53e3(%rip), %rsi      # 0x9200
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x3034
cmpl $0x3, 0x53df(%rip)      # 0x9210
jl 0x3e3f
leaq 0x2bad(%rip), %rdi      # 0x69e7
callq 0x22f0
movq %r14, %rdi
callq 0x2290
leaq 0x18(%rsp), %rdi
callq 0x4578
xorl %edi, %edi
callq 0x2250