// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rsi, %r15
movq %rdi, %rbx
movq %rdx, (%rsp)
cmpl $0x3, 0x66c8(%rip)      # 0x9210
jl 0x2b56
leaq 0x3d8d(%rip), %rdi      # 0x68de
callq 0x22f0
imull $0x64, 0x66c7(%rip), %eax # 0x9224
movq (%rsp), %rcx
movzbl 0x32(%rcx), %ecx
imull $0x3c, %ecx, %ecx
addl %ecx, %eax
addl $0x1d, %eax
movslq %eax, %r14
movq %r15, %rdi
callq 0x20d0
addq %rax, %r14
movq %r14, %rdi
callq 0x2280
testq %rax, %rax
je 0x301b
movq %rax, %r14
movq %r12, 0x10(%rsp)
leaq 0x3662(%rip), %rsi      # 0x61fd
movq %rax, %rdi
movq %r15, 0x8(%rsp)
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
cmpl $0x0, 0x6670(%rip)      # 0x9224
je 0x2bf4
movq %r14, %rdi
callq 0x20d0
leaq (%rax,%r14), %rdi
leaq 0x3648(%rip), %rsi      # 0x6211
movl $0x48, %edx
callq 0x2170
movq %r14, %rdi
callq 0x20d0
movups 0x3677(%rip), %xmm0     # 0x6259
movups %xmm0, (%r14,%rax)
movups 0x3676(%rip), %xmm0     # 0x6264
movups %xmm0, 0xb(%r14,%rax)
movq %rbx, 0x18(%rsp)
movq (%rsp), %r13
cmpb $0x0, 0x32(%r13)
je 0x2ed8
movw $0x60, %bx
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movq 0x40(%r13), %rax
cmpb $0x0, (%rax,%rbp)
je 0x2eae
testl %r15d, %r15d
je 0x2c37
movq %r14, %rdi
callq 0x20d0
movl $0x90a2c, (%r14,%rax)   # imm = 0x90A2C
cmpl $0x0, 0x65e2(%rip)      # 0x9220
je 0x2c4c
movq 0x40(%r13), %rdi
addq %rbp, %rdi
callq 0x55d9
movq %r14, %rdi
callq 0x20d0
movw %bx, (%r14,%rax)
movq (%rsp), %r13
movq 0x40(%r13), %rsi
addq %rbp, %rsi
movq %r14, %rdi
callq 0x2370
movq %r14, %rdi
callq 0x20d0
movw %bx, (%r14,%rax)
movq 0x40(%r13), %rax
movzbl 0xb(%rax,%rbp), %ecx
addl $-0x42, %ecx
cmpl $0x17, %ecx
ja 0x2e3b
leaq 0x336f(%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 0x2e3b
movq %r14, %rdi
callq 0x20d0
movabsq $0x6d69746574616420, %rcx # imm = 0x6D69746574616420
movq %rcx, (%r14,%rax)
movw $0x65, 0x8(%r14,%rax)
jmp 0x2e3b
movq %r14, %rdi
callq 0x20d0
movl $0x74616420, (%r14,%rax) # imm = 0x74616420
movw $0x65, 0x4(%r14,%rax)
jmp 0x2e3b
movq %r14, %rdi
callq 0x20d0
movabsq $0x6c616d6963656420, %rcx # imm = 0x6C616D6963656420
movq %rcx, (%r14,%rax)
movabsq $0x29342c3132286c, %rcx # imm = 0x29342C3132286C
jmp 0x2d3d
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 0x2e3b
movzbl 0xd(%rax,%rbp), %edx
leaq 0x20(%rsp), %r15
movq %r15, %rdi
leaq 0x354e(%rip), %rsi      # 0x62a9
jmp 0x2e29
movl 0x6489(%rip), %r15d     # 0x91f0
movq %r14, %rdi
callq 0x20d0
addq %r14, %rax
testl %r15d, %r15d
je 0x2e05
movabsq $0x7261686372617620, %rcx # imm = 0x7261686372617620
movq %rcx, (%rax)
movb $0x0, 0x8(%rax)
jmp 0x2e11
movq %r14, %rdi
callq 0x20d0
movl $0x78657420, (%r14,%rax) # imm = 0x78657420
movw $0x74, 0x4(%r14,%rax)
jmp 0x2e3b
movb 0xd(%rax,%rbp), %r15b
movq %r14, %rdi
callq 0x20d0
addq %r14, %rax
testb %r15b, %r15b
je 0x2ec9
movw $0x6c, 0x4(%rax)
movl $0x61657220, (%rax)     # imm = 0x61657220
jmp 0x2e3b
movq %r14, %rdi
callq 0x20d0
movabsq $0x656c62756f6420, %rcx # imm = 0x656C62756F6420
movq %rcx, (%r14,%rax)
jmp 0x2e3b
movq %r14, %rdi
callq 0x20d0
movl $0x746e6920, (%r14,%rax) # imm = 0x746E6920
movb $0x0, 0x4(%r14,%rax)
jmp 0x2e3b
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 0x3ccf(%rip), %rsi      # 0x6af8
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
movq %r15, %rsi
callq 0x2370
movq (%rsp), %rax
movq 0x40(%rax), %rdi
addq %rbp, %rdi
leaq 0x6423(%rip), %rsi      # 0x9270
callq 0x22b0
testl %eax, %eax
je 0x2e85
movl $0x1, %r15d
cmpl $0x0, 0x63c5(%rip)      # 0x9228
jne 0x2eae
movq %r14, %rdi
callq 0x20d0
movabsq $0x6c756e20746f6e20, %rcx # imm = 0x6C756E20746F6E20
movq %rcx, (%r14,%rax)
movw $0x6c, 0x8(%r14,%rax)
jmp 0x2eae
movq %r14, %rdi
callq 0x20d0
movabsq $0x79656b20797261, %rcx # imm = 0x79656B20797261
movq %rcx, 0xe(%r14,%rax)
movups 0x3435(%rip), %xmm0     # 0x62d8
movups %xmm0, (%r14,%rax)
movl $0x1, %r15d
incq %r12
movq (%rsp), %r13
movzbl 0x32(%r13), %eax
addq $0xf, %rbp
cmpq %rax, %r12
jb 0x2c14
jmp 0x2ed8
movb $0x0, 0x4(%rax)
movl $0x746e6920, (%rax)     # imm = 0x746E6920
jmp 0x2e3b
movq 0x6381(%rip), %rdi      # 0x9260
testq %rdi, %rdi
je 0x2f41
leaq 0x3565(%rip), %rsi      # 0x6450
callq 0x2310
testq %rax, %rax
je 0x2f41
movq %rax, %rbp
movabsq $0x285845444e492c, %rbx # imm = 0x285845444E492C
leaq 0x3547(%rip), %r13      # 0x6450
movq %r14, %rdi
callq 0x20d0
movq %rbx, (%r14,%rax)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2370
movq %r14, %rdi
callq 0x20d0
movw $0x29, (%r14,%rax)
xorl %edi, %edi
movq %r13, %rsi
callq 0x2310
movq %rax, %rbp
testq %rax, %rax
jne 0x2f09
movq %r14, %rdi
callq 0x20d0
movw $0xa29, (%r14,%rax)     # imm = 0xA29
movb $0x0, 0x2(%r14,%rax)
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x2f96
movq %r14, %rdi
callq 0x20d0
movups 0x3389(%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, 0x6273(%rip)      # 0x9210
jl 0x2ff1
leaq 0x394f(%rip), %rdi      # 0x68f5
callq 0x22f0
movq %r14, %rdi
callq 0x22f0
movq (%rsp), %rax
movzbl 0x32(%rax), %r12d
imulq $0x3c, %r12, %rbx
movq 0x8(%rsp), %rdi
callq 0x20d0
leaq (%rbx,%rax), %r15
addq $0x1d, %r15
movq %r14, %rdi
callq 0x20d0
leaq 0x3329(%rip), %rdi      # 0x630a
movl %r12d, %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 0x3026
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x246e
movq %rsp, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2400