// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0xcba6(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpq $0x0, 0x8020(%rax)
jbe 0x4a82
movq -0x10(%rbp), %rax
movslq -0x4(%rbp), %rdx
leaq 0xcb83(%rip), %rcx      # 0x115a0
imulq $0x8038, %rdx, %rdx     # imm = 0x8038
addq %rdx, %rcx
addq $0x8018, %rcx           # imm = 0x8018
cmpq %rcx, %rax
je 0x4a82
movslq -0x4(%rbp), %rax
leaq 0xcb62(%rip), %rdi      # 0x115a0
imulq $0x8038, %rax, %rax     # imm = 0x8038
addq %rax, %rdi
addq $0x8018, %rdi           # imm = 0x8018
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0xa130
movslq -0x4(%rbp), %rcx
leaq 0xcb32(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
addq $0x8018, %rax           # imm = 0x8018
movq %rax, -0x10(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0xcb13(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl (%rax), %edi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x20d0
movl %eax, -0x14(%rbp)
movslq -0x14(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x4c18
cmpl $0x0, -0x14(%rbp)
jg 0x4b9e
callq 0x2050
cmpl $0xb, (%rax)
je 0x4b9c
movslq -0x4(%rbp), %rcx
leaq 0xcabb(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpl $0x10, 0x4(%rax)
je 0x4b10
movslq -0x4(%rbp), %rcx
leaq 0xcaa0(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpl $0x11, 0x4(%rax)
jne 0x4b2d
callq 0x2050
movl (%rax), %edi
callq 0x2370
movq %rax, %rsi
leaq 0x80c9(%rip), %rdi      # 0xcbef
movb $0x0, %al
callq 0x9c40
movslq -0x4(%rbp), %rcx
leaq 0xca68(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
leaq 0xc6c2(%rip), %rax      # 0x11210
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0xca3f(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x18(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2370
movq -0x20(%rbp), %rsi
movl -0x18(%rbp), %edx
movq %rax, %rcx
leaq 0x807e(%rip), %rdi      # 0xcc0b
movb $0x0, %al
callq 0x9b90
movl -0x4(%rbp), %edi
callq 0x3a30
jmp 0x4c16
movq -0x10(%rbp), %rax
movslq -0x4(%rbp), %rdx
leaq 0xc9f3(%rip), %rcx      # 0x115a0
imulq $0x8038, %rdx, %rdx     # imm = 0x8038
addq %rdx, %rcx
addq $0x8018, %rcx           # imm = 0x8018
cmpq %rcx, %rax
jne 0x4bd6
movq -0x10(%rbp), %rdi
movslq -0x14(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x9ff0
jmp 0x4c14
movslq -0x4(%rbp), %rax
leaq 0xc9bf(%rip), %rdi      # 0x115a0
imulq $0x8038, %rax, %rax     # imm = 0x8038
addq %rax, %rdi
addq $0x8018, %rdi           # imm = 0x8018
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movslq -0x14(%rbp), %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movslq -0x14(%rbp), %rax
subq %rax, %rdx
callq 0xa130
jmp 0x4c16
jmp 0x4c8a
movq -0x10(%rbp), %rax
movslq -0x4(%rbp), %rdx
leaq 0xc979(%rip), %rcx      # 0x115a0
imulq $0x8038, %rdx, %rdx     # imm = 0x8038
addq %rdx, %rcx
addq $0x8018, %rcx           # imm = 0x8018
cmpq %rcx, %rax
jne 0x4c88
movq -0x10(%rbp), %rcx
xorl %eax, %eax
cmpq 0x8(%rcx), %rax
jae 0x4c51
xorl %eax, %eax
movq %rax, -0x28(%rbp)
jmp 0x4c5d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x4c86
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movb $0x0, (%rax,%rcx)
jmp 0x4c88
jmp 0x4c8a
addq $0x30, %rsp
popq %rbp
retq