// File: /workspace/github_c_makefile_star2_O2/canboat[P]canboat/n2kd/main.c
// Function: safeWriteBuffer
static void safeWriteBuffer(int idx, StringBuffer *sb)
{
  int r;

  if (stream[idx].writeBuffer.len > 0 && sb != &stream[idx].writeBuffer)
  {
    // Awww shit, last time we did not write everything, append the
    // new bits to the old unwritten data and try to write the buffered
    // data
    sbAppendData(&stream[idx].writeBuffer, sb->data, sb->len);
    sb = &stream[idx].writeBuffer;
  }

  // We are told the stream is ready to write, but it is in non-blocked
  // mode so it can write less than the buffer.

  r = write(stream[idx].fd, sb->data, sb->len);
  if (r < sb->len)
  {
    if (r <= 0)
    {
      if (errno != EAGAIN)
      {
        if (stream[idx].type == DATA_OUTPUT_COPY || stream[idx].type == DATA_OUTPUT_STREAM)
        {
          logAbort("Cannot write to stdout: %s\n", strerror(errno));
        }
        logError("Closing %s stream %d: %s\n", streamTypeName[stream[idx].type], stream[idx].fd, strerror(errno));
        closeStream(idx);
      }
    }
    else
    {
      if (sb == &stream[idx].writeBuffer)
      {
        // Remove the part of the buffer we did write
        sbDelete(sb, 0, (size_t) r);
      }
      else
      {
        // Store the remaining part in the per-fd writebuffer
        // and write it on the next attempt
        sbAppendData(&stream[idx].writeBuffer, sb->data + r, sb->len - r);
      }
    }
  }
  else
  {
    if (sb == &stream[idx].writeBuffer)
    {
      sbEmpty(sb);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %r14
movl %edi, %ebx
movslq %edi, %rax
leaq 0x95b7(%rip), %rcx      # 0xd530
imulq $0x8038, %rax, %rax     # imm = 0x8038
leaq (%rcx,%rax), %r15
addq $0x8018, %r15           # imm = 0x8018
movq %rcx, %r12
addq %rax, %r12
cmpq $0x0, 0x8020(%rcx,%rax)
sete %al
cmpq %rsi, %r15
sete %cl
orb %al, %cl
jne 0x3fb9
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x70cf
movq %r15, %r14
movl (%r12), %edi
movq (%r14), %rsi
movq 0x8(%r14), %rdx
callq 0x20e0
movslq %eax, %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
jbe 0x3ff4
testl %eax, %eax
jle 0x400b
cmpq %r15, %r14
je 0x4020
addq (%r14), %rcx
movq %r15, %rdi
movq %rcx, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x70cf
cmpq %r15, %r14
jne 0x4017
andq $0x0, 0x8(%r14)
movq (%r14), %rax
testq %rax, %rax
je 0x4017
movb $0x0, (%rax)
jmp 0x4017
callq 0x2050
movl (%rax), %edi
cmpl $0xb, %edi
jne 0x4035
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
xorl %esi, %esi
movq %rcx, %rdx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x7074
movq %rax, %r14
movl 0x4(%r12), %eax
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x10, %ecx
jne 0x4065
callq 0x2370
leaq 0x4bd0(%rip), %rdi      # 0x8c23
movq %rax, %rsi
xorl %eax, %eax
callq 0x6e9a
movl 0x4(%r12), %eax
movl (%r14), %edi
movl %eax, %eax
leaq 0x91a2(%rip), %rcx      # 0xd210
movq (%rcx,%rax,8), %r14
movl (%r12), %ebp
callq 0x2370
leaq 0x4bbd(%rip), %rdi      # 0x8c3f
movq %r14, %rsi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x6e09
movl %ebx, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3e5c