// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %eax, %eax
movq %rax, 0x8(%rdi)
movw $0x1, 0x10(%rdi)
movq %rax, 0x48(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x38(%rdi)
movl $0x2, 0x48(%rdi)
movl %esi, 0x50(%rdi)
leaq 0x25b3c(%rip), %rcx     # 0x457c0
movq %rcx, (%rdi)
leaq 0x58(%rdi), %r14
leaq 0x70(%rdi), %rcx
movq %rcx, 0x8(%rsp)
movups %xmm0, 0xa8(%rdi)
movups %xmm0, 0xb8(%rdi)
movq %rax, 0xc8(%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 0x7750
cmpl $0x10001, %ebp          # imm = 0x10001
jae 0x1fe58
movl %ebp, %r15d
movq %r14, %rdi
movq %r15, %rsi
callq 0x212a8
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x21344
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %r13d, %edi
movl $0x1, %esi
movl $0x9, %edx
movl $0x4, %r8d
callq 0x79c0
cmpl $-0x1, %eax
je 0x1fe8a
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %r13d, %edi
movl $0x6, %esi
movl $0x3, %edx
movl $0x4, %r8d
callq 0x79c0
cmpl $-0x1, %eax
je 0x1fe93
leaq 0x4(%rsp), %rcx
movl $0x12c, (%rcx)          # imm = 0x12C
movl %r13d, %edi
movl $0x6, %esi
movl $0x4, %edx
movl $0x4, %r8d
callq 0x79c0
cmpl $-0x1, %eax
je 0x1fe9c
leaq 0x4(%rsp), %rcx
movl $0x5, (%rcx)
movl %r13d, %edi
movl $0x6, %esi
movl $0x5, %edx
movl $0x4, %r8d
callq 0x79c0
cmpl $-0x1, %eax
je 0x1fea5
testl %ebp, %ebp
je 0x1fde9
leaq 0x10(%rsp), %r13
movl $0x100000, %edi         # imm = 0x100000
callq 0x76d0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1fdf8
movq 0x60(%rbx), %rsi
cmpq 0x68(%rbx), %rsi
je 0x1fdda
movq %rax, (%rsi)
addq $0x8, 0x60(%rbx)
jmp 0x1fde5
movq %r14, %rdi
movq %r13, %rdx
callq 0x21560
decl %ebp
jne 0x1fdb2
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 0x1fe24
movq (%r15), %rdi
callq 0x75a0
addq $0x8, %r15
cmpq %r13, %r15
jne 0x1fe05
movq 0x58(%rbx), %rax
cmpq %rax, 0x60(%rbx)
je 0x1fe24
movq %rax, 0x60(%rbx)
movl $0x10, %edi
callq 0x7220
movq %rax, %rbp
leaq 0x1690c(%rip), %rsi     # 0x36744
movq %rax, %rdi
callq 0x7190
movq 0x261a9(%rip), %rsi     # 0x45ff0
movq 0x26122(%rip), %rdx     # 0x45f70
movq %rbp, %rdi
callq 0x7960
jmp 0x1fe8a
movl $0x10, %edi
callq 0x7220
movq %rax, %rbp
leaq 0x1685a(%rip), %rsi     # 0x366c6
movq %rax, %rdi
callq 0x7790
movq 0x2613d(%rip), %rsi     # 0x45fb8
movq 0x2610e(%rip), %rdx     # 0x45f90
movq %rbp, %rdi
callq 0x7960
leaq 0x16852(%rip), %r13     # 0x366e3
jmp 0x1feac
leaq 0x16862(%rip), %r13     # 0x366fc
jmp 0x1feac
leaq 0x1686e(%rip), %r13     # 0x36711
jmp 0x1feac
leaq 0x1687e(%rip), %r13     # 0x3672a
callq 0x7040
movl (%rax), %edi
movq %r13, %rsi
callq 0x8d8d
jmp 0x1fec8
jmp 0x1fec3
jmp 0x1fec3
jmp 0x1fec3
movq %rax, %r13
jmp 0x1fed3
movq %rax, %r13
movq %rbp, %rdi
callq 0x7370
movq %r12, %rdi
callq 0x79f0
movq 0x88(%rbx), %rdi
testq %rdi, %rdi
je 0x1fef6
movq 0x98(%rbx), %rsi
subq %rdi, %rsi
callq 0x7500
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x1ff12
movq 0x80(%rbx), %rsi
subq %rdi, %rsi
callq 0x7500
movq (%r14), %rdi
testq %rdi, %rdi
je 0x1ff26
movq 0x68(%rbx), %rsi
subq %rdi, %rsi
callq 0x7500
movq %rbx, %rdi
callq 0x128aa
movq %r13, %rdi
callq 0x79a0