// File: /workspace/github_c_makefile_star2_O2/sonicse[P]dbf2mysql/dbf2mysql.c
// Function: do_create
void do_create(MYSQL *SQLsock, char *table, dbhead *dbh, char *charset) {
    /* Patched by GLC to support date and boolean fields */
    /* Patched by WKV to support Visual FoxPro fields */
    char *query, *s;
    char t[20];
    int i, first_done;

    if (verbose > 2) {
        printf("Building CREATE-clause\n");
    }

    if (!(query = (char *) malloc((express * 100) +
            (dbh->db_nfields * 60) + 29 + strlen(table)))) {
        fprintf(stderr, "Memory allocation error in function do_create\n");
        mysql_close(SQLsock);
        dbf_close(&dbh);
        exit(1);
    }

    sprintf(query, "CREATE TABLE `%s` (", table);
    if (express) {
        strcat(query,
                "_rec int(10) unsigned DEFAULT '0' NOT NULL auto_increment PRIMARY KEY,\n");
        strcat(query, "_timestamp timestamp(14),\n");
    }
    first_done = 0;
    for (i = 0; i < dbh->db_nfields; i++) {
        if (!strlen(dbh->db_fields[i].db_name)) {
            continue;
            /* skip field if length of name == 0 */
        }
        if (first_done) {
            strcat(query, ",\n\t");
        }
        first_done = 1;
        if (fieldlow)
            strtolower(dbh->db_fields[i].db_name);

        strcat(query, "`\0");
        strcat(query, dbh->db_fields[i].db_name);
        strcat(query, "`\0");
        switch (dbh->db_fields[i].db_type) {
            case 'C':
                if (var_chars)
                    strcat(query, " varchar");
                else
                    strcat(query, " char");
                sprintf(t, "(%d)", dbh->db_fields[i].db_flen);
                strcat(query, t);
                break;

            case 'N':
                if (dbh->db_fields[i].db_dec != 0) {
                    strcat(query, " real"); /* decimal the better choice? */
                } else {
                    strcat(query, " int");
                }
                break;

            case 'L':
                /* strcat(query, " char (1)"); */
                strcat(query, " enum('F','T')");
                break;

            case 'D':
                strcat(query, " date");
                break;

            case 'M':
                strcat(query, " text");
                break;

            case 'F':
                strcat(query, " double");
                break;

            case 'B': /* ?Depends on DB vs FoxPro? Former is memo, latter is Binary */
                sprintf(t, " decimal(15,%d)", dbh->db_fields[i].db_dec);
                strcat(query, t);
                break;

            case 'G':
                strcat(query, " blob");
                break;

            case 'P':
                strcat(query, " blob");
                break;

            case 'Y':
                strcat(query, " decimal(21,4)");
                break;

            case 'T':
                strcat(query, " datetime");
                break;

            case 'I':
                strcat(query, " int");
                break;

        }
        if (strcmp(dbh->db_fields[i].db_name, primary) == 0) {
            strcat(query, " not null primary key");
        } else if (!null_fields)
            strcat(query, " not null");
    }

    if (indexes) /* add INDEX statements */ {
        for (s = strtok(indexes, ","); s != NULL; s = strtok(NULL, ",")) {
            strcat(query, ",INDEX(");
            strcat(query, s);
            strcat(query, ")");
        }
    }

    strcat(query, ")\n");

    if (charset) {
        strcat(query, " DEFAULT CHARSET=");
        strcat(query, charset);
        strcat(query, "\n");
    }

    if (verbose > 2) {
        printf("Sending create-clause\n");
        printf("%s\n", query);
        printf("fields in dbh %d, allocated mem for query %zu, query size %zu\n",
                dbh->db_nfields, (dbh->db_nfields * 60) + 29 + strlen(table), strlen(query));
    }

    if (mysql_query(SQLsock, query) != 0) {
        fprintf(stderr, "Error creating table!\n");
        fprintf(stderr, "Detailed report: %s\n", mysql_error(SQLsock));
        dbf_close(&dbh);
        free(query);
        mysql_close(SQLsock);
        exit(1);
    }

    free(query);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %rbp
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r15
movq %rdx, (%rsp)
cmpl $0x3, 0x69b9(%rip)      # 0x9210
jl 0x2865
leaq 0x407e(%rip), %rdi      # 0x68de
callq 0x22f0
imull $0x64, 0x69b8(%rip), %eax # 0x9224
movzbl 0x32(%r13), %ecx
imull $0x3c, %ecx, %ecx
addl %ecx, %eax
addl $0x1d, %eax
movslq %eax, %rbx
movq %r12, %rdi
callq 0x20d0
addq %rax, %rbx
movq %rbx, %rdi
callq 0x2280
testq %rax, %rax
je 0x2d0b
movq %rax, %r14
movq %rbp, 0x10(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x3951(%rip), %rsi      # 0x61fd
movq %rax, %rdi
movq %r12, 0x8(%rsp)
movq %r12, %rdx
xorl %eax, %eax
callq 0x2080
cmpl $0x0, 0x695f(%rip)      # 0x9224
je 0x2903
movq %r14, %rdi
callq 0x20d0
leaq (%rax,%r14), %rdi
leaq 0x3937(%rip), %rsi      # 0x6211
pushq $0x48
popq %rdx
callq 0x2170
movq %r14, %rdi
callq 0x20d0
movups 0x3968(%rip), %xmm0     # 0x6259
movups %xmm0, (%r14,%rax)
movups 0x3967(%rip), %xmm0     # 0x6264
movups %xmm0, 0xb(%r14,%rax)
movw $0x60, %r12w
leaq 0x6961(%rip), %r15      # 0x9270
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %eax, %eax
movzbl 0x32(%r13), %ecx
cmpq %rcx, %rbx
jae 0x2bd5
movq 0x40(%r13), %rcx
cmpb $0x0, (%rcx,%rbp)
je 0x2bc9
testl %eax, %eax
je 0x2945
movq %r14, %rdi
callq 0x20d0
movl $0x90a2c, (%r14,%rax)   # imm = 0x90A2C
cmpl $0x0, 0x68d4(%rip)      # 0x9220
je 0x295a
movq 0x40(%r13), %rdi
addq %rbp, %rdi
callq 0x521b
movq %r14, %rdi
callq 0x20d0
movw %r12w, (%r14,%rax)
movq 0x40(%r13), %rsi
addq %rbp, %rsi
movq %r14, %rdi
callq 0x2370
movq %r14, %rdi
callq 0x20d0
movw %r12w, (%r14,%rax)
movq 0x40(%r13), %rax
movzbl 0xb(%rax,%rbp), %ecx
addl $-0x42, %ecx
cmpl $0x17, %ecx
ja 0x2b63
leaq 0x3665(%rip), %rdx      # 0x6004
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %r14, %rdi
callq 0x20d0
movl $0x6f6c6220, (%r14,%rax) # imm = 0x6F6C6220
movw $0x62, 0x4(%r14,%rax)
jmp 0x2b63
movq %r14, %rdi
callq 0x20d0
movabsq $0x6d69746574616420, %rcx # imm = 0x6D69746574616420
movq %rcx, (%r14,%rax)
movw $0x65, 0x8(%r14,%rax)
jmp 0x2b63
movq %r14, %rdi
callq 0x20d0
movl $0x74616420, (%r14,%rax) # imm = 0x74616420
movw $0x65, 0x4(%r14,%rax)
jmp 0x2b63
movq %r14, %rdi
callq 0x20d0
movabsq $0x6c616d6963656420, %rcx # imm = 0x6C616D6963656420
movq %rcx, (%r14,%rax)
movabsq $0x29342c3132286c, %rcx # imm = 0x29342C3132286C
jmp 0x2a47
movq %r14, %rdi
callq 0x20d0
movabsq $0x4627286d756e6520, %rcx # imm = 0x4627286D756E6520
movq %rcx, (%r14,%rax)
movabsq $0x292754272c2746, %rcx # imm = 0x292754272C2746
movq %rcx, 0x7(%r14,%rax)
jmp 0x2b63
movzbl 0xd(%rax,%rbp), %edx
leaq 0x20(%rsp), %r13
movq %r13, %rdi
leaq 0x3844(%rip), %rsi      # 0x62a9
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
movq %r13, %rsi
callq 0x2370
jmp 0x2b63
movl 0x676d(%rip), %r15d     # 0x91f0
movq %r14, %rdi
callq 0x20d0
addq %r14, %rax
testl %r15d, %r15d
je 0x2b1a
movabsq $0x7261686372617620, %rcx # imm = 0x7261686372617620
movq %rcx, (%rax)
movb $0x0, 0x8(%rax)
jmp 0x2b26
movq %r14, %rdi
callq 0x20d0
movl $0x78657420, (%r14,%rax) # imm = 0x78657420
movw $0x74, 0x4(%r14,%rax)
jmp 0x2b63
movb 0xd(%rax,%rbp), %r15b
movq %r14, %rdi
callq 0x20d0
addq %r14, %rax
testb %r15b, %r15b
je 0x2b52
movw $0x6c, 0x4(%rax)
movl $0x61657220, (%rax)     # imm = 0x61657220
jmp 0x2b5c
movq %r14, %rdi
callq 0x20d0
movabsq $0x656c62756f6420, %rcx # imm = 0x656C62756F6420
movq %rcx, (%r14,%rax)
jmp 0x2b63
movq %r14, %rdi
callq 0x20d0
movl $0x746e6920, (%r14,%rax) # imm = 0x746E6920
movb $0x0, 0x4(%r14,%rax)
jmp 0x2b63
movw $0x72, 0x4(%rax)
movl $0x61686320, (%rax)     # imm = 0x61686320
movq 0x40(%r13), %rax
movzbl 0xc(%rax,%rbp), %edx
leaq 0x20(%rsp), %r15
movq %r15, %rdi
leaq 0x3fba(%rip), %rsi      # 0x6af8
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
movq %r15, %rsi
callq 0x2370
jmp 0x2b5c
movb $0x0, 0x4(%rax)
movl $0x746e6920, (%rax)     # imm = 0x746E6920
leaq 0x670d(%rip), %r15      # 0x9270
movq (%rsp), %r13
movq 0x40(%r13), %rdi
addq %rbp, %rdi
movq %r15, %rsi
callq 0x22b0
testl %eax, %eax
je 0x2ba3
cmpl $0x0, 0x66a7(%rip)      # 0x9228
jne 0x2bc6
movq %r14, %rdi
callq 0x20d0
movabsq $0x6c756e20746f6e20, %rcx # imm = 0x6C756E20746F6E20
movq %rcx, (%r14,%rax)
movw $0x6c, 0x8(%r14,%rax)
jmp 0x2bc6
movq %r14, %rdi
callq 0x20d0
movabsq $0x79656b20797261, %rcx # imm = 0x79656B20797261
movq %rcx, 0xe(%r14,%rax)
movups 0x3717(%rip), %xmm0     # 0x62d8
movups %xmm0, (%r14,%rax)
pushq $0x1
popq %rax
incq %rbx
addq $0xf, %rbp
jmp 0x2915
movq 0x6684(%rip), %rdi      # 0x9260
testq %rdi, %rdi
movq 0x18(%rsp), %r12
je 0x2c3c
leaq 0x3863(%rip), %rbx      # 0x6450
movq %rbx, %rsi
callq 0x2310
movq %rax, %rbp
movabsq $0x285845444e492c, %r15 # imm = 0x285845444E492C
testq %rbp, %rbp
je 0x2c3c
movq %r14, %rdi
callq 0x20d0
movq %r15, (%r14,%rax)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2370
movq %r14, %rdi
callq 0x20d0
movw $0x29, (%r14,%rax)
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2310
movq %rax, %rbp
jmp 0x2c02
movq %r14, %rdi
callq 0x20d0
movw $0xa29, (%r14,%rax)     # imm = 0xA29
movb $0x0, 0x2(%r14,%rax)
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x2c91
movq %r14, %rdi
callq 0x20d0
movups 0x368e(%rip), %xmm0     # 0x62f8
movups %xmm0, (%r14,%rax)
movw $0x3d, 0x10(%r14,%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2370
movq %r14, %rdi
callq 0x20d0
movw $0xa, (%r14,%rax)
cmpl $0x3, 0x6578(%rip)      # 0x9210
jl 0x2ce6
leaq 0x3c54(%rip), %rdi      # 0x68f5
callq 0x22f0
movq %r14, %rdi
callq 0x22f0
movzbl 0x32(%r13), %ebx
imulq $0x3c, %rbx, %r15
movq 0x8(%rsp), %rdi
callq 0x20d0
addq %rax, %r15
addq $0x1d, %r15
movq %r14, %rdi
callq 0x20d0
leaq 0x3633(%rip), %rdi      # 0x630a
movl %ebx, %esi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
movq %r14, %rsi
callq 0x2130
testl %eax, %eax
jne 0x2d39
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230
movq 0x62c6(%rip), %rax      # 0x8fd8
movq (%rax), %rcx
leaq 0x34b2(%rip), %rdi      # 0x61ce
pushq $0x2e
popq %rsi
pushq $0x1
popq %rdx
callq 0x23a0
movq %r15, %rdi
callq 0x2290
movq %rsp, %rdi
callq 0x4310
jmp 0x2d8c
movq 0x6298(%rip), %rbx      # 0x8fd8
movq (%rbx), %rcx
leaq 0x35ff(%rip), %rdi      # 0x6349
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x23a0
movq (%rbx), %rbx
movq %r12, %rdi
callq 0x20c0
leaq 0x35f9(%rip), %rsi      # 0x6360
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
movq %rsp, %rdi
callq 0x4310
movq %r14, %rdi
callq 0x2230
movq %r12, %rdi
callq 0x2290
pushq $0x1
popq %rdi
callq 0x2250