// File: /workspace/github_c_makefile_star2_O1/sonicse[P]dbf2mysql/dbf.c
// Function: dbf_get_record
int dbf_get_record(dbhead *dbh, field *fields,  u_long rec)
{
    int     	t, i;
    u_char  	*dbffield;
    char	*end, *sp;
    double	dblval;
    long long	int8val;
    long	int4val;
    u_int	hh, mm, ss, month, day, year;
    u_int	daynum, leap_day, day_of_year, days_in_year;
    u_char 	*days_per_month;
    u_long	blknum;

/* calculate at which offset we have to read. *DON'T* forget the
   0x0D which seperates field-descriptions from records!

   Note (april 5 1996): This turns out to be included in db_hlen
*/
    i = dbh->db_hlen + (rec * dbh->db_rlen);

    if (lseek(dbh->db_fd, i, SEEK_SET) == -1) {
	lseek(dbh->db_fd, 0, SEEK_SET);
	dbh->db_offset = 0;
	return DBF_ERROR;
    }

    dbh->db_offset 	= i;
    dbh->db_currec	= rec;

    read(dbh->db_fd, dbh->db_buff, dbh->db_rlen);

    if (dbh->db_buff[0] == DBF_DELETED) {
	return DBF_DELETED;
    }

    dbffield = &dbh->db_buff[1];
    for (t = 0; t < dbh->db_nfields; t++)
    {
        switch(fields[t].db_type) {
          case 'C':		/* Character data */
	    end = (char *)&dbffield[fields[t].db_flen - 1];
	    i = fields[t].db_flen;

            //2011-03-23 AA: Skip only 0-21 code character
	    while (( i > 0) && ((*end < 0x21) && (*end >= 0))) {
		end--;
		i--;
	    }
            strncpy(fields[t].db_contents, (char *)dbffield, i);
	    fields[t].db_contents[i] = '\0';
	    break;

	  case 'B':		/* Binary data - 8 byte field - ???Endian Alert??? */
	    memcpy(&dblval, dbffield, sizeof(dblval));
	    snprintf(fields[t].db_contents, fields[t].db_blen,  "%.*f", fields[t].db_dec, dblval);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    break;

          case 'M':		/* Memo field */
          case 'G':		/* OLE field */
	    switch(dbh->db_memo)
	    {
	      case DBF_MTYPE_FPT:
		blknum = get_long(dbffield);
		break;
	      case DBF_MTYPE_DBT3:
	      case DBF_MTYPE_DBT4:
		strncpy(fields[t].db_contents, (char *)dbffield, fields[t].db_flen);
		fields[t].db_contents[fields[t].db_flen] = '\0';
		blknum = atol(fields[t].db_contents);
		memset(fields[t].db_contents, 0, fields[t].db_flen);
		break;
	      default:
		blknum = 0;
	    }
	    if (blknum != 0)
		dbf_get_memo_data(dbh, &fields[t], blknum);
	    else
		memset(fields[t].db_contents, '\0', fields[t].db_blen);
	    break;

	  case 'Y':		/* Currency 8 byte field - ???Endian Alert??? */
	    memcpy(&int8val, dbffield, sizeof(int8val));
	    snprintf(fields[t].db_contents, fields[t].db_blen,  "%0*lld",
		     fields[t].db_dec+1, int8val);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    end = fields[t].db_contents + strlen(fields[t].db_contents) + 1;
	    *end-- = '\0';
	    sp = end - 1;
	    for (i=0; i<fields[t].db_dec; i++)
		*end-- = *sp--;
	    *++sp = '.';
	    break;

	  case 'T':		/* Time 8 byte field - ???Endian Alert??? */
	    memcpy(&daynum, &dbffield[0], sizeof(daynum));
	    daynum -= 1721060;	/* Magic number for 0000-01-01 */
	    if (daynum <= 365 || daynum >= 3652500)
	    {			/* Only allow 0001-01-01 through 9999-12-31 */
	      year = month = day = 0;
	    }
	    else
	    {
	      year = daynum * 100 / 36525;
	      day_of_year = (daynum - year * 365) - (year-1) / 4 +
		  (((year - 1) / 100 + 1) * 3) / 4;
	      while (day_of_year >
		     (days_in_year =
		      ((year & 3) == 0 &&
		       (year % 100 ||
			(year % 400 == 0 && year)) ? 366 : 365)))
	      {
		day_of_year -= days_in_year;
		year++;
	      }
	      leap_day = 0;
	      if (days_in_year == 366)
	      {
		if (day_of_year > 31 + 28)
		{
		  day_of_year--;
		  if (day_of_year == 31 + 28)
		    leap_day = 1;		/* Handle leapyear's leapday */
		}
	      }
	      month = 1;
	      days_per_month = days_in_month;
	      while (day_of_year > *days_per_month)
	      {
		  month++;
		  day_of_year -= *days_per_month++;
	      }
	      day = day_of_year + leap_day;
	    }
	    memcpy(&int4val, &dbffield[4], sizeof(int4val));
	    int4val = int4val / 1000; /* Discard millseconds */
	    ss = int4val % 60;
	    mm = (int4val / 60) % 60;
	    hh = int4val / (60 * 60);
	    snprintf(fields[t].db_contents, fields[t].db_blen,
		     "%04d-%02d-%02d %02d:%02d:%02d",
		     year, month, day, hh, mm, ss);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    break;

	  case 'I':		/* Integer 8 byte field - ???Endian Alert??? */
	    memcpy(&int4val, dbffield, sizeof(int4val));
	    snprintf(fields[t].db_contents, fields[t].db_blen,  "%ld", int4val);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    break;

	  default:
	    end = (char *)dbffield;
	    i = fields[t].db_flen;
	    while (( i > 0) && ((*end < 0x21)/* || (*end > 0x7E)*/)) {
		end++;
		i--;
	    }
	    strncpy(fields[t].db_contents, end, i);
	    fields[t].db_contents[i] = '\0';
	    break;
	}
	dbffield += fields[t].db_flen;
    }

    dbh->db_offset += dbh->db_rlen;

    return DBF_VALID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdx, %r15
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movzwl 0x30(%rdi), %eax
movl %r15d, %ecx
imull %eax, %ecx
addl 0x18(%rdi), %ecx
movl (%rdi), %edi
movslq %ecx, %r14
movq %r14, %rsi
xorl %edx, %edx
callq 0x2390
cmpq $-0x1, %rax
je 0x5166
movq %r14, 0x8(%rbx)
movq %r15, 0x28(%rbx)
movl (%rbx), %edi
movq 0x38(%rbx), %rsi
movzwl 0x30(%rbx), %edx
callq 0x22d0
movq 0x38(%rbx), %r12
movl $0x2a, %eax
cmpb $0x2a, (%r12)
je 0x518e
movq %rbx, %r14
cmpb $0x0, 0x32(%rbx)
je 0x5180
incq %r12
leaq 0x10(%r14), %rax
movq %rax, 0x20(%rsp)
leaq 0x58(%r14), %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq %r14, 0x8(%rsp)
movq %rbp, %rax
shlq $0x5, %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax), %r15
movzbl 0xb(%rcx,%rax), %eax
leal -0x42(%rax), %ecx
cmpl $0xb, %ecx
ja 0x4a07
leaq 0x208c(%rip), %rdx      # 0x6a30
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x20(%rsp), %rax
movzbl (%rax), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x4abf
movq 0x18(%r15), %rdi
movzbl 0xc(%r15), %edx
movq %r12, %rsi
callq 0x2110
movq 0x18(%r15), %rax
movzbl 0xc(%r15), %ecx
movb $0x0, (%rax,%rcx)
movq 0x18(%r15), %r14
movq %r14, %rdi
callq 0x2240
movq %rax, %r13
movzbl 0xc(%r15), %edx
movq %r14, %rdi
movq 0x8(%rsp), %r14
xorl %esi, %esi
callq 0x2120
jmp 0x4c18
cmpl $0x54, %eax
je 0x4b55
cmpl $0x59, %eax
jne 0x4a88
movq (%r12), %r8
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdi
movzbl 0xd(%r15), %ecx
incl %ecx
leaq 0x214e(%rip), %rdx      # 0x6b7d
xorl %eax, %eax
callq 0x2330
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
xorl %ebx, %ebx
movb %bl, -0x1(%rcx,%rax)
movq 0x18(%r15), %r14
movq %r14, %rdi
callq 0x20d0
leaq (%r14,%rax), %rcx
movb %bl, 0x1(%r14,%rax)
cmpb %bl, 0xd(%r15)
je 0x4fe6
xorl %eax, %eax
movq 0x8(%rsp), %r14
leaq -0x1(%rcx), %rdx
movb -0x1(%rcx), %sil
movb %sil, (%rcx)
incl %eax
movzbl 0xd(%r15), %esi
movq %rdx, %rcx
cmpl %esi, %eax
jb 0x4a6a
jmp 0x4fee
movzbl 0xc(%r15), %eax
testl %eax, %eax
je 0x4ca4
leal -0x1(%rax), %ecx
leaq (%r12,%rcx), %rsi
incq %rsi
movq %r12, %rcx
cmpb $0x20, (%rcx)
jg 0x4dd3
incq %rcx
leal -0x1(%rax), %edx
cmpl $0x1, %eax
movl %edx, %eax
jg 0x4aa2
xorl %eax, %eax
jmp 0x4dd6
cmpl $0x4, %eax
jne 0x4c15
movq %r12, %rdi
callq 0x557c
movq %rax, %r13
jmp 0x4c18
movzbl 0xc(%r15), %eax
testl %eax, %eax
je 0x4fc4
movl %eax, %ecx
incl %eax
cmpb $0x20, -0x1(%r12,%rcx)
ja 0x4fc2
decq %rcx
decl %eax
cmpl $0x1, %eax
jg 0x4ae9
xorl %eax, %eax
jmp 0x4fc4
movsd (%r12), %xmm0
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdi
movzbl 0xd(%r15), %ecx
leaq 0x2058(%rip), %rdx      # 0x6b78
movb $0x1, %al
callq 0x2330
jmp 0x4b43
movq (%r12), %rcx
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdi
leaq 0x2066(%rip), %rdx      # 0x6ba2
xorl %eax, %eax
callq 0x2330
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
movb $0x0, -0x1(%rcx,%rax)
jmp 0x4ff1
movl (%r12), %eax
leal -0x51fe78(%rax), %edx
xorl %r8d, %r8d
movl $0x0, %r9d
movl $0x0, %ecx
cmpl $0xffc845da, %edx       # imm = 0xFFC845DA
jb 0x4cf3
leal -0x1a42e4(%rax), %ecx
imull $0x64, %ecx, %ecx
movl $0xe5aae5e1, %edx       # imm = 0xE5AAE5E1
imulq %rdx, %rcx
shrq $0x2f, %rcx
imull $0xfffffe93, %ecx, %edx # imm = 0xFFFFFE93
addl %eax, %edx
addl $0xffe5bd1c, %edx       # imm = 0xFFE5BD1C
leal -0x1(%rcx), %eax
imulq $0x51eb851f, %rax, %rsi # imm = 0x51EB851F
shrl $0x2, %eax
subl %eax, %edx
shrq $0x25, %rsi
leal (%rsi,%rsi,2), %eax
addl $0x3, %eax
shrl $0x2, %eax
addl %edx, %eax
leaq 0x1eda(%rip), %r9       # 0x6a9c
testb $0x3, %cl
je 0x4bcb
xorl %edx, %edx
jmp 0x4bfc
movl %ecx, %edx
imulq $0x51eb851f, %rdx, %rsi # imm = 0x51EB851F
movq %rsi, %rdx
shrq $0x25, %rdx
imull $0x64, %edx, %edi
movb $0x1, %dl
cmpl %ecx, %edi
jne 0x4bfc
shrq $0x27, %rsi
imull $0x190, %esi, %edx      # imm = 0x190
cmpl %ecx, %edx
sete %sil
testl %ecx, %ecx
setne %dl
andb %sil, %dl
movzbl %dl, %edi
addl $0x16d, %edi            # imm = 0x16D
movl %eax, %esi
subl %edi, %esi
jbe 0x4cac
incl %ecx
movl %esi, %eax
jmp 0x4bc2
xorl %r13d, %r13d
testq %r13, %r13
je 0x4c90
movq %r12, 0x18(%rsp)
movq 0x68(%r14), %r12
imulq %r13, %r12
movl 0x50(%r14), %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2390
cmpq $-0x1, %rax
je 0x5069
movq %rax, %r14
movq %r15, 0x10(%rsp)
cmpq %r12, %rax
movq %rbp, 0x28(%rsp)
jne 0x507a
movq 0x20(%rsp), %rax
movzbl (%rax), %eax
cmpq $0x4, %rax
ja 0x5111
leaq 0x1def(%rip), %rcx      # 0x6a60
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rsp), %r15
movq 0x18(%r15), %rax
movb $0x0, (%rax)
movq 0x8(%rsp), %r14
jmp 0x4fbb
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x4ff1
movq %r12, %rsi
jmp 0x4dd6
cmpl $0x3c, %eax
setae %sil
andb %sil, %dl
leal -0x1(%rax), %edi
cmpl $0x3b, %edi
sete %sil
testb %dl, %dl
cmovnel %edi, %eax
movl $0x1, %r8d
cmpl $0x20, %eax
jb 0x4ce9
movl $0x1f, %edi
movl $0x1, %r8d
subl %edi, %eax
movzbl (%r8,%r9), %edi
incq %r8
cmpl %edi, %eax
ja 0x4cdb
andb %sil, %dl
movzbl %dl, %r9d
addl %eax, %r9d
movq 0x4(%r12), %rsi
movq %rsi, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
imulq %rdx
movq %rdx, %r10
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %r10
addq %rax, %r10
movq %r10, %rax
movabsq $-0x7777777777777777, %rdi # imm = 0x8888888888888889
imulq %rdi
addq %r10, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
subl %eax, %r10d
movq %rsi, %rax
movabsq $0x45e7b272f608770f, %rdx # imm = 0x45E7B272F608770F
imulq %rdx
movq %rdx, %r11
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xe, %r11
addq %rax, %r11
movq %r11, %rax
imulq %rdi
addq %r11, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %edi
movq %rsi, %rax
movabsq $0x4a90be587de6e565, %rdx # imm = 0x4A90BE587DE6E565
imulq %rdx
movq %rdx, %r14
subl %edi, %r11d
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x14, %r14
addl %eax, %r14d
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdi
subq $0x8, %rsp
leaq 0x1dd4(%rip), %rdx      # 0x6b84
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r14
callq 0x2330
addq $0x20, %rsp
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
movb $0x0, -0x1(%rcx,%rax)
jmp 0x4fdf
movq %rcx, %rsi
movq 0x18(%r15), %rdi
movl %eax, %r14d
jmp 0x4fce
movq 0x8(%rsp), %r14
movl 0x50(%r14), %edi
movq 0x58(%r14), %rsi
movl $0x8, %edx
callq 0x22d0
testq %rax, %rax
movq 0x18(%rsp), %r12
js 0x5103
movq 0x58(%r14), %rdi
movl (%rdi), %ebx
movzbl 0x4(%rdi), %eax
shll $0x18, %eax
movzbl 0x5(%rdi), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x6(%rdi), %ecx
shll $0x8, %ecx
orq %rax, %rcx
movzbl 0x7(%rdi), %r13d
orq %rcx, %r13
cmpq %r13, 0x60(%r14)
movq %r14, %r15
jae 0x4e64
leal 0xfff(%r13), %eax
andl $0xfffff000, %eax       # imm = 0xFFFFF000
orl $0xff0, %eax            # imm = 0xFF0
movslq %eax, %r14
movq %r14, %rsi
callq 0x2350
testq %rax, %rax
je 0x4e64
movq %rax, 0x58(%r15)
movq %r14, 0x60(%r15)
movq 0x60(%r15), %rax
cmpq %r13, %rax
cmovbq %rax, %r13
movl 0x50(%r15), %edi
movq 0x58(%r15), %rsi
movq %r13, %rdx
callq 0x22d0
testq %rax, %rax
movq %r15, %r14
js 0x5103
bswapl %ebx
cmpl $0x2, %ebx
movq 0x10(%rsp), %r15
je 0x4ead
cmpl $0x1, %ebx
je 0x500f
testl %ebx, %ebx
movq 0x10(%rsp), %r15
jne 0x4ff1
movq 0x18(%r15), %rax
movb $0x0, (%rax)
jmp 0x4ff1
movq 0x8(%rsp), %r14
movq 0x58(%r14), %rax
xorl %r15d, %r15d
xorl %ebx, %ebx
leaq 0x200(%r15), %r12
movq %rax, %r13
cmpq %r12, 0x60(%r14)
jae 0x4f09
movq 0x58(%r14), %rdi
leal 0x800(%r15), %eax
movslq %eax, %rbp
movq %rbp, %rsi
callq 0x2350
testq %rax, %rax
je 0x51a0
movq %rax, %r13
movq %rax, 0x58(%r14)
movq %rbp, 0x60(%r14)
addq %r15, %r13
movq 0x28(%rsp), %rbp
movl 0x50(%r14), %edi
movl $0x200, %edx            # imm = 0x200
movq %r13, %rsi
callq 0x22d0
cmpq $0x200, %rax            # imm = 0x200
jl 0x512f
leaq 0x200(%r13), %rax
xorl %ecx, %ecx
cmpb $0x1a, (%r13,%rcx)
je 0x4f47
incq %rcx
cmpl $0x200, %ecx            # imm = 0x200
jne 0x4f2f
movq %r12, %r15
jmp 0x4f55
addq %rcx, %r15
addq %rcx, %r13
movl $0x1, %ebx
movq %r13, %rax
testl %ebx, %ebx
je 0x4ec7
movb $0x0, (%rax)
movq 0x10(%rsp), %rbx
cmpq %r15, 0x10(%rbx)
ja 0x4f88
movq 0x18(%rbx), %rdi
leaq 0x1(%r15), %r14
movq %r14, %rsi
callq 0x2350
testq %rax, %rax
je 0x4f88
movq %rax, 0x18(%rbx)
movq %r14, 0x10(%rbx)
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdi
leaq -0x1(%rax), %r14
cmpq %rax, %r15
cmovbq %r15, %r14
movq %rbx, %r15
movq 0x8(%rsp), %rbx
movq 0x58(%rbx), %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%r15), %rax
movb $0x0, (%rax,%r14)
movq %rbx, %r14
movq 0x18(%rsp), %r12
jmp 0x4ff1
movl %ecx, %eax
movq 0x18(%r15), %rdi
movl %eax, %r14d
movq %r12, %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%r15), %rax
movb $0x0, (%rax,%r14)
movq 0x8(%rsp), %r14
jmp 0x4ff1
movq %rcx, %rdx
movq 0x8(%rsp), %r14
movb $0x2e, (%rdx)
movzbl 0xc(%r15), %eax
addq %rax, %r12
incq %rbp
movzbl 0x32(%r14), %eax
cmpq %rax, %rbp
jb 0x4980
jmp 0x5180
movq 0x10(%rsp), %r15
cmpq %r13, 0x10(%r15)
ja 0x5037
movq 0x18(%r15), %rdi
leaq 0x1(%r13), %r14
movq %r14, %rsi
callq 0x2350
testq %rax, %rax
je 0x5037
movq %rax, 0x18(%r15)
movq %r14, 0x10(%r15)
movq 0x10(%r15), %rax
movq 0x18(%r15), %rdi
leaq -0x1(%rax), %r14
cmpq %rax, %r13
cmovbq %r13, %r14
movq 0x8(%rsp), %rbx
movq 0x58(%rbx), %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%r15), %rax
movb $0x0, (%rax,%r14)
movq %rbx, %r14
jmp 0x4ff1
leaq 0x1c05(%rip), %rdi      # 0x6c75
callq 0x2180
jmp 0x4c86
callq 0x2040
movq %rax, %r15
movl (%rax), %ebx
subq $0x8, %rsp
movl $0x100, %esi            # imm = 0x100
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
leaq 0x1be8(%rip), %rdx      # 0x6c84
movq %r13, %rcx
movq %r12, %r8
movq %r14, %r9
xorl %eax, %eax
pushq %rbx
callq 0x2330
addq $0x10, %rsp
movb $0x0, 0x13f(%rsp)
movl %ebx, (%r15)
movq %rbp, %rdi
movq 0x28(%rsp), %rbp
callq 0x2180
movq 0x10(%rsp), %r15
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x4c86
movq 0x8(%rsp), %r14
movl 0x50(%r14), %edi
movq 0x58(%r14), %rsi
movl $0x8, %edx
callq 0x22d0
testq %rax, %rax
movq 0x18(%rsp), %r12
jns 0x514c
leaq 0x1bbb(%rip), %rdi      # 0x6cc5
callq 0x2180
jmp 0x5125
movq 0x20(%rsp), %rdi
callq 0x26ca
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r15
jmp 0x4ff1
leaq 0x1b8f(%rip), %rdi      # 0x6cc5
callq 0x2180
movq 0x10(%rsp), %r15
movq 0x18(%r15), %rax
movb $0x0, (%rax)
jmp 0x4fbb
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
movq %r13, %rdx
callq 0x266b
jmp 0x4ff1
movl (%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2390
movq $0x0, 0x8(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x518e
movzwl 0x30(%r14), %eax
addq %rax, 0x8(%r14)
movl $0x20, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x26b5