// File: romkatv[P]hcproxy/src/forwarder.cc
// Function: hcproxy::(anonymous namespace)::Buffer::ReadTo(int)
IoStatus ReadTo(int fd) {
    CHECK(pipe_[0] >= 0);
    CHECK(size_ >= 0);
    if (size_ == 0) {
      if (pipe_[1] >= 0) return IoStatus::kNoOp;
      CHECK(close(pipe_[0]) == 0) << Errno();
      pipe_[0] = -1;
      return IoStatus::kEof;
    }
    // There is a bug in splice() in WSL that makes it clear the pipe upon returning an error, be it
    // EAGAIN or something else. To work around it, we do two things. First, we issue a zero-byte
    // write to check whether the socket is writable and thus to reduce the chance splice() will
    // return EAGAIN. Second, we terminate the connection if we trigger the bug in splice().
    if (write(fd, "", 0) == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
      return IoStatus::kNoOp;
    }
    ssize_t ret = splice(pipe_[0], nullptr, fd, nullptr, size_, SPLICE_F_NONBLOCK | SPLICE_F_MOVE);
    CHECK(ret != 0);
    if (ret < 0) {
      if (errno == EAGAIN) {
        int len = 0;
        CHECK(ioctl(pipe_[0], FIONREAD, &len) == 0) << Errno();
        if (len == size_) return IoStatus::kNoOp;
      }
      CHECK(close(pipe_[0]) == 0) << Errno();
      pipe_[0] = -1;
      return IoStatus::kError;
    }
    size_ -= ret;
    CHECK(size_ >= 0);
    CHECK(size_ <= capacity_);
    return IoStatus::kData;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
movl 0x8(%rax), %eax
movb $0x0, -0x39(%rbp)
cmpl $0x0, %eax
jl 0x30a3b
jmp 0x30ab0
leaq 0xb453a(%rip), %rsi     # 0xe4f7c
leaq -0x38(%rbp), %rdi
movq %rdi, -0x188(%rbp)
movl $0x6d, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x188(%rbp), %rdi
movb $0x1, -0x39(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb466d(%rip), %rsi     # 0xe50e3
callq 0x19590
movq %rax, -0x180(%rbp)
jmp 0x30a84
movq -0x180(%rbp), %rdi
leaq 0xb88f9(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x190(%rbp)
jmp 0x30aa0
movq -0x190(%rbp), %rsi
leaq -0x4d(%rbp), %rdi
callq 0x194f0
testb $0x1, -0x39(%rbp)
jne 0x30ab8
jmp 0x30adc
leaq -0x38(%rbp), %rdi
callq 0x19500
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0x39(%rbp)
jne 0x30b77
jmp 0x30b80
movq -0x178(%rbp), %rax
movl 0x4(%rax), %eax
movb $0x0, -0x71(%rbp)
cmpl $0x0, %eax
jl 0x30af1
jmp 0x30b66
leaq 0xb4484(%rip), %rsi     # 0xe4f7c
leaq -0x70(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
movl $0x6e, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x1a0(%rbp), %rdi
movb $0x1, -0x71(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb4534(%rip), %rsi     # 0xe5060
callq 0x1c330
movq %rax, -0x198(%rbp)
jmp 0x30b3a
movq -0x198(%rbp), %rdi
leaq 0xb8843(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x1a8(%rbp)
jmp 0x30b56
movq -0x1a8(%rbp), %rsi
leaq -0x72(%rbp), %rdi
callq 0x194f0
testb $0x1, -0x71(%rbp)
jne 0x30b6e
jmp 0x30b99
leaq -0x70(%rbp), %rdi
callq 0x19500
leaq -0x38(%rbp), %rdi
callq 0x19500
jmp 0x31253
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0x71(%rbp)
jne 0x30bc3
jmp 0x30bcc
movq -0x178(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x30ce4
movq -0x178(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jl 0x30bd1
movl $0x3, -0x4(%rbp)
jmp 0x31247
leaq -0x70(%rbp), %rdi
callq 0x19500
jmp 0x31253
movq -0x178(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x15140
movb $0x0, -0x99(%rbp)
cmpl $0x0, %eax
jne 0x30bf1
jmp 0x30c8b
leaq 0xb4384(%rip), %rsi     # 0xe4f7c
leaq -0x98(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
movl $0x71, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x1b8(%rbp), %rdi
movb $0x1, -0x99(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb44bf(%rip), %rsi     # 0xe50f1
callq 0x1c400
movq %rax, -0x1b0(%rbp)
jmp 0x30c40
movq -0x1b0(%rbp), %rdi
leaq 0xb873d(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x1c0(%rbp)
jmp 0x30c5c
callq 0x1c0c0
jmp 0x30c63
movq -0x1c0(%rbp), %rdi
callq 0x1c050
movq %rax, -0x1c8(%rbp)
jmp 0x30c78
movq -0x1c8(%rbp), %rsi
leaq -0x9c(%rbp), %rdi
callq 0x194f0
testb $0x1, -0x99(%rbp)
jne 0x30c96
jmp 0x30cb9
leaq -0x98(%rbp), %rdi
callq 0x19500
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0x99(%rbp)
jne 0x30cd3
jmp 0x30cdf
movq -0x178(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x1, -0x4(%rbp)
jmp 0x31247
leaq -0x98(%rbp), %rdi
callq 0x19500
jmp 0x31253
movl -0x14(%rbp), %edi
leaq 0xb3fc2(%rip), %rsi     # 0xe4cb0
xorl %eax, %eax
movl %eax, %edx
callq 0x158a0
cmpq $-0x1, %rax
jne 0x30d1d
callq 0x15640
cmpl $0xb, (%rax)
je 0x30d11
callq 0x15640
cmpl $0xb, (%rax)
jne 0x30d1d
movl $0x3, -0x4(%rbp)
jmp 0x31247
movq -0x178(%rbp), %rax
movl 0x8(%rax), %edi
movl -0x14(%rbp), %edx
movslq 0x4(%rax), %r8
xorl %eax, %eax
movl %eax, %ecx
movl $0x3, %r9d
movq %rcx, %rsi
callq 0x151d0
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movb $0x0, -0xc9(%rbp)
cmpq $0x0, %rax
je 0x30d5d
jmp 0x30ddb
leaq 0xb4218(%rip), %rsi     # 0xe4f7c
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
movl $0x7d, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x1d8(%rbp), %rdi
movb $0x1, -0xc9(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb4368(%rip), %rsi     # 0xe5106
callq 0x1b110
movq %rax, -0x1d0(%rbp)
jmp 0x30dac
movq -0x1d0(%rbp), %rdi
leaq 0xb85d1(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x1e0(%rbp)
jmp 0x30dc8
movq -0x1e0(%rbp), %rsi
leaq -0xca(%rbp), %rdi
callq 0x194f0
testb $0x1, -0xc9(%rbp)
jne 0x30de6
jmp 0x30e10
leaq -0xc8(%rbp), %rdi
callq 0x19500
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0xc9(%rbp)
jne 0x30f15
jmp 0x30f21
cmpq $0x0, -0xa8(%rbp)
jge 0x31081
callq 0x15640
cmpl $0xb, (%rax)
jne 0x30f6e
movq -0x178(%rbp), %rax
movl $0x0, -0xd0(%rbp)
movl 0x8(%rax), %edi
movl $0x541b, %esi           # imm = 0x541B
leaq -0xd0(%rbp), %rdx
movb $0x0, %al
callq 0x15160
movb $0x0, -0xf1(%rbp)
cmpl $0x0, %eax
jne 0x30e64
jmp 0x30efe
leaq 0xb4111(%rip), %rsi     # 0xe4f7c
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
movl $0x81, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x1f0(%rbp), %rdi
movb $0x1, -0xf1(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb426a(%rip), %rsi     # 0xe510f
callq 0x1c900
movq %rax, -0x1e8(%rbp)
jmp 0x30eb3
movq -0x1e8(%rbp), %rdi
leaq 0xb84ca(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x1f8(%rbp)
jmp 0x30ecf
callq 0x1c0c0
jmp 0x30ed6
movq -0x1f8(%rbp), %rdi
callq 0x1c050
movq %rax, -0x200(%rbp)
jmp 0x30eeb
movq -0x200(%rbp), %rsi
leaq -0xf4(%rbp), %rdi
callq 0x194f0
testb $0x1, -0xf1(%rbp)
jne 0x30f09
jmp 0x30f3d
leaq -0xf0(%rbp), %rdi
callq 0x19500
leaq -0xc8(%rbp), %rdi
callq 0x19500
jmp 0x31253
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0xf1(%rbp)
jne 0x30f5b
jmp 0x30f67
movq -0x178(%rbp), %rcx
movl -0xd0(%rbp), %eax
cmpl 0x4(%rcx), %eax
jne 0x30f6c
movl $0x3, -0x4(%rbp)
jmp 0x31247
leaq -0xf0(%rbp), %rdi
callq 0x19500
jmp 0x31253
jmp 0x30f6e
movq -0x178(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x15140
movb $0x0, -0x119(%rbp)
cmpl $0x0, %eax
jne 0x30f8e
jmp 0x31028
leaq 0xb3fe7(%rip), %rsi     # 0xe4f7c
leaq -0x118(%rbp), %rdi
movq %rdi, -0x210(%rbp)
movl $0x84, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x210(%rbp), %rdi
movb $0x1, -0x119(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb4122(%rip), %rsi     # 0xe50f1
callq 0x1c400
movq %rax, -0x208(%rbp)
jmp 0x30fdd
movq -0x208(%rbp), %rdi
leaq 0xb83a0(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x218(%rbp)
jmp 0x30ff9
callq 0x1c0c0
jmp 0x31000
movq -0x218(%rbp), %rdi
callq 0x1c050
movq %rax, -0x220(%rbp)
jmp 0x31015
movq -0x220(%rbp), %rsi
leaq -0x11c(%rbp), %rdi
callq 0x194f0
testb $0x1, -0x119(%rbp)
jne 0x31033
jmp 0x31056
leaq -0x118(%rbp), %rdi
callq 0x19500
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0x119(%rbp)
jne 0x31070
jmp 0x3107c
movq -0x178(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x2, -0x4(%rbp)
jmp 0x31247
leaq -0x118(%rbp), %rdi
callq 0x19500
jmp 0x31253
movq -0x178(%rbp), %rax
movq -0xa8(%rbp), %rdx
movslq 0x4(%rax), %rcx
subq %rdx, %rcx
movl %ecx, 0x4(%rax)
movl 0x4(%rax), %eax
movb $0x0, -0x141(%rbp)
cmpl $0x0, %eax
jl 0x310aa
jmp 0x31128
leaq 0xb3ecb(%rip), %rsi     # 0xe4f7c
leaq -0x140(%rbp), %rdi
movq %rdi, -0x230(%rbp)
movl $0x89, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x230(%rbp), %rdi
movb $0x1, -0x141(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb3f75(%rip), %rsi     # 0xe5060
callq 0x1c330
movq %rax, -0x228(%rbp)
jmp 0x310f9
movq -0x228(%rbp), %rdi
leaq 0xb8284(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x238(%rbp)
jmp 0x31115
movq -0x238(%rbp), %rsi
leaq -0x142(%rbp), %rdi
callq 0x194f0
testb $0x1, -0x141(%rbp)
jne 0x31133
jmp 0x3115d
leaq -0x140(%rbp), %rdi
callq 0x19500
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0x141(%rbp)
jne 0x3120b
jmp 0x31217
movq -0x178(%rbp), %rcx
movl 0x4(%rcx), %eax
movl (%rcx), %ecx
movb $0x0, -0x169(%rbp)
cmpl %ecx, %eax
jg 0x31176
jmp 0x311f4
leaq 0xb3dff(%rip), %rsi     # 0xe4f7c
leaq -0x168(%rbp), %rdi
movq %rdi, -0x248(%rbp)
movl $0x8a, %edx
movl $0x3, %ecx
callq 0x194b0
movq -0x248(%rbp), %rdi
movb $0x1, -0x169(%rbp)
callq 0x194e0
movq %rax, %rdi
leaq 0xb3eb4(%rip), %rsi     # 0xe506b
callq 0x1d970
movq %rax, -0x240(%rbp)
jmp 0x311c5
movq -0x240(%rbp), %rdi
leaq 0xb81b8(%rip), %rsi     # 0xe938b
callq 0x19450
movq %rax, -0x250(%rbp)
jmp 0x311e1
movq -0x250(%rbp), %rsi
leaq -0x16a(%rbp), %rdi
callq 0x194f0
testb $0x1, -0x169(%rbp)
jne 0x311ff
jmp 0x31230
leaq -0x168(%rbp), %rdi
callq 0x19500
leaq -0x140(%rbp), %rdi
callq 0x19500
jmp 0x31253
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
testb $0x1, -0x169(%rbp)
jne 0x31239
jmp 0x31245
movl $0x0, -0x4(%rbp)
jmp 0x31247
leaq -0x168(%rbp), %rdi
callq 0x19500
jmp 0x31253
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0xe1080
nopl (%rax)