// File: /workspace/github_c_makefile_star2_O3/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, 0x669f(%rip)      # 0x9210
jl 0x2b7f
leaq 0x3d64(%rip), %rdi      # 0x68de
callq 0x22f0
imull $0x64, 0x669e(%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 0x302d
movq %rax, %r14
leaq 0x3641(%rip), %rsi      # 0x61fd
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2080
cmpl $0x0, 0x6654(%rip)      # 0x9224
je 0x2c10
movq %r14, %rdi
callq 0x20d0
leaq (%rax,%r14), %rdi
leaq 0x362c(%rip), %rsi      # 0x6211
movl $0x48, %edx
callq 0x2170
movq %r14, %rdi
callq 0x20d0
movups 0x365b(%rip), %xmm0     # 0x6259
movups %xmm0, (%r14,%rax)
movups 0x365a(%rip), %xmm0     # 0x6264
movups %xmm0, 0xb(%r14,%rax)
movq %rbp, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
cmpb $0x0, 0x32(%r13)
je 0x2eed
movw $0x60, %r12w
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x40(%r13), %rax
cmpb $0x0, (%rax,%r15)
je 0x2ec7
testl %ebx, %ebx
je 0x2c59
movq %r14, %rdi
callq 0x20d0
movl $0x90a2c, (%r14,%rax)   # imm = 0x90A2C
cmpl $0x0, 0x65c0(%rip)      # 0x9220
je 0x2c6e
movq 0x40(%r13), %rdi
addq %r15, %rdi
callq 0x5569
movq %r14, %rdi
callq 0x20d0
movw %r12w, (%r14,%rax)
movq 0x40(%r13), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0x2370
movq %r14, %rdi
callq 0x20d0
movw %r12w, (%r14,%rax)
movq 0x40(%r13), %rax
movzbl 0xb(%rax,%r15), %ecx
addl $-0x42, %ecx
cmpl $0x17, %ecx
ja 0x2e56
leaq 0x3350(%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 0x2e56
movq %r14, %rdi
callq 0x20d0
movabsq $0x6d69746574616420, %rcx # imm = 0x6D69746574616420
movq %rcx, (%r14,%rax)
movw $0x65, 0x8(%r14,%rax)
jmp 0x2e56
movq %r14, %rdi
callq 0x20d0
movl $0x74616420, (%r14,%rax) # imm = 0x74616420
movw $0x65, 0x4(%r14,%rax)
jmp 0x2e56
movq %r14, %rdi
callq 0x20d0
movabsq $0x6c616d6963656420, %rcx # imm = 0x6C616D6963656420
movq %rcx, (%r14,%rax)
movabsq $0x29342c3132286c, %rcx # imm = 0x29342C3132286C
jmp 0x2d5c
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 0x2e56
movzbl 0xd(%rax,%r15), %edx
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
leaq 0x352e(%rip), %rsi      # 0x62a9
jmp 0x2e44
movl 0x646a(%rip), %ebx      # 0x91f0
movq %r14, %rdi
callq 0x20d0
addq %r14, %rax
testl %ebx, %ebx
je 0x2e1f
movabsq $0x7261686372617620, %rcx # imm = 0x7261686372617620
movq %rcx, (%rax)
movb $0x0, 0x8(%rax)
jmp 0x2e2b
movq %r14, %rdi
callq 0x20d0
movl $0x78657420, (%r14,%rax) # imm = 0x78657420
movw $0x74, 0x4(%r14,%rax)
jmp 0x2e56
movb 0xd(%rax,%r15), %bl
movq %r14, %rdi
callq 0x20d0
addq %r14, %rax
testb %bl, %bl
je 0x2ede
movw $0x6c, 0x4(%rax)
movl $0x61657220, (%rax)     # imm = 0x61657220
jmp 0x2e56
movq %r14, %rdi
callq 0x20d0
movabsq $0x656c62756f6420, %rcx # imm = 0x656C62756F6420
movq %rcx, (%r14,%rax)
jmp 0x2e56
movq %r14, %rdi
callq 0x20d0
movl $0x746e6920, (%r14,%rax) # imm = 0x746E6920
movb $0x0, 0x4(%r14,%rax)
jmp 0x2e56
movw $0x72, 0x4(%rax)
movl $0x61686320, (%rax)     # imm = 0x61686320
movq 0x40(%r13), %rax
movzbl 0xc(%rax,%r15), %edx
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
leaq 0x3cb4(%rip), %rsi      # 0x6af8
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2370
movq (%rsp), %r13
movq 0x40(%r13), %rdi
addq %r15, %rdi
leaq 0x6408(%rip), %rsi      # 0x9270
callq 0x22b0
testl %eax, %eax
je 0x2e9f
movl $0x1, %ebx
cmpl $0x0, 0x63ab(%rip)      # 0x9228
jne 0x2ec7
movq %r14, %rdi
callq 0x20d0
movabsq $0x6c756e20746f6e20, %rcx # imm = 0x6C756E20746F6E20
movq %rcx, (%r14,%rax)
movw $0x6c, 0x8(%r14,%rax)
jmp 0x2ec7
movq %r14, %rdi
callq 0x20d0
movabsq $0x79656b20797261, %rcx # imm = 0x79656B20797261
movq %rcx, 0xe(%r14,%rax)
movups 0x341b(%rip), %xmm0     # 0x62d8
movups %xmm0, (%r14,%rax)
movl $0x1, %ebx
incq %rbp
movzbl 0x32(%r13), %eax
addq $0xf, %r15
cmpq %rax, %rbp
jb 0x2c36
jmp 0x2eed
movb $0x0, 0x4(%rax)
movl $0x746e6920, (%rax)     # imm = 0x746E6920
jmp 0x2e56
movq 0x636c(%rip), %rdi      # 0x9260
testq %rdi, %rdi
je 0x2f56
leaq 0x3550(%rip), %rsi      # 0x6450
callq 0x2310
testq %rax, %rax
je 0x2f56
movq %rax, %rbp
movabsq $0x285845444e492c, %r15 # imm = 0x285845444E492C
leaq 0x3532(%rip), %rbx      # 0x6450
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
testq %rax, %rax
jne 0x2f1e
movq %r14, %rdi
callq 0x20d0
movw $0xa29, (%r14,%rax)     # imm = 0xA29
movb $0x0, 0x2(%r14,%rax)
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
movq 0x10(%rsp), %r12
je 0x2fb0
movq %r14, %rdi
callq 0x20d0
movups 0x336f(%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, 0x6259(%rip)      # 0x9210
jl 0x3003
leaq 0x3935(%rip), %rdi      # 0x68f5
callq 0x22f0
movq %r14, %rdi
callq 0x22f0
movzbl 0x32(%r13), %ebx
imulq $0x3c, %rbx, %r15
movq %r12, %rdi
callq 0x20d0
addq %rax, %r15
addq $0x1d, %r15
movq %r14, %rdi
callq 0x20d0
leaq 0x3316(%rip), %rdi      # 0x630a
movl %ebx, %esi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2050
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2130
testl %eax, %eax
jne 0x3038
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230
movq %rsp, %rsi
movq %r15, %rdi
callq 0x246e
movq %rsp, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2400