// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jbe 0x6b61
movq -0x10(%rbp), %rax
movl (%rax), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x2370
movq %rax, -0x30(%rbp)
cmpq $-0x1, %rax
jne 0x6b4f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6f59
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
jmp 0x6ba9
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rdx
movzwl 0x30(%rdx), %edx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x2370
movq %rax, -0x30(%rbp)
cmpq $-0x1, %rax
jne 0x6ba7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6f59
jmp 0x6ba9
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
movl %eax, %edx
movl $0x20, %esi
callq 0x2110
movq -0x58(%rbp), %rax
movb $0x20, (%rax)
movq $0x1, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movzbl 0x32(%rcx), %ecx
cmpq %rcx, %rax
jae 0x6f14
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x6ee8
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xb(%rax), %eax
cmpl $0x43, %eax
jne 0x6cce
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x20c0
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movzbl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x6c89
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
movq %rax, -0x50(%rbp)
jmp 0x6ca5
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x20c0
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rdi
addq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rsi
movq -0x50(%rbp), %rdx
callq 0x2100
jmp 0x6ee6
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xb(%rax), %eax
cmpl $0x44, %eax
jne 0x6dad
movl $0x0, -0x108(%rbp)
movl $0x0, -0x104(%rbp)
cmpl $0xa, -0x104(%rbp)
jge 0x6d74
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movslq -0x104(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
je 0x6d61
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movslq -0x104(%rbp), %rcx
movb (%rax,%rcx), %cl
movl -0x108(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x108(%rbp)
cltq
movb %cl, -0x112(%rbp,%rax)
jmp 0x6d63
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x6cfe
movl -0x108(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x108(%rbp)
cltq
movb $0x0, -0x112(%rbp,%rax)
movq -0x58(%rbp), %rdi
addq -0x38(%rbp), %rdi
leaq -0x112(%rbp), %rsi
movl $0x8, %edx
callq 0x2100
jmp 0x6ee4
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xb(%rax), %eax
cmpl $0x4e, %eax
jne 0x6e3e
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xd(%rax), %eax
cmpl $0x0, %eax
je 0x6e3e
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x2240
movsd %xmm0, -0x60(%rbp)
leaq -0x100(%rbp), %rdi
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xd(%rax), %edx
leaq 0x1e77(%rip), %rsi      # 0x8c92
movb $0x0, %al
callq 0x2060
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movsd -0x60(%rbp), %xmm0
movb $0x1, %al
callq 0x2060
jmp 0x6e5d
leaq -0xe0(%rbp), %rdi
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rsi
callq 0x2190
leaq -0xe0(%rbp), %rdi
callq 0x20c0
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movzbl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x6e9a
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
movq %rax, -0x50(%rbp)
jmp 0x6eaa
leaq -0xe0(%rbp), %rdi
callq 0x20c0
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
subq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rdi
movq -0x38(%rbp), %rax
addq -0x48(%rbp), %rax
addq %rax, %rdi
leaq -0xe0(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x2100
jmp 0x6ee6
jmp 0x6ee8
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x6bf0
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x58(%rbp), %rsi
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
movl %eax, %edx
callq 0x2090
cmpq $-0x1, %rax
jne 0x6f3c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6f59
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop