// File: /workspace/github_c_makefile_star2_O3/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 0xa3f7(%rip), %rcx      # 0xe520
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 0x4169
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x7870
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 0x41a4
testl %eax, %eax
jle 0x41be
cmpq %r15, %r14
je 0x41d3
addq (%r14), %rcx
movq %r15, %rdi
movq %rcx, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x7870
cmpq %r15, %r14
jne 0x41ca
movq $0x0, 0x8(%r14)
movq (%r14), %rax
testq %rax, %rax
je 0x41ca
movb $0x0, (%rax)
jmp 0x41ca
callq 0x2050
movl (%rax), %edi
cmpl $0xb, %edi
jne 0x41e8
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 0x7815
movq %rax, %r14
movl 0x4(%r12), %eax
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x10, %ecx
jne 0x4218
callq 0x2360
leaq 0x5a1d(%rip), %rdi      # 0x9c23
movq %rax, %rsi
xorl %eax, %eax
callq 0x762f
movl 0x4(%r12), %eax
movl (%r14), %edi
movl %eax, %eax
leaq 0x9fdf(%rip), %rcx      # 0xe200
movq (%rcx,%rax,8), %r14
movl (%r12), %ebp
callq 0x2360
leaq 0x5a0a(%rip), %rdi      # 0x9c3f
movq %r14, %rsi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x759c
movl %ebx, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3fe5