// File: /workspace/github_c_makefile_star2_O3/sonicse[P]dbf2mysql/dbf2mysql.c
// Function: do_inserts
void do_inserts(MYSQL *SQLsock, char *table, dbhead *dbh) {
    int result, i, j, nc = 0, h;
    field *fields;
    char *query, *vpos, *pos;
    char str[257], *cvt = NULL, *s;
    u_long val_len = 0;
    char *datafile = NULL;
    FILE *fconv, *tempfile = NULL;
    int quote_field;
    u_long val_used;
    int base_pos;

    /* Max Number of characters that can be stored before checking buffer size */
#define VAL_EXTRA 16

    if (convert != NULL) /* If specified conversion table */ {
        if ((fconv = fopen(convert, "rt")) == NULL)
            fprintf(stderr, "Cannot open convert file '%s'.\n", convert);
        else {
            nc = atoi(fgets(str, 256, fconv));
            if (verbose > 2)
                printf("Using conversion table '%s' with %d entries\n", convert, nc);
            if ((cvt = (char *) malloc(nc * 2 + 1)) == NULL) {
                fprintf(stderr, "Memory allocation error in do_inserts (cvt)\n");
                mysql_close(SQLsock);
                dbf_close(&dbh);
                exit(1);
            }
            for (i = 0, fgets(str, 256, fconv); (i < nc * 2) && (str != NULL); i++) {
                cvt[i++] = atoi(strtok(str, " \t"));
                cvt[i] = atoi(strtok(NULL, " \t"));
                fgets(str, 256, fconv);
            }
            cvt[i] = '\0';
        }
    }

    if (verbose > 2) {
        printf("Inserting records\n");
    }

    for (i = 0; i < dbh->db_nfields; i++) {
        switch (dbh->db_fields[i].db_type) {
            case 'M':
            case 'G':
                val_len += 2048;
                break;
            case 'T':
                val_len += 23;
                break;
            default:
                val_len += dbh->db_fields[i].db_flen * 2 + 3;
        }
    }

    if (!(query = (char *) malloc((express * 10) + 26 + strlen(table) + val_len + VAL_EXTRA))) {
        fprintf(stderr,
                "Memory allocation error in function do_inserts (query)\n");
        mysql_close(SQLsock);
        dbf_close(&dbh);
        exit(1);
    }
    if (!quick) {
        sprintf(query, "INSERT INTO `%s` VALUES (", table);
    } else
        if (express)
        strcat(query, "NULL,NULL,");
    else /* if specified -q create file for 'LOAD DATA' */ {
        datafile = tempnam("/tmp", "d2my");
        tempfile = fdopen(open(datafile, O_WRONLY | O_CREAT | O_EXCL,
                0600), "wt");
        if (tempfile == NULL || datafile == NULL) {
            fprintf(stderr, "Cannot open file '%s' for writing\n", datafile);
            return;
        }
        query[0] = '\0';
    }
    base_pos = strlen(query);

    if ((fields = dbf_build_record(dbh)) == (field *) DBF_ERROR) {
        fprintf(stderr, "Couldn't create memory structure for record\n");
        return;
    }

    for (i = 0; i < dbh->db_records; i++) {
        result = dbf_get_record(dbh, fields, i);
        if (result == DBF_VALID) {
            vpos = query + base_pos;
            val_used = 0;
            for (h = 0; h < dbh->db_nfields; h++) {
                /* if length of fieldname==0, skip it */
                if (!strlen(fields[h].db_name))
                    continue;
                if (fields[h].db_type == 'N' ||
                        fields[h].db_type == 'F' ||
                        fields[h].db_type == 'B' ||
                        fields[h].db_type == 'Y' ||
                        fields[h].db_type == 'I')
                    quote_field = 0;
                else
                    quote_field = 1;

                if (vpos != query + base_pos) {
                    *vpos++ = ',';
                    val_used++;
                }

                if (quick || quote_field) {
                    *vpos++ = '\'';
                    val_used++;
                }

                if (trim && quote_field) /* trim leading spaces */ {
                    for (pos = fields[h].db_contents; isspace(*pos) && (*pos != '\0');
                            pos++);
                    memmove(fields[h].db_contents, pos, strlen(pos) + 1);
                }
                if ((nc > 0) && (fields[h].db_type == 'C')) {
                    for (j = 0; fields[h].db_contents[j] != '\0'; j++) {
                        if ((s = strchr(cvt, fields[h].db_contents[j])) != NULL)
                            if ((s - cvt) % 2 == 0)
                                fields[h].db_contents[j] = s[1];
                    }
                }
                if (upper)
                    strtoupper(fields[h].db_contents);
                if (lower)
                    strtolower(fields[h].db_contents);

                for (pos = fields[h].db_contents; *pos; pos++) {
                    if (*pos == '\\' || *pos == '\'') {
                        *vpos++ = '\\';
                        val_used++;
                    }
                    *vpos++ = *pos;
                    val_used++;
                    if (val_used >= val_len) {
                        //			int	newsiz = (((val_used + strlen(pos) + VAL_EXTRA + base_pos + 4095)/4096)+1)*4096-16;
                        int newsiz = base_pos + val_used + strlen(pos) + VAL_EXTRA;
                        void *tmpptr = realloc(query, newsiz);
                        if (tmpptr == NULL) {
                            perror("out of memory in do_inserts");
                            mysql_close(SQLsock);
                            dbf_close(&dbh);
                            exit(1);
                        }
                        query = tmpptr;
                        vpos = query + base_pos + val_used;
                        val_len = newsiz - base_pos - VAL_EXTRA;
                    }
                }
                if (quote_field && !quick) {
                    *vpos++ = '\'';
                    val_used++;
                } else {
                    if (!fields[h].db_contents[0]) { /* Numeric field is null */
                        if (null_fields) {
                            strcpy(vpos, "NULL");
                            vpos += 4;
                            val_used += 4;
                        } else {
                            if (!quote_field) {
                                *vpos++ = '0';
                                val_used++;
                            }
                        }
                    }
                }
                if (quick) {
                    *vpos++ = '\'';
                    val_used++;
                }
            }
            if (!quick) {
                vpos[0] = ')'; /* End of values */
                vpos[1] = 0; /* End of query */
            } else
                vpos[0] = '\0';
            if ((verbose == 3) && ((i % 100) == 0)) {
                printf("Inserting record %d\n", i);
            }
            if (verbose > 3) {
                printf("Record %4d: %s\n", i, query);
            }

            if (!quick) {
                if (mysql_query(SQLsock, query) == -1) {
                    fprintf(stderr,
                            "Error sending INSERT in record %04d\n", i);
                    fprintf(stderr,
                            "Detailed report: %s\n",
                            mysql_error(SQLsock));
                    if (verbose > 1) {
                        fprintf(stderr, "%s\n", query);
                    }
                }
            } else {
                if (express)
                    fprintf(tempfile, "NULL,NULL,%s\n", query);
                else
                    fprintf(tempfile, "%s\n", query);
            }
        }
    }

    dbf_free_record(dbh, fields);

    free(query);
    if (nc > 0)
        free(cvt);

    if (quick) {
        fclose(tempfile);
        query = str;
        sprintf(query, "LOAD DATA LOCAL INFILE '%s' REPLACE INTO table %s fields terminated by ',' enclosed by ''''",
                datafile, table);
        if (verbose > 1) {
            fprintf(stderr, "%s\n", query);
        }
        if (mysql_query(SQLsock, query) == -1) {
            fprintf(stderr,
                    "Error sending LOAD DATA INFILE from file '%s'\n", datafile);
            fprintf(stderr,
                    "Detailed report: %s\n",
                    mysql_error(SQLsock));
        }
        if (unlink(datafile) == -1) {
            fprintf(stderr, "Error while removing temporary file '%s'.\n", datafile);
        }
        free(datafile);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, 0x48(%rsp)
movq %rdi, %r15
movq %rdx, 0x10(%rsp)
movq 0x61fd(%rip), %rdi      # 0x9268
testq %rdi, %rdi
movq %r15, 0x20(%rsp)
je 0x3182
leaq 0x32f5(%rip), %rsi      # 0x6375
callq 0x2220
testq %rax, %rax
je 0x317d
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rax, %rdx
callq 0x2320
movq %rax, %rdi
callq 0x2300
movl %eax, %r14d
cmpl $0x3, 0x6158(%rip)      # 0x9210
jl 0x30d2
movq 0x61a7(%rip), %rsi      # 0x9268
leaq 0x32d0(%rip), %rdi      # 0x6398
movl %r14d, %edx
xorl %eax, %eax
callq 0x2050
leal 0x1(,%r14,2), %eax
movslq %eax, %rdi
callq 0x2280
testq %rax, %rax
je 0x3877
movq %rax, %rbp
leaq 0x90(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x2320
testl %r14d, %r14d
setg %al
movq %rax, 0x38(%rsp)
jle 0x3172
addl %r14d, %r14d
movl %r14d, %r13d
xorl %r15d, %r15d
leaq 0x32d2(%rip), %r14      # 0x63f2
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2310
movq %rax, %rdi
callq 0x2300
movb %al, (%rbp,%r15)
xorl %edi, %edi
movq %r14, %rsi
callq 0x2310
movq %rax, %rdi
callq 0x2300
movb %al, 0x1(%rbp,%r15)
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x2320
addq $0x2, %r15
cmpq %r13, %r15
jb 0x3128
jmp 0x3175
xorl %r15d, %r15d
movb $0x0, (%rbp,%r15)
jmp 0x318d
callq 0x24ea
xorl %ebp, %ebp
movq $0x0, 0x38(%rsp)
movq %rbp, 0x78(%rsp)
cmpl $0x3, 0x6077(%rip)      # 0x9210
jl 0x31a7
leaq 0x3769(%rip), %rdi      # 0x690b
callq 0x22f0
movq 0x10(%rsp), %rax
movzbl 0x32(%rax), %ecx
testq %rcx, %rcx
je 0x3201
movq 0x40(%rax), %rax
leaq (%rcx,%rcx,4), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %r14d, %r14d
movzbl 0xb(%rax,%rdx), %edi
movl $0x800, %esi            # imm = 0x800
cmpl $0x47, %edi
je 0x31f3
cmpl $0x4d, %edi
je 0x31f3
cmpl $0x54, %edi
jne 0x31e6
movl $0x17, %esi
jmp 0x31f3
movzbl 0xc(%rax,%rdx), %esi
leaq 0x3(,%rsi,2), %rsi
addq %rsi, %r14
addq $0xf, %rdx
cmpq %rdx, %rcx
jne 0x31c6
jmp 0x3204
xorl %r14d, %r14d
movslq 0x6019(%rip), %rbx      # 0x9224
leaq (%rbx,%rbx,4), %r15
movq 0x48(%rsp), %rdi
callq 0x20d0
addq %r14, %rax
leaq (%rax,%r15,2), %rdi
addq $0x2a, %rdi
callq 0x2280
testq %rax, %rax
je 0x3868
movq %rax, %r15
cmpl $0x0, 0x5ff4(%rip)      # 0x9230
je 0x326c
testl %ebx, %ebx
je 0x3296
movq %r15, %rdi
callq 0x20d0
movabsq $0x4c554e2c4c4c554e, %rcx # imm = 0x4C554E2C4C4C554E
movq %rcx, (%r15,%rax)
movl $0x2c4c4c, 0x7(%r15,%rax) # imm = 0x2C4C4C
movq $0x0, 0x30(%rsp)
jmp 0x328b
leaq 0x31ba(%rip), %rsi      # 0x642d
movq $0x0, 0x30(%rsp)
movq %r15, %rdi
movq 0x48(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
movq $0x0, 0x28(%rsp)
jmp 0x32f1
leaq 0x31b5(%rip), %rdi      # 0x6452
leaq 0x31b3(%rip), %rsi      # 0x6457
callq 0x2360
movq %rax, %rbx
movq %rax, %rdi
movl $0xc1, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x2150
leaq 0x3195(%rip), %rsi      # 0x645c
movl %eax, %edi
callq 0x21f0
movq %rbx, 0x30(%rsp)
testq %rbx, %rbx
je 0x3837
movq %rax, %rcx
testq %rax, %rax
je 0x3837
movq %rcx, 0x28(%rsp)
movb $0x0, (%r15)
movq %r15, %rdi
callq 0x20d0
movq %rax, 0x68(%rsp)
movq 0x10(%rsp), %rdi
callq 0x512c
cmpq $-0x1, %rax
je 0x37e4
movq %rax, %rbp
movq 0x10(%rsp), %rdi
cmpq $0x0, 0x20(%rdi)
je 0x3743
movslq 0x68(%rsp), %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x60(%rsp)
movq %rbp, %rsi
movq %r12, %rdx
callq 0x488f
cmpl $0x20, %eax
jne 0x3731
movq %r12, 0x80(%rsp)
movq %r15, 0x40(%rsp)
movq 0x50(%rsp), %rax
leaq (%r15,%rax), %r13
movq 0x10(%rsp), %rax
cmpb $0x0, 0x32(%rax)
je 0x3620
xorl %ecx, %ecx
xorl %r12d, %r12d
movq %rcx, %rax
shlq $0x5, %rax
cmpb $0x0, (%rbp,%rax)
je 0x35f5
movq %rcx, 0x70(%rsp)
addq %rbp, %rax
movq %rax, 0x18(%rsp)
movb 0xb(%rax), %cl
addb $-0x42, %cl
cmpb $0x18, %cl
setb 0xe(%rsp)
setae %sil
movl $0x7fef6e, %eax         # imm = 0x7FEF6E
shrl %cl, %eax
movl $0x801091, %edx         # imm = 0x801091
shrl %cl, %edx
movl %edx, 0x5c(%rsp)
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %rdx
addq %rdx, %rcx
cmpq %rcx, %r13
je 0x33d5
movb $0x2c, (%r13)
incq %r13
incq %r12
orb %al, %sil
cmpl $0x0, 0x5e51(%rip)      # 0x9230
sete %cl
movl %esi, %eax
notb %al
testb %cl, %al
movb %sil, 0xf(%rsp)
jne 0x33fa
movb $0x27, (%r13)
incq %r13
incq %r12
cmpl $0x0, 0x5e2b(%rip)      # 0x922c
sete %cl
orb %cl, %al
testb $0x1, %al
jne 0x344f
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rbp
callq 0x2200
movq (%rax), %rax
leaq -0x1(%rbp), %rbx
movsbq 0x1(%rbx), %rcx
incq %rbx
testq %rcx, %rcx
je 0x3438
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0x341f
movq %rbx, %rdi
callq 0x20d0
leaq 0x1(%rax), %rdx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2340
movq %r14, 0x88(%rsp)
cmpb $0x0, 0x38(%rsp)
je 0x34bf
movq 0x18(%rsp), %rax
cmpb $0x43, 0xb(%rax)
jne 0x34bf
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, %r15
movb (%rax), %al
testb %al, %al
je 0x34bf
movl $0x1, %ebp
movq %r15, %rbx
movsbl %al, %esi
movq 0x78(%rsp), %r14
movq %r14, %rdi
callq 0x20b0
testq %rax, %rax
je 0x34b0
movl %eax, %ecx
subl %r14d, %ecx
testb $0x1, %cl
jne 0x34b0
movb 0x1(%rax), %al
movb %al, (%rbx)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r15
leaq (%r15,%rbp), %rbx
movb (%r15,%rbp), %al
incq %rbp
testb %al, %al
jne 0x3483
cmpl $0x0, 0x5d4e(%rip)      # 0x9214
je 0x34d6
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x5534
cmpl $0x0, 0x5d3b(%rip)      # 0x9218
movq 0x88(%rsp), %r14
movq 0x18(%rsp), %rax
je 0x34fa
movq 0x18(%rax), %rdi
callq 0x5569
movq 0x18(%rsp), %rax
movl 0x5c(%rsp), %ecx
andb %cl, 0xe(%rsp)
movq 0x18(%rax), %rbp
movq 0x68(%rsp), %r15
movzbl (%rbp), %eax
cmpl $0x27, %eax
je 0x351f
cmpl $0x5c, %eax
je 0x351f
testl %eax, %eax
jne 0x352d
jmp 0x3584
movb $0x5c, (%r13)
incq %r13
incq %r12
movb (%rbp), %al
movb %al, (%r13)
incq %r12
cmpq %r14, %r12
jae 0x353e
incq %r13
jmp 0x357f
leaq (%r12,%r15), %r14
movq %rbp, %rdi
callq 0x20d0
movq %rax, %rbx
addq %r14, %rbx
leal 0x10(%rbx), %eax
movslq %eax, %rsi
movq 0x40(%rsp), %rdi
callq 0x2350
testq %rax, %rax
je 0x3859
movq %rax, 0x40(%rsp)
movq %rax, %r13
addq 0x50(%rsp), %r13
addq %r12, %r13
subl %r15d, %ebx
movslq %ebx, %r14
incq %rbp
jmp 0x350b
cmpl $0x0, 0x5ca5(%rip)      # 0x9230
setne %al
orb %al, 0xe(%rsp)
je 0x35cc
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
cmpb $0x0, (%rax)
movq 0x60(%rsp), %rbp
movq 0x70(%rsp), %rcx
jne 0x35e1
cmpl $0x0, 0x5c75(%rip)      # 0x9228
je 0x360c
movb $0x0, 0x4(%r13)
movl $0x4c4c554e, (%r13)     # imm = 0x4C4C554E
addq $0x4, %r13
addq $0x4, %r12
jmp 0x35e1
movb $0x27, (%r13)
incq %r13
incq %r12
movq 0x60(%rsp), %rbp
movq 0x70(%rsp), %rcx
cmpl $0x0, 0x5c48(%rip)      # 0x9230
je 0x35f5
movb $0x27, (%r13)
incq %r13
incq %r12
incq %rcx
movq 0x10(%rsp), %rax
movzbl 0x32(%rax), %eax
cmpq %rax, %rcx
jb 0x3375
jmp 0x3620
testb $0x1, 0xf(%rsp)
jne 0x35e1
movb $0x30, (%r13)
incq %r13
incq %r12
jmp 0x35e1
cmpl $0x0, 0x5c09(%rip)      # 0x9230
movq 0x80(%rsp), %r12
je 0x3635
xorl %ecx, %ecx
jmp 0x363c
movb $0x0, 0x1(%r13)
movb $0x29, %cl
movl 0x5bce(%rip), %eax      # 0x9210
cmpl $0x3, %eax
setne %dl
imull $0xc28f5c29, %r12d, %esi # imm = 0xC28F5C29
rorl $0x2, %esi
movb %cl, (%r13)
cmpl $0x28f5c29, %esi        # imm = 0x28F5C29
setae %cl
orb %dl, %cl
jne 0x367a
leaq 0x2e66(%rip), %rdi      # 0x64d0
movl %r12d, %esi
xorl %eax, %eax
callq 0x2050
movl 0x5b96(%rip), %eax      # 0x9210
cmpl $0x4, %eax
movq 0x40(%rsp), %r15
jl 0x3698
leaq 0x2e5a(%rip), %rdi      # 0x64e5
movl %r12d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0x5b91(%rip)      # 0x9230
je 0x36b8
cmpl $0x0, 0x5b7c(%rip)      # 0x9224
je 0x371b
movq 0x28(%rsp), %rdi
leaq 0x2e64(%rip), %rsi      # 0x651a
jmp 0x3727
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x2130
cmpl $-0x1, %eax
jne 0x3731
movq 0x5907(%rip), %rbx      # 0x8fd8
movq (%rbx), %rdi
leaq 0x2e1a(%rip), %rsi      # 0x64f5
movl %r12d, %edx
xorl %eax, %eax
callq 0x22c0
movq (%rbx), %rbx
movq 0x20(%rsp), %rdi
callq 0x20c0
movq %rbx, %rdi
leaq 0x2c64(%rip), %rsi      # 0x6360
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
cmpl $0x2, 0x5b03(%rip)      # 0x9210
jl 0x3731
movq 0x58c2(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
jmp 0x3720
movq 0x28(%rsp), %rdi
leaq 0x2dca(%rip), %rsi      # 0x64f1
movq %r15, %rdx
xorl %eax, %eax
callq 0x22c0
incq %r12
movq 0x10(%rsp), %rdi
cmpq %r12, 0x20(%rdi)
ja 0x3337
movq %rbp, %rsi
callq 0x527b
movq %r15, %rdi
callq 0x2230
cmpb $0x0, 0x38(%rsp)
je 0x3764
movq 0x78(%rsp), %rdi
callq 0x2230
cmpl $0x0, 0x5ac5(%rip)      # 0x9230
movq 0x20(%rsp), %rbx
movq 0x30(%rsp), %r14
je 0x37d2
movq 0x28(%rsp), %rdi
callq 0x21c0
leaq 0x2da0(%rip), %rsi      # 0x6528
leaq 0x90(%rsp), %rdi
movq %r14, %rdx
movq 0x48(%rsp), %rcx
xorl %eax, %eax
callq 0x2080
cmpl $0x2, 0x5a6a(%rip)      # 0x9210
jge 0x37eb
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0x2130
cmpl $-0x1, %eax
je 0x380d
movq %r14, %rdi
callq 0x20f0
cmpl $-0x1, %eax
je 0x381a
movq %r14, %rdi
callq 0x2230
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2580
jmp 0x37d2
movq 0x57e6(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x2cf5(%rip), %rsi      # 0x64f1
leaq 0x90(%rsp), %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x37a8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2537
jmp 0x37bd
movq 0x57b7(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x2d88(%rip), %rsi      # 0x65b3
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x37ca
movq 0x579a(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x2c17(%rip), %rsi      # 0x645f
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x37d2
leaq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x2509
leaq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x25a2
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x24ac