// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/dbf.c
// Function: DBFWriteAttribute
static int DBFWriteAttribute(DBFHandle psDBF, int hEntity, int iField, void *pValue) {
  int i, j, nRetResult = TRUE;
  unsigned char *pabyRec;
  char szSField[400], szFormat[20];

  /* Is this a valid record? */
  if (hEntity < 0 || hEntity > psDBF->nRecords)
    return (FALSE);
  if (psDBF->bNoHeader)
    DBFWriteHeader(psDBF);

  /* Is this a brand new record? */
  if (hEntity == psDBF->nRecords) {
    if (!DBFFlushRecord(psDBF))
      return FALSE;
    psDBF->nRecords++;
    for (i = 0; i < psDBF->nRecordLength; i++)
      psDBF->pszCurrentRecord[i] = ' ';
    psDBF->nCurrentRecord = hEntity;
  }

  /* Is this an existing record, but different than the last one */
  /* we accessed? */
  if (!DBFLoadRecord(psDBF, hEntity))
    return FALSE;
  pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
  psDBF->bCurrentRecordModified = TRUE;
  psDBF->bUpdated = TRUE;

  /* Translate NULL value to valid DBF file representation. */
  if (pValue == NULL) {
    memset((char *) (pabyRec + psDBF->panFieldOffset[iField]),
           DBFGetNullCharacter(psDBF->pachFieldType[iField]),
           psDBF->panFieldSize[iField]);
    return TRUE;
  }

  /* Assign all the record fields. */
  switch (psDBF->pachFieldType[iField]) {
  case 'D':
  case 'N':
  case 'F':
    if (psDBF->panFieldDecimals[iField] == 0) {
      int nWidth = psDBF->panFieldSize[iField];
      if ((int) sizeof(szSField) - 2 < nWidth)
        nWidth = sizeof(szSField) - 2;
      sprintf(szFormat, "%%%dd", nWidth);
      sprintf(szSField, szFormat, (int) *((double *) pValue));
      if ((int) strlen(szSField) > psDBF->panFieldSize[iField]) {
        szSField[psDBF->panFieldSize[iField]] = '\0';
        nRetResult = FALSE;
      }
      strncpy((char *) (pabyRec + psDBF->panFieldOffset[iField]),szSField, strlen(szSField));
    } else {
      int nWidth = psDBF->panFieldSize[iField];
      if ((int) sizeof(szSField) - 2 < nWidth)
        nWidth = sizeof(szSField) - 2;
      sprintf(szFormat, "%%%d.%df",
              nWidth, psDBF->panFieldDecimals[iField]);
      sprintf(szSField, szFormat, *((double *) pValue));
      if ((int) strlen(szSField) > psDBF->panFieldSize[iField]) {
        szSField[psDBF->panFieldSize[iField]] = '\0';
        nRetResult = FALSE;
      }
      strncpy((char *) (pabyRec + psDBF->panFieldOffset[iField]),szSField, strlen(szSField));
    }
    break;

  case 'L':
    if (psDBF->panFieldSize[iField] >= 1 &&
        (*(char *) pValue == 'F' || *(char *) pValue == 'T'))
      *(pabyRec + psDBF->panFieldOffset[iField]) = *(char *) pValue;
    break;

  default:
    if ((int) strlen((char *) pValue) > psDBF->panFieldSize[iField]) {
      j = psDBF->panFieldSize[iField];
      nRetResult = FALSE;
    } else {
      memset(pabyRec + psDBF->panFieldOffset[iField], ' ',psDBF->panFieldSize[iField]);
      j = strlen((char *) pValue);
    }
    strncpy((char *) (pabyRec + psDBF->panFieldOffset[iField]), (char *) pValue, j);
    break;
  }

  return (nRetResult);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
xorl %ebx, %ebx
testl %esi, %esi
js 0x2760
movl %esi, %r15d
movq %rdi, %r14
movl 0x8(%rdi), %eax
cmpl %esi, %eax
jl 0x2760
movq %rcx, %r12
movl %edx, %ebp
cmpl $0x0, 0x60(%r14)
je 0x2577
movq %r14, %rdi
callq 0x15b5
movl 0x8(%r14), %eax
cmpl %r15d, %eax
jne 0x25ac
movq %r14, %rdi
callq 0x1672
testl %eax, %eax
je 0x2760
incl 0x8(%r14)
xorl %eax, %eax
movslq 0xc(%r14), %rcx
cmpq %rcx, %rax
jge 0x25a8
movq 0x48(%r14), %rcx
movb $0x20, (%rcx,%rax)
incq %rax
jmp 0x2592
movl %r15d, 0x40(%r14)
movq %r14, %rdi
movl %r15d, %esi
callq 0x2467
testl %eax, %eax
je 0x2760
movq 0x48(%r14), %r15
pushq $0x1
popq %rbx
movl %ebx, 0x44(%r14)
movl %ebx, 0x64(%r14)
movslq %ebp, %rbp
testq %r12, %r12
je 0x2646
movq 0x30(%r14), %rax
movsbl (%rax,%rbp), %eax
addl $-0x44, %eax
rorl %eax
cmpl $0x2, %eax
jb 0x25fa
cmpl $0x4, %eax
je 0x26e8
cmpl $0x5, %eax
jne 0x270f
movq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
movl (%rcx,%rbp,4), %ecx
movl (%rax,%rbp,4), %eax
movl $0x18e, %edx            # imm = 0x18E
cmpl %edx, %eax
cmovll %eax, %edx
testl %ecx, %ecx
je 0x2679
leaq 0x1a2d(%rip), %rsi      # 0x404a
xorl %ebx, %ebx
movq %rsp, %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x11d0
movsd (%r12), %xmm0
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x11d0
jmp 0x26a7
movq 0x18(%r14), %rax
movq 0x30(%r14), %rcx
movslq (%rax,%rbp,4), %rax
addq %rax, %r15
movsbl (%rcx,%rbp), %edi
callq 0x209e
movq 0x20(%r14), %rcx
movslq (%rcx,%rbp,4), %rdx
movzbl %al, %esi
movq %r15, %rdi
callq 0x10f0
pushq $0x1
popq %rbx
jmp 0x2760
leaq 0x1a97(%rip), %rsi      # 0x4117
xorl %ebx, %ebx
movq %rsp, %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x11d0
cvttsd2si (%r12), %edx
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x11d0
movq %r12, %rdi
callq 0x10d0
movq 0x20(%r14), %rcx
movslq (%rcx,%rbp,4), %rcx
cmpl %eax, %ecx
jge 0x26c2
movb $0x0, 0x20(%rsp,%rcx)
jmp 0x26c5
pushq $0x1
popq %rbx
movq 0x18(%r14), %rax
movslq (%rax,%rbp,4), %rax
addq %rax, %r15
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x10d0
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
jmp 0x275b
movq 0x20(%r14), %rax
cmpl $0x0, (%rax,%rbp,4)
jle 0x2760
movzbl (%r12), %eax
cmpl $0x54, %eax
je 0x2701
cmpl $0x46, %eax
jne 0x2760
movq 0x18(%r14), %rcx
movslq (%rcx,%rbp,4), %rcx
movb %al, (%r15,%rcx)
jmp 0x2760
movq %r12, %rdi
callq 0x10d0
movq %rax, %rcx
movq 0x20(%r14), %rax
movl (%rax,%rbp,4), %eax
cmpl %ecx, %eax
jge 0x2729
xorl %ebx, %ebx
jmp 0x2747
movslq %eax, %rdx
movq 0x18(%r14), %rax
movslq (%rax,%rbp,4), %rdi
addq %r15, %rdi
pushq $0x20
popq %rsi
callq 0x10f0
movq %r12, %rdi
callq 0x10d0
movq 0x18(%r14), %rcx
movslq (%rcx,%rbp,4), %rcx
addq %rcx, %r15
movslq %eax, %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x1050
movl %ebx, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq