// File: /workspace/github_c_makefile_star2_O1/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 $0x188, %rsp            # imm = 0x188
movq %rsi, 0x48(%rsp)
movq %rdi, %r15
movq %rdx, 0x10(%rsp)
movq 0x620f(%rip), %rdi      # 0x9268
testq %rdi, %rdi
movq %r15, 0x28(%rsp)
je 0x3178
leaq 0x3307(%rip), %rsi      # 0x6375
callq 0x2220
testq %rax, %rax
je 0x3173
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rax, %rdx
callq 0x2320
movq %rax, %rdi
callq 0x2300
movl %eax, %r12d
cmpl $0x3, 0x616a(%rip)      # 0x9210
jl 0x30c0
movq 0x61b9(%rip), %rsi      # 0x9268
leaq 0x32e2(%rip), %rdi      # 0x6398
movl %r12d, %edx
xorl %eax, %eax
callq 0x2050
leal 0x1(,%r12,2), %eax
movslq %eax, %rdi
callq 0x2280
testq %rax, %rax
je 0x383c
movq %rax, %r14
leaq 0x80(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x2320
testl %r12d, %r12d
movq %r12, 0x20(%rsp)
jle 0x315c
leal (%r12,%r12), %eax
movl %eax, %ebp
xorl %r15d, %r15d
leaq 0x32e7(%rip), %r12      # 0x63f2
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x2310
movq %rax, %rdi
callq 0x2300
movb %al, (%r14,%r15)
xorl %edi, %edi
movq %r12, %rsi
callq 0x2310
movq %rax, %rdi
callq 0x2300
movb %al, 0x1(%r14,%r15)
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x2320
addq $0x2, %r15
cmpq %rbp, %r15
jb 0x3113
jmp 0x315f
xorl %r15d, %r15d
movb $0x0, (%r14,%r15)
cmpl $0x0, 0x20(%rsp)
setg %al
movq %rax, 0x40(%rsp)
jmp 0x3184
callq 0x24ea
xorl %r14d, %r14d
movq $0x0, 0x40(%rsp)
movq %r14, 0x50(%rsp)
cmpl $0x3, 0x6080(%rip)      # 0x9210
jl 0x319e
leaq 0x3772(%rip), %rdi      # 0x690b
callq 0x22f0
movq 0x10(%rsp), %rax
movzbl 0x32(%rax), %ecx
testq %rcx, %rcx
je 0x31f8
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 0x31ea
cmpl $0x4d, %edi
je 0x31ea
cmpl $0x54, %edi
jne 0x31dd
movl $0x17, %esi
jmp 0x31ea
movzbl 0xc(%rax,%rdx), %esi
leaq 0x3(,%rsi,2), %rsi
addq %rsi, %r14
addq $0xf, %rdx
cmpq %rdx, %rcx
jne 0x31bd
jmp 0x31fb
xorl %r14d, %r14d
movslq 0x6022(%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 0x382d
movq %rax, %r15
cmpl $0x0, 0x5ffd(%rip)      # 0x9230
je 0x3263
testl %ebx, %ebx
je 0x328d
movq %r15, %rdi
callq 0x20d0
movabsq $0x4c554e2c4c4c554e, %rcx # imm = 0x4C554E2C4C4C554E
movq %rcx, (%r15,%rax)
movl $0x2c4c4c, 0x7(%r15,%rax) # imm = 0x2C4C4C
movq $0x0, 0x38(%rsp)
jmp 0x3282
leaq 0x31c3(%rip), %rsi      # 0x642d
movq $0x0, 0x38(%rsp)
movq %r15, %rdi
movq 0x48(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
movq $0x0, 0x30(%rsp)
jmp 0x32e8
leaq 0x31be(%rip), %rdi      # 0x6452
leaq 0x31bc(%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 0x319e(%rip), %rsi      # 0x645c
movl %eax, %edi
callq 0x21f0
movq %rbx, 0x38(%rsp)
testq %rbx, %rbx
je 0x37fc
movq %rax, %rcx
testq %rax, %rax
je 0x37fc
movq %rcx, 0x30(%rsp)
movb $0x0, (%r15)
movq %r15, %rdi
callq 0x20d0
movq %rax, 0x68(%rsp)
movq 0x10(%rsp), %rdi
callq 0x51a5
cmpq $-0x1, %rax
je 0x37a9
movq %rax, %rbp
movq 0x10(%rsp), %rdi
cmpq $0x0, 0x20(%rdi)
je 0x3708
movslq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x60(%rsp)
movq %rbp, %rsi
movq %r12, %rdx
callq 0x48ea
cmpl $0x20, %eax
jne 0x36f6
movq %r12, 0x78(%rsp)
movq %r15, 0x20(%rsp)
movq 0x58(%rsp), %rax
leaq (%r15,%rax), %r13
movq 0x10(%rsp), %rax
cmpb $0x0, 0x32(%rax)
je 0x35f5
xorl %ecx, %ecx
xorl %r12d, %r12d
movq %rcx, %rax
shlq $0x5, %rax
cmpb $0x0, (%rbp,%rax)
je 0x35ca
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, %r15d        # imm = 0x801091
shrl %cl, %r15d
movq 0x20(%rsp), %rcx
movq 0x58(%rsp), %rdx
addq %rdx, %rcx
cmpq %rcx, %r13
je 0x33c7
movb $0x2c, (%r13)
incq %r13
incq %r12
orb %al, %sil
cmpl $0x0, 0x5e5f(%rip)      # 0x9230
sete %cl
movl %esi, %eax
notb %al
testb %cl, %al
movb %sil, 0xf(%rsp)
jne 0x33ec
movb $0x27, (%r13)
incq %r13
incq %r12
cmpl $0x0, 0x5e39(%rip)      # 0x922c
sete %cl
orb %cl, %al
testb $0x1, %al
jne 0x3441
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 0x342a
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0x3411
movq %rbx, %rdi
callq 0x20d0
leaq 0x1(%rax), %rdx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2340
cmpb $0x0, 0x40(%rsp)
je 0x349f
movq 0x18(%rsp), %rax
cmpb $0x43, 0xb(%rax)
jne 0x349f
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rbx
movb (%rbx), %al
testb %al, %al
je 0x349f
movl $0x1, %ebp
movsbl %al, %esi
movq 0x50(%rsp), %rdi
callq 0x20b0
testq %rax, %rax
je 0x3489
movl %eax, %ecx
subl 0x50(%rsp), %ecx
testb $0x1, %cl
jne 0x3489
movb 0x1(%rax), %al
movb %al, (%rbx)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rbx
movb (%rbx,%rbp), %al
addq %rbp, %rbx
incq %rbp
testb %al, %al
jne 0x3467
cmpl $0x0, 0x5d6e(%rip)      # 0x9214
je 0x34b6
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x55a4
cmpl $0x0, 0x5d5b(%rip)      # 0x9218
movq 0x18(%rsp), %rax
je 0x34d2
movq 0x18(%rax), %rdi
callq 0x55d9
movq 0x18(%rsp), %rax
andb %r15b, 0xe(%rsp)
movq 0x18(%rax), %rbp
movq 0x68(%rsp), %r15
movzbl (%rbp), %eax
cmpl $0x27, %eax
je 0x34f4
cmpl $0x5c, %eax
je 0x34f4
testl %eax, %eax
jne 0x34ff
jmp 0x3559
movb $0x5c, (%r13)
incq %r13
incq %r12
movb (%rbp), %al
movb %al, (%r13)
incq %r12
cmpq %r14, %r12
jae 0x3513
incq %r13
jmp 0x3554
leaq (%r12,%r15), %r14
movq %rbp, %rdi
callq 0x20d0
movq %rax, %rbx
addq %r14, %rbx
leal 0x10(%rbx), %eax
movslq %eax, %rsi
movq 0x20(%rsp), %rdi
callq 0x2350
testq %rax, %rax
je 0x381e
movq %rax, 0x20(%rsp)
movq %rax, %r13
addq 0x58(%rsp), %r13
addq %r12, %r13
subl %r15d, %ebx
movslq %ebx, %r14
incq %rbp
jmp 0x34e0
cmpl $0x0, 0x5cd0(%rip)      # 0x9230
setne %al
orb %al, 0xe(%rsp)
je 0x35a1
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
cmpb $0x0, (%rax)
movq 0x60(%rsp), %rbp
movq 0x70(%rsp), %rcx
jne 0x35b6
cmpl $0x0, 0x5ca0(%rip)      # 0x9228
je 0x35e1
movb $0x0, 0x4(%r13)
movl $0x4c4c554e, (%r13)     # imm = 0x4C4C554E
addq $0x4, %r13
addq $0x4, %r12
jmp 0x35b6
movb $0x27, (%r13)
incq %r13
incq %r12
movq 0x60(%rsp), %rbp
movq 0x70(%rsp), %rcx
cmpl $0x0, 0x5c73(%rip)      # 0x9230
je 0x35ca
movb $0x27, (%r13)
incq %r13
incq %r12
incq %rcx
movq 0x10(%rsp), %rax
movzbl 0x32(%rax), %eax
cmpq %rax, %rcx
jb 0x3369
jmp 0x35f5
testb $0x1, 0xf(%rsp)
jne 0x35b6
movb $0x30, (%r13)
incq %r13
incq %r12
jmp 0x35b6
cmpl $0x0, 0x5c34(%rip)      # 0x9230
movq 0x78(%rsp), %r12
jne 0x360b
movb $0x29, (%r13)
incq %r13
movb $0x0, (%r13)
imull $0xc28f5c29, %r12d, %eax # imm = 0xC28F5C29
rorl $0x2, %eax
cmpl $0x28f5c28, %eax        # imm = 0x28F5C28
ja 0x363b
cmpl $0x3, 0x5be8(%rip)      # 0x9210
jne 0x363b
leaq 0x2e9f(%rip), %rdi      # 0x64d0
movl %r12d, %esi
xorl %eax, %eax
callq 0x2050
cmpl $0x4, 0x5bce(%rip)      # 0x9210
movq 0x20(%rsp), %r15
jl 0x365d
leaq 0x2e95(%rip), %rdi      # 0x64e5
movl %r12d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0x5bcc(%rip)      # 0x9230
je 0x367d
cmpl $0x0, 0x5bb7(%rip)      # 0x9224
je 0x36e0
movq 0x30(%rsp), %rdi
leaq 0x2e9f(%rip), %rsi      # 0x651a
jmp 0x36ec
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x2130
cmpl $-0x1, %eax
jne 0x36f6
movq 0x5942(%rip), %rbx      # 0x8fd8
movq (%rbx), %rdi
leaq 0x2e55(%rip), %rsi      # 0x64f5
movl %r12d, %edx
xorl %eax, %eax
callq 0x22c0
movq (%rbx), %rbx
movq 0x28(%rsp), %rdi
callq 0x20c0
movq %rbx, %rdi
leaq 0x2c9f(%rip), %rsi      # 0x6360
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c0
cmpl $0x2, 0x5b3e(%rip)      # 0x9210
jl 0x36f6
movq 0x58fd(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
jmp 0x36e5
movq 0x30(%rsp), %rdi
leaq 0x2e05(%rip), %rsi      # 0x64f1
movq %r15, %rdx
xorl %eax, %eax
callq 0x22c0
incq %r12
movq 0x10(%rsp), %rdi
cmpq %r12, 0x20(%rdi)
ja 0x332e
movq %rbp, %rsi
callq 0x52e8
movq %r15, %rdi
callq 0x2230
cmpb $0x0, 0x40(%rsp)
je 0x3729
movq 0x50(%rsp), %rdi
callq 0x2230
cmpl $0x0, 0x5b00(%rip)      # 0x9230
movq 0x28(%rsp), %rbx
movq 0x38(%rsp), %r14
je 0x3797
movq 0x30(%rsp), %rdi
callq 0x21c0
leaq 0x2ddb(%rip), %rsi      # 0x6528
leaq 0x80(%rsp), %rdi
movq %r14, %rdx
movq 0x48(%rsp), %rcx
xorl %eax, %eax
callq 0x2080
cmpl $0x2, 0x5aa5(%rip)      # 0x9210
jge 0x37b0
leaq 0x80(%rsp), %rsi
movq %rbx, %rdi
callq 0x2130
cmpl $-0x1, %eax
je 0x37d2
movq %r14, %rdi
callq 0x20f0
cmpl $-0x1, %eax
je 0x37df
movq %r14, %rdi
callq 0x2230
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2580
jmp 0x3797
movq 0x5821(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x2d30(%rip), %rsi      # 0x64f1
leaq 0x80(%rsp), %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x376d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2537
jmp 0x3782
movq 0x57f2(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x2dc3(%rip), %rsi      # 0x65b3
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x378f
movq 0x57d5(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x2c52(%rip), %rsi      # 0x645f
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x3797
leaq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x2509
leaq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x25a2
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x24ac