// File: /workspace/github_c_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movq %rdx, %r15
movq %rsi, 0x28(%rsp)
movq %rdi, %r12
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 0x50e8
movq %r14, 0x8(%r12)
movq %r15, 0x28(%r12)
movl (%r12), %edi
movq 0x38(%r12), %rsi
movzwl 0x30(%r12), %edx
callq 0x22d0
movq 0x38(%r12), %r15
movl $0x2a, %eax
cmpb $0x2a, (%r15)
je 0x5115
cmpb $0x0, 0x32(%r12)
je 0x5105
incq %r15
leaq 0x10(%r12), %rax
movq %rax, 0x18(%rsp)
leaq 0x58(%r12), %rax
movq %rax, 0x10(%rsp)
leaq 0x216f(%rip), %r14      # 0x6a9c
xorl %ebx, %ebx
movq %r12, (%rsp)
movq %rbx, %rax
shlq $0x5, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax), %rbp
movzbl 0xb(%rcx,%rax), %eax
leal -0x42(%rax), %ecx
cmpl $0xb, %ecx
ja 0x49b9
leaq 0x20d9(%rip), %rdx      # 0x6a30
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x18(%rsp), %rax
movzbl (%rax), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x4a71
movq 0x18(%rbp), %rdi
movzbl 0xc(%rbp), %edx
movq %r15, %rsi
callq 0x2110
movq 0x18(%rbp), %rax
movzbl 0xc(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movq 0x18(%rbp), %r14
movq %r14, %rdi
callq 0x2240
movq %rax, %r13
movzbl 0xc(%rbp), %edx
movq %r14, %rdi
leaq 0x20ef(%rip), %r14      # 0x6a9c
xorl %esi, %esi
callq 0x2120
jmp 0x4a81
cmpl $0x54, %eax
je 0x4b65
cmpl $0x59, %eax
jne 0x4a3b
movq (%r15), %r8
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdi
movzbl 0xd(%rbp), %ecx
incl %ecx
leaq 0x219e(%rip), %rdx      # 0x6b7d
xorl %eax, %eax
callq 0x2330
movq 0x10(%rbp), %rax
movq 0x18(%rbp), %rcx
xorl %r13d, %r13d
movb %r13b, -0x1(%rcx,%rax)
movq 0x18(%rbp), %r14
movq %r14, %rdi
callq 0x20d0
leaq (%r14,%rax), %rcx
movb %r13b, 0x1(%r14,%rax)
cmpb %r13b, 0xd(%rbp)
je 0x4d8d
xorl %eax, %eax
leaq 0x207e(%rip), %r14      # 0x6a9c
leaq -0x1(%rcx), %rdx
movb -0x1(%rcx), %sil
movb %sil, (%rcx)
incl %eax
movzbl 0xd(%rbp), %esi
movq %rdx, %rcx
cmpl %esi, %eax
jb 0x4a1e
jmp 0x4d97
movzbl 0xc(%rbp), %eax
testl %eax, %eax
je 0x4c29
leal -0x1(%rax), %ecx
leaq (%r15,%rcx), %rsi
incq %rsi
movq %r15, %rcx
cmpb $0x20, (%rcx)
jg 0x4d61
incq %rcx
leal -0x1(%rax), %edx
cmpl $0x1, %eax
movl %edx, %eax
jg 0x4a54
xorl %eax, %eax
jmp 0x4d64
cmpl $0x4, %eax
jne 0x4ae3
movq %r15, %rdi
callq 0x550c
movq %rax, %r13
testq %r13, %r13
je 0x4ae3
movq %r12, %rax
movq 0x68(%r12), %r12
imulq %r13, %r12
movl 0x50(%rax), %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2390
cmpq $-0x1, %rax
je 0x4f90
movq %rax, %r14
cmpq %r12, %rax
jne 0x4f9e
movq 0x18(%rsp), %rax
movzbl (%rax), %eax
cmpq $0x4, %rax
ja 0x50a8
leaq 0x1f92(%rip), %rcx      # 0x6a60
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x18(%rbp), %rax
movb $0x0, (%rax)
jmp 0x4f67
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x4f72
movzbl 0xc(%rbp), %ecx
testl %ecx, %ecx
je 0x4d6d
movl %ecx, %eax
incl %ecx
cmpb $0x20, -0x1(%r15,%rax)
ja 0x4b1b
decq %rax
decl %ecx
cmpl $0x1, %ecx
jg 0x4b07
xorl %eax, %eax
movl %eax, %r14d
jmp 0x4d70
movsd (%r15), %xmm0
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdi
movzbl 0xd(%rbp), %ecx
leaq 0x203d(%rip), %rdx      # 0x6b78
movb $0x1, %al
callq 0x2330
jmp 0x4d4f
movq (%r15), %rcx
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdi
leaq 0x2049(%rip), %rdx      # 0x6ba2
xorl %eax, %eax
callq 0x2330
jmp 0x4d4f
movl (%r15), %eax
leal -0x51fe78(%rax), %edx
xorl %r8d, %r8d
movl $0x0, %r9d
movl $0x0, %ecx
cmpl $0xffc845da, %edx       # imm = 0xFFC845DA
jb 0x4c7b
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
movzwl %ax, %esi
shrl $0x2, %eax
subl %eax, %edx
shrl $0x2, %esi
imull $0x147b, %esi, %eax     # imm = 0x147B
shrl $0x11, %eax
leal (%rax,%rax,2), %eax
addl $0x3, %eax
shrl $0x2, %eax
addl %edx, %eax
testb $0x3, %cl
je 0x4bdc
movl $0x16d, %esi            # imm = 0x16D
xorl %edx, %edx
jmp 0x4c1d
movl %ecx, %edx
imulq $0x51eb851f, %rdx, %rdx # imm = 0x51EB851F
movq %rdx, %rsi
shrq $0x25, %rsi
imull $0x64, %esi, %esi
cmpl %ecx, %esi
jne 0x4c16
shrq $0x27, %rdx
imull $0x190, %edx, %edx      # imm = 0x190
cmpl %ecx, %edx
sete %sil
testl %ecx, %ecx
setne %dl
andb %sil, %dl
movzbl %dl, %esi
addl $0x16d, %esi            # imm = 0x16D
jmp 0x4c1d
movl $0x16e, %esi            # imm = 0x16E
movb $0x1, %dl
movl %eax, %edi
subl %esi, %edi
jbe 0x4c34
incl %ecx
movl %edi, %eax
jmp 0x4bce
movq %r15, %rsi
xorl %r14d, %r14d
jmp 0x4d67
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 0x4c71
movl $0x1f, %edi
movl $0x1, %r8d
subl %edi, %eax
movzbl (%r8,%r14), %edi
incq %r8
cmpl %edi, %eax
ja 0x4c63
andb %sil, %dl
movzbl %dl, %r9d
addl %eax, %r9d
movq 0x4(%r15), %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(%rbp), %rsi
movq 0x18(%rbp), %rdi
subq $0x8, %rsp
leaq 0x1e4d(%rip), %rdx      # 0x6b84
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r14
leaq 0x1d56(%rip), %r14      # 0x6a9c
callq 0x2330
addq $0x20, %rsp
movq 0x10(%rbp), %rax
movq 0x18(%rbp), %rcx
movb $0x0, -0x1(%rcx,%rax)
jmp 0x4f72
movq %rcx, %rsi
movl %eax, %r14d
movq 0x18(%rbp), %rdi
jmp 0x4d77
xorl %r14d, %r14d
movq 0x18(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%rbp), %rax
movb $0x0, (%rax,%r14)
jmp 0x4f6b
movq %rcx, %rdx
leaq 0x1d05(%rip), %r14      # 0x6a9c
movb $0x2e, (%rdx)
jmp 0x4f72
movq (%rsp), %r12
movl 0x50(%r12), %edi
movq 0x58(%r12), %rsi
movl $0x8, %edx
callq 0x22d0
testq %rax, %rax
js 0x5062
movq 0x58(%r12), %rsi
movq 0x60(%r12), %r14
movl (%rsi), %eax
movl %eax, 0x8(%rsp)
movzbl 0x4(%rsi), %eax
shll $0x18, %eax
movzbl 0x5(%rsi), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x6(%rsi), %ecx
shll $0x8, %ecx
orq %rax, %rcx
movzbl 0x7(%rsi), %r13d
orq %rcx, %r13
cmpq %r13, %r14
jae 0x4f31
leal 0xfff(%r13), %eax
andl $0xfffff000, %eax       # imm = 0xFFFFF000
orl $0xff0, %eax            # imm = 0xFF0
movslq %eax, %r14
movq %rsi, %rdi
movq %r14, %rsi
callq 0x2350
testq %rax, %rax
je 0x5007
movq %rax, %rsi
movq (%rsp), %r12
movq %rax, 0x58(%r12)
movq %r14, 0x60(%r12)
jmp 0x5015
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x1c45(%rip), %r14      # 0x6a9c
leaq 0x200(%r13), %rdx
movq %rax, %r12
movq (%rsp), %rcx
movq %rdx, 0x20(%rsp)
cmpq %rdx, 0x60(%rcx)
jae 0x4eac
movq 0x10(%rsp), %rax
movq (%rax), %rdi
leal 0x800(%r13), %eax
movslq %eax, %r14
movq %r14, %rsi
callq 0x2350
testq %rax, %rax
je 0x5127
movq (%rsp), %rcx
movq %rax, 0x58(%rcx)
movq %r14, 0x60(%rcx)
addq %rax, %r13
movq %r13, %r12
leaq 0x1bf0(%rip), %r14      # 0x6a9c
movl 0x50(%rcx), %edi
movl $0x200, %edx            # imm = 0x200
movq %r12, %rsi
callq 0x22d0
cmpq $0x200, %rax            # imm = 0x200
jl 0x50cc
leaq 0x200(%r12), %rax
xorl %r13d, %r13d
movq 0x8(%rsp), %rcx
cmpb $0x1a, (%r12,%r13)
je 0x4f01
incq %r13
cmpl $0x200, %r13d           # imm = 0x200
jne 0x4ed8
addq $-0x200, %rcx           # imm = 0xFE00
movq %rcx, 0x8(%rsp)
movq 0x20(%rsp), %r13
jmp 0x4e57
movb $0x0, (%r12,%r13)
movq 0x10(%rbp), %r12
subq %rcx, %r13
cmpq %r13, %r12
ja 0x4f3e
movq 0x18(%rbp), %rdi
leaq 0x1(%r13), %r12
movq %r12, %rsi
callq 0x2350
testq %rax, %rax
je 0x4f3a
movq %rax, 0x18(%rbp)
movq %r12, 0x10(%rbp)
jmp 0x4f3e
movq (%rsp), %r12
jmp 0x5015
movq 0x10(%rbp), %r12
leaq -0x1(%r12), %r14
cmpq %r12, %r13
cmovbq %r13, %r14
movq 0x18(%rbp), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%rbp), %rax
movb $0x0, (%rax,%r14)
movq (%rsp), %r12
leaq 0x1b2a(%rip), %r14      # 0x6a9c
movzbl 0xc(%rbp), %eax
addq %rax, %r15
incq %rbx
movzbl 0x32(%r12), %eax
cmpq %rax, %rbx
jb 0x4933
jmp 0x5105
leaq 0x1cde(%rip), %rdi      # 0x6c75
callq 0x2180
jmp 0x4f67
callq 0x2040
movq %rax, 0x8(%rsp)
movl (%rax), %r10d
subq $0x8, %rsp
movl $0x100, %esi            # imm = 0x100
leaq 0x38(%rsp), %rdi
leaq 0x1cc4(%rip), %rdx      # 0x6c84
movq %r13, %rcx
movq %r12, %r8
movq %r14, %r9
xorl %eax, %eax
movq %r10, %r14
pushq %r10
callq 0x2330
addq $0x10, %rsp
movb $0x0, 0x12f(%rsp)
movq 0x8(%rsp), %rax
movl %r14d, (%rax)
leaq 0x30(%rsp), %rdi
callq 0x2180
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x4f67
movq (%rsp), %r12
movq 0x58(%r12), %rsi
movq 0x60(%r12), %r14
cmpq %r13, %r14
cmovbq %r14, %r13
movl 0x50(%r12), %edi
movq %r13, %rdx
callq 0x22d0
testq %rax, %rax
js 0x5062
movl 0x8(%rsp), %eax
bswapl %eax
cmpl $0x2, %eax
leaq 0x1a5e(%rip), %r14      # 0x6a9c
je 0x504d
cmpl $0x1, %eax
je 0x5059
testl %eax, %eax
jne 0x4f72
movq 0x18(%rbp), %rax
movb $0x0, (%rax)
jmp 0x4f72
movq 0x10(%rbp), %r12
jmp 0x4f0d
leaq 0x1c5c(%rip), %rdi      # 0x6cc5
callq 0x2180
jmp 0x4f6b
movq (%rsp), %r12
movl 0x50(%r12), %edi
movq 0x58(%r12), %rsi
movl $0x8, %edx
callq 0x22d0
testq %rax, %rax
leaq 0x1a07(%rip), %r14      # 0x6a9c
jns 0x50b7
leaq 0x1c27(%rip), %rdi      # 0x6cc5
callq 0x2180
jmp 0x4f72
movq 0x18(%rsp), %rdi
callq 0x2707
jmp 0x4f67
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x26a8
jmp 0x4f72
leaq 0x1bf2(%rip), %rdi      # 0x6cc5
callq 0x2180
movq 0x18(%rbp), %rax
movb $0x0, (%rax)
movq (%rsp), %r12
jmp 0x4f72
movl (%r12), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2390
movq $0x0, 0x8(%r12)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5115
movzwl 0x30(%r12), %eax
addq %rax, 0x8(%r12)
movl $0x20, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x26f2