// File: /workspace/github_cpp_makefile_star2_O1/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 0x2531a(%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 0x7760
cmpl $0x10001, %ebp          # imm = 0x10001
jae 0x20677
movl %ebp, %r15d
movq %r14, %rdi
movq %r15, %rsi
callq 0x21bdc
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x21c72
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %r13d, %edi
movl $0x1, %esi
movl $0x9, %edx
movl $0x4, %r8d
callq 0x79d0
cmpl $-0x1, %eax
je 0x206a9
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %r13d, %edi
movl $0x6, %esi
movl $0x3, %edx
movl $0x4, %r8d
callq 0x79d0
cmpl $-0x1, %eax
je 0x206b2
leaq 0x4(%rsp), %rcx
movl $0x12c, (%rcx)          # imm = 0x12C
movl %r13d, %edi
movl $0x6, %esi
movl $0x4, %edx
movl $0x4, %r8d
callq 0x79d0
cmpl $-0x1, %eax
je 0x206bb
leaq 0x4(%rsp), %rcx
movl $0x5, (%rcx)
movl %r13d, %edi
movl $0x6, %esi
movl $0x5, %edx
movl $0x4, %r8d
callq 0x79d0
cmpl $-0x1, %eax
je 0x206c4
testl %ebp, %ebp
je 0x2060b
leaq 0x10(%rsp), %r13
movl $0x100000, %edi         # imm = 0x100000
callq 0x76e0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2061a
movq 0x60(%rbx), %rsi
cmpq 0x68(%rbx), %rsi
je 0x205fc
movq %rax, (%rsi)
addq $0x8, 0x60(%rbx)
jmp 0x20607
movq %r14, %rdi
movq %r13, %rdx
callq 0x21e9c
decl %ebp
jne 0x205d4
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 0x20635
movq (%r15), %rdi
callq 0x75b0
addq $0x8, %r15
jmp 0x20622
movq 0x58(%rbx), %rax
cmpq %rax, 0x60(%rbx)
je 0x20643
movq %rax, 0x60(%rbx)
movl $0x10, %edi
callq 0x7220
movq %rax, %rbp
leaq 0x16127(%rip), %rsi     # 0x3677e
movq %rax, %rdi
callq 0x7190
movq 0x2598a(%rip), %rsi     # 0x45ff0
movq 0x25903(%rip), %rdx     # 0x45f70
movq %rbp, %rdi
callq 0x7970
jmp 0x206a9
movl $0x10, %edi
callq 0x7220
movq %rax, %rbp
leaq 0x16075(%rip), %rsi     # 0x36700
movq %rax, %rdi
callq 0x77a0
movq 0x2591e(%rip), %rsi     # 0x45fb8
movq 0x258ef(%rip), %rdx     # 0x45f90
movq %rbp, %rdi
callq 0x7970
leaq 0x1606d(%rip), %r13     # 0x3671d
jmp 0x206cb
leaq 0x1607d(%rip), %r13     # 0x36736
jmp 0x206cb
leaq 0x16089(%rip), %r13     # 0x3674b
jmp 0x206cb
leaq 0x16099(%rip), %r13     # 0x36764
callq 0x7040
movl (%rax), %edi
movq %r13, %rsi
callq 0x8dd3
jmp 0x206e7
jmp 0x206e2
jmp 0x206e2
jmp 0x206e2
movq %rax, %r13
jmp 0x206f2
movq %rax, %r13
movq %rbp, %rdi
callq 0x7370
movq %r12, %rdi
callq 0x7a00
movq 0x88(%rbx), %rdi
testq %rdi, %rdi
je 0x20715
movq 0x98(%rbx), %rsi
subq %rdi, %rsi
callq 0x7510
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x20731
movq 0x80(%rbx), %rsi
subq %rdi, %rsi
callq 0x7510
movq (%r14), %rdi
testq %rdi, %rdi
je 0x20745
movq 0x68(%rbx), %rsi
subq %rdi, %rsi
callq 0x7510
movq %rbx, %rdi
callq 0x12866
movq %r13, %rdi
callq 0x79b0
nop