// File: /workspace/github_c_makefile_star2_O2/sonicse[P]dbf2mysql/dbf.c
// Function: dbf_put_record
int dbf_put_record(dbhead *dbh, field *rec, u_long where) {
  u_long	offset, new, idx, t, h, length;
  u_char	*data;
  double	fl;
  char		foo[128], format[32];

  int 		i,j;                        // for type D
  char 		newdate[10];

  /*	offset:	offset in file for this record
	new:	real offset after lseek
	idx:	index to which place we are inside the 'hardcore'-data for this
	record
	t:		field-counter
	data:	the hardcore-data that is put on disk
	h:		index into the field-part in the hardcore-data
	length:	length of the data to copy
	fl:		a float used to get the right precision with real numbers
	foo:	copy of db_contents when field is not 'C'
	format:	sprintf format-string to get the right precision with real numbers
	
	NOTE: this declaration of 'foo' can cause overflow when the contents-field
	is longer the 127 chars (which is highly unlikely, cos it is not used
	in text-fields).
  */


  if (where > dbh->db_records) {
    if ((new = lseek(dbh->db_fd, 0, SEEK_END)) == -1) {
      return DBF_ERROR;
    }
    dbh->db_records++;
  } else {
    offset = dbh->db_hlen + (where * dbh->db_rlen);
    if ((new = lseek(dbh->db_fd, offset, SEEK_SET)) == -1) {
      return DBF_ERROR;
    }
  }
  
  dbh->db_offset = new;
  
  data = dbh->db_buff;

  /* Set dataarea of data to ' ' (space) */
  memset(data,' ',dbh->db_rlen);
 
  data[0] = DBF_VALID;
 
  idx = 1;
  for (t = 0; t < dbh->db_nfields; t++) {

    /* if field is empty, don't do a thing */
    if (rec[t].db_contents[0] != '\0') {
      /*	Handle text */
      if (rec[t].db_type == 'C') {

	if (strlen(rec[t].db_contents) > rec[t].db_flen) {
	  length = rec[t].db_flen;
	} else {
	  length = strlen(rec[t].db_contents);
	}
	strncpy((char *)&data[idx], rec[t].db_contents, length);
      } else if (rec[t].db_type == 'D' ) {
	/* handle date fields: make 1999-12-31...to 19991231 **/
	j = 0;
	for (i = 0; i  < 10; i++) {
	  if (rec[t].db_contents[i] != '-') {
	    newdate[j++] = rec[t].db_contents[i];
	  }
	}
	newdate[j++] = '\000';
	//printf("DATE: %s -> %s\n",rec[t].db_contents, newdate );
	strncpy((char *)&data[idx], newdate, 8);
      } else {
	/* Handle the rest */
	
	/* Numeric is special, because of real numbers */
	if ((rec[t].db_type == 'N') && (rec[t].db_dec != 0)) {

	  fl = atof(rec[t].db_contents);
	  sprintf(format, "%%.%df", rec[t].db_dec); 
	  sprintf(foo, format, fl);
	} else {
	  strcpy(foo, rec[t].db_contents);
	}
	if (strlen(foo) > rec[t].db_flen) {
	  length = rec[t].db_flen;
	} else {
	  length = strlen(foo);
	}
	h = rec[t].db_flen - length;
	strncpy((char *)&data[idx+h], foo, length);
      }
      //printf("ii6\n");printf("is %d\n",t);

    }
    //printf("iii5");
    idx += rec[t].db_flen;
  }
  //printf("iii4\n");
  if (write(dbh->db_fd, data, dbh->db_rlen) == -1) {
    return DBF_ERROR;
  }
  
  dbh->db_offset += dbh->db_rlen;
  
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r15
movq %rdi, %rbp
pushq $-0x1
popq %rbx
cmpq %rdx, 0x20(%rdi)
jae 0x4fec
movl (%rbp), %edi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x2390
cmpq $-0x1, %rax
je 0x51b3
incq 0x20(%rbp)
jmp 0x500c
movzwl 0x30(%rbp), %esi
imulq %rdx, %rsi
addq 0x18(%rbp), %rsi
movl (%rbp), %edi
xorl %edx, %edx
callq 0x2390
cmpq $-0x1, %rax
je 0x51b3
movq %rax, 0x8(%rbp)
movq 0x38(%rbp), %rbx
movzwl 0x30(%rbp), %edx
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x2120
movq %rbx, (%rsp)
movb $0x20, (%rbx)
pushq $0x1
popq %rdi
leaq 0x50(%rsp), %r13
xorl %r12d, %r12d
movzbl 0x32(%rbp), %eax
cmpq %rax, %r12
jae 0x5190
movq %r12, %rax
shlq $0x5, %rax
leaq (%r15,%rax), %r14
movq 0x18(%r15,%rax), %rbx
cmpb $0x0, (%rbx)
je 0x5180
movzbl 0xb(%r14), %eax
cmpl $0x43, %eax
je 0x5130
cmpl $0x4e, %eax
je 0x509b
cmpl $0x44, %eax
jne 0x50ea
xorl %eax, %eax
xorl %ecx, %ecx
movslq %ecx, %rdx
cmpq $0xa, %rax
je 0x515c
movb (%rbx,%rax), %sil
cmpb $0x2d, %sil
je 0x5096
leal 0x1(%rdx), %ecx
movb %sil, 0x1e(%rsp,%rdx)
incq %rax
jmp 0x5077
movzbl 0xd(%r14), %eax
testl %eax, %eax
je 0x50ea
movq %rdi, 0x10(%rsp)
movq %rbx, %rdi
movl %eax, 0xc(%rsp)
callq 0x2260
movsd %xmm0, 0x28(%rsp)
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1adc(%rip), %rsi      # 0x6ba6
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x2080
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x2080
jmp 0x50fa
movq %rdi, 0x10(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x21b0
movq %r13, %rdi
callq 0x20d0
movzbl 0xc(%r14), %edi
cmpq %rdi, %rax
cmovaeq %rdi, %rax
subq %rax, %rdi
movq (%rsp), %rcx
movq 0x10(%rsp), %rbx
addq %rbx, %rcx
addq %rcx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x2110
movq %rbx, %rdi
jmp 0x5180
movq %rdi, %r13
movq %rbx, %rdi
callq 0x20d0
movzbl 0xc(%r14), %edx
cmpq %rdx, %rax
cmovbq %rax, %rdx
movq (%rsp), %rax
leaq (%rax,%r13), %rdi
movq %rbx, %rsi
callq 0x2110
movq %r13, %rdi
jmp 0x517b
movb $0x0, 0x1e(%rsp,%rdx)
movq (%rsp), %rax
movq %rdi, %rbx
addq %rax, %rdi
leaq 0x1e(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x2110
movq %rbx, %rdi
leaq 0x50(%rsp), %r13
movzbl 0xc(%r14), %eax
addq %rax, %rdi
incq %r12
jmp 0x5035
movl (%rbp), %edi
movzwl 0x30(%rbp), %edx
movq (%rsp), %rsi
callq 0x20a0
cmpq $-0x1, %rax
pushq $-0x1
popq %rbx
je 0x51b3
movzwl 0x30(%rbp), %eax
addq %rax, 0x8(%rbp)
xorl %ebx, %ebx
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop