// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdi, %rax
cmpq %rdx, 0x20(%rdi)
jae 0x5370
movl (%rax), %edi
xorl %esi, %esi
movl $0x2, %edx
movq %rax, %rbx
callq 0x2390
cmpq $-0x1, %rax
je 0x5562
incq 0x20(%rbx)
movq %rbx, %rcx
jmp 0x5395
movzwl 0x30(%rax), %esi
imulq %rdx, %rsi
addq 0x18(%rax), %rsi
movl (%rax), %edi
xorl %edx, %edx
movq %rax, %rbx
callq 0x2390
movq %rbx, %rcx
cmpq $-0x1, %rax
je 0x5562
movq %rax, 0x8(%rcx)
movq 0x38(%rcx), %rbx
movzwl 0x30(%rcx), %edx
movq %rbx, %rdi
movl $0x20, %esi
movq %rcx, %r15
callq 0x2120
movq %r15, %rcx
movq %rbx, (%rsp)
movb $0x20, (%rbx)
cmpb $0x0, 0x32(%r15)
je 0x553e
movl $0x1, %ebp
leaq 0x50(%rsp), %rbx
xorl %r15d, %r15d
movq %r14, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
movq %r15, %rax
shlq $0x5, %rax
leaq (%r14,%rax), %r13
movq 0x18(%r14,%rax), %r12
cmpb $0x0, (%r12)
je 0x5526
movzbl 0xb(%r13), %eax
cmpl $0x43, %eax
je 0x54f7
movq %rcx, %r14
cmpl $0x4e, %eax
je 0x5469
cmpl $0x44, %eax
jne 0x54b3
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x8(%rsp), %r14
movb (%r12,%rax), %dl
cmpb $0x2d, %dl
je 0x5432
movslq %ecx, %rsi
incl %ecx
movb %dl, 0x16(%rsp,%rsi)
incq %rax
cmpq $0xa, %rax
jne 0x5420
movslq %ecx, %rax
movb $0x0, 0x16(%rsp,%rax)
movq (%rsp), %rax
leaq (%rax,%rbp), %rdi
movl $0x8, %edx
leaq 0x16(%rsp), %rsi
callq 0x2110
leaq 0x50(%rsp), %rbx
movq 0x20(%rsp), %rcx
jmp 0x5526
movzbl 0xd(%r13), %eax
testl %eax, %eax
je 0x54b3
movq %r12, %rdi
movl %eax, 0x10(%rsp)
callq 0x2260
movsd %xmm0, 0x28(%rsp)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
leaq 0x1713(%rip), %rsi      # 0x6ba6
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
movq %r12, %rsi
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x2080
jmp 0x54be
movq %rbx, %rdi
movq %r12, %rsi
callq 0x21b0
movq %r14, %r12
movq 0x8(%rsp), %r14
movq %rbx, %rdi
callq 0x20d0
movzbl 0xc(%r13), %edi
cmpq %rdi, %rax
cmovaeq %rdi, %rax
subq %rax, %rdi
movq (%rsp), %rcx
addq %rbp, %rcx
addq %rcx, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2110
movq %r12, %rcx
jmp 0x5526
movq %r12, %rdi
movq %rcx, %rbx
callq 0x20d0
movzbl 0xc(%r13), %edx
cmpq %rdx, %rax
cmovbq %rax, %rdx
movq (%rsp), %rax
leaq (%rax,%rbp), %rdi
movq %r12, %rsi
callq 0x2110
movq %rbx, %rcx
leaq 0x50(%rsp), %rbx
movzbl 0xc(%r13), %eax
addq %rax, %rbp
incq %r15
movzbl 0x32(%rcx), %eax
cmpq %rax, %r15
jb 0x53dd
movl (%rcx), %edi
movzwl 0x30(%rcx), %edx
movq (%rsp), %rsi
movq %rcx, %rbx
callq 0x20a0
cmpq $-0x1, %rax
je 0x5562
movzwl 0x30(%rbx), %eax
addq %rax, 0x8(%rbx)
xorl %eax, %eax
jmp 0x5567
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)