// File: /workspace/github_cpp_makefile_star2_O2/cybozu[P]yrmcds/cybozu/tcp.cpp
// Function: cybozu::tcp_socket::tcp_socket(int, unsigned int)
tcp_socket::tcp_socket(int fd, unsigned int bufcnt):
    resource(fd) {
    if( bufcnt > MAX_BUFCNT )
        throw std::logic_error("tcp_socket: Too many buffers");
    m_free_buffers.reserve(bufcnt);
    m_pending.reserve(bufcnt);

    int v = 1;
    if( setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &v, sizeof(v)) == -1 )
        throw_unix_error(errno, "setsockopt(SO_KEEPALIVE)");

    v = 1;
    if( setsockopt(fd, IPPROTO_TCP, TCP_CORK, &v, sizeof(v)) == -1 )
        throw_unix_error(errno, "setsockopt(TCP_CORK)");

    v = KEEPALIVE_IDLE;
    if( setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &v, sizeof(v)) == -1 )
        throw_unix_error(errno, "setsockopt(TCP_KEEPIDLE)");

    v = KEEPALIVE_INTERVAL;
    if( setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &v, sizeof(v)) == -1 )
        throw_unix_error(errno, "setsockopt(TCP_KEEPINTVL)");

    for( unsigned int i = 0; i < bufcnt; ++i ) {
        char* p = (char*)MALLOC(SENDBUF_SIZE);
        if( p == NULL ) {
            for( char* p2: m_free_buffers )
                FREE(p2);
            m_free_buffers.clear();
            throw std::runtime_error("tcp_socket: failed to allocate buffers");
        }
        // reserved, hence no throw
        m_free_buffers.push_back(p);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movl %esi, %r13d
movq %rdi, %rbx
andq $0x0, 0x8(%rdi)
movw $0x1, 0x10(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x38(%rdi)
andq $0x0, 0x48(%rdi)
movl $0x2, 0x48(%rdi)
movl %esi, 0x50(%rdi)
leaq 0x1ea84(%rip), %rax     # 0x3a7c0
movq %rax, (%rdi)
leaq 0x58(%rdi), %r14
leaq 0x70(%rdi), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0xc8(%rdi)
movups %xmm0, 0xa8(%rdi)
movups %xmm0, 0xb8(%rdi)
leaq 0xd0(%rdi), %r12
movups %xmm0, 0x58(%rdi)
movups %xmm0, 0x68(%rdi)
movups %xmm0, 0x78(%rdi)
movups %xmm0, 0x88(%rdi)
movups %xmm0, 0x91(%rdi)
movq %r12, %rdi
callq 0x7770
cmpl $0x10001, %ebp          # imm = 0x10001
jae 0x1bede
movl %ebp, %r15d
movq %r14, %rdi
movq %r15, %rsi
callq 0x1ce54
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x1ceea
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x9
popq %rdx
pushq $0x4
popq %r8
movl %r13d, %edi
callq 0x79b0
cmpl $-0x1, %eax
je 0x1bf0e
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x6
popq %rsi
pushq $0x3
popq %rdx
pushq $0x4
popq %r8
movl %r13d, %edi
callq 0x79b0
cmpl $-0x1, %eax
je 0x1bf17
leaq 0x4(%rsp), %rcx
movl $0x12c, (%rcx)          # imm = 0x12C
pushq $0x6
popq %rsi
pushq $0x4
popq %rdx
movl %r13d, %edi
movl %edx, %r8d
callq 0x79b0
cmpl $-0x1, %eax
je 0x1bf20
leaq 0x4(%rsp), %rcx
movl $0x5, (%rcx)
pushq $0x6
popq %rsi
pushq $0x5
popq %rdx
pushq $0x4
popq %r8
movl %r13d, %edi
callq 0x79b0
cmpl $-0x1, %eax
je 0x1bf29
leaq 0x10(%rsp), %r13
subl $0x1, %ebp
jb 0x1be74
movl $0x100000, %edi         # imm = 0x100000
callq 0x76f0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1be83
movq %r14, %rdi
movq %r13, %rsi
callq 0x1cf8c
jmp 0x1be4e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x58(%rbx), %r15
movq 0x60(%rbx), %r13
cmpq %r13, %r15
je 0x1be9e
movq (%r15), %rdi
callq 0x75b0
addq $0x8, %r15
jmp 0x1be8b
movq 0x58(%rbx), %rax
cmpq %rax, 0x60(%rbx)
je 0x1beac
movq %rax, 0x60(%rbx)
pushq $0x10
popq %rdi
callq 0x7240
movq %rax, %rbp
leaq 0xc816(%rip), %rsi      # 0x286d4
movq %rax, %rdi
callq 0x7190
movq 0x1f123(%rip), %rsi     # 0x3aff0
movq 0x1f09c(%rip), %rdx     # 0x3af70
movq %rbp, %rdi
callq 0x7950
jmp 0x1bf0e
pushq $0x10
popq %rdi
callq 0x7240
movq %rax, %rbp
leaq 0xc766(%rip), %rsi      # 0x28656
movq %rax, %rdi
callq 0x77a0
movq 0x1f0c1(%rip), %rsi     # 0x3afc0
movq 0x1f082(%rip), %rdx     # 0x3af88
movq %rbp, %rdi
callq 0x7950
leaq 0xc75e(%rip), %r13      # 0x28673
jmp 0x1bf30
leaq 0xc76e(%rip), %r13      # 0x2868c
jmp 0x1bf30
leaq 0xc77a(%rip), %r13      # 0x286a1
jmp 0x1bf30
leaq 0xc78a(%rip), %r13      # 0x286ba
callq 0x7030
movl (%rax), %edi
movq %r13, %rsi
callq 0x8513
jmp 0x1bf47
jmp 0x1bf54
jmp 0x1bf54
jmp 0x1bf54
movq %rax, %r13
movq %rbp, %rdi
callq 0x7390
jmp 0x1bf57
movq %rax, %r13
leaq 0x88(%rbx), %r15
movq %r12, %rdi
callq 0x79f0
movq %r15, %rdi
callq 0x11e84
movq 0x8(%rsp), %rdi
callq 0x12f5a
movq %r14, %rdi
callq 0x12f96
movq %rbx, %rdi
callq 0x11048
movq %r13, %rdi
callq 0x7990