// File: flordan[P]ipc_benchmark/libboost_mq/consumer.cpp
// Function: main
int main(int argc, char *argv[])
{
    std::cout << "\t\t\t\t\tConsumer running" << std::endl;
    int pid = getpid();
    std::cout << "\t\t\t\t\tConsumer PID " << pid << std::endl;

    std::ostringstream consumer_queue_name_builder;
    consumer_queue_name_builder << "consumer" << pid;
    const char *consumer_queue_name = consumer_queue_name_builder.str().c_str();

    boost::interprocess::message_queue mq_consumer (boost::interprocess::open_or_create, consumer_queue_name, 20, 200);
    std::cout << "\t\t\t\t\tObtained consumer queue " << consumer_queue_name << "."<< std::endl;
    
    
    int mq_producer;
    unsigned int priority=0;
    std::cout << "\t\t\t\t\tSynchronizing with producer" << std::endl;
    size_t recvd_size;
    char in_buffer[200] = "";
    std::cout<<"\t\t\t\t\tWaiting for producer message on consumer queue." <<std::endl;
    mq_consumer.receive((void *)in_buffer, 200, recvd_size, priority);
    std::cout<<"\t\t\t\t\tWaiting for producer message on consumer queue." <<std::endl;
    char * mq_producer_name = in_buffer;
    std::cout << "\t\t\t\t\tProducer queue: " << mq_producer_name << std::endl;

    boost::interprocess::message_queue producer_mq (boost::interprocess::open_or_create, mq_producer_name, 20, 200);
    producer_mq.send(consumer_queue_name, std::strlen(consumer_queue_name) + 1, 0);
    std::cout << "\t\t\t\t\tSynchronized" << std::endl;

    struct timeval st;

    int max = 0;
    int min = 10000000;
    int total = 0;
    for (int i = 0; i < LATENCY_PROBES; i++)
    {
        mq_consumer.receive((void *)in_buffer, 200, recvd_size, priority);
        gettimeofday(&st, NULL);

        double send;
        std::sscanf(in_buffer, "%lf", &send);
        int send_secs = (int)send;
        int send_usec = ((send - send_secs)*10000000)-1000000;
        send = (double)send_secs+((double)send_usec/(double)1000000);
        double now = (double)st.tv_sec + ((double)st.tv_usec)/(double)1000000;
        double diff = (now - send) * 1000000;
        if (diff < 0)
        {
            diff += 1000000;
        }
        total += diff;
        if (diff > max)
        {
            max = diff;
        }
        if (diff < min)
        {
            min = diff;
        }
    }

    std::cout << "Latency test results" << std::endl;
    std::cout << "Average " << total / LATENCY_PROBES << " us" << std::endl;
    std::cout << "Max time " << max << " us" << std::endl;
    std::cout << "Min time " << min << " us" << std::endl;

    /*if (mq_close (mq_consumer) == -1) {
        std::cerr << "\t\t\t\t\tNot able to close consumer queue" << std::endl;
        return 1;
    }
    if (mq_unlink (consumer_queue_name) == -1) {
        std::cerr << "\t\t\t\t\tNot able to delete consumer queue" << std::endl;
        return 1;
    }*/
    
    std::cout << "\t\t\t\t\tFinished" << std::endl;
}

// Assembly:
subq $0x438, %rsp            # imm = 0x438
movl $0x0, 0x434(%rsp)
movl %edi, 0x430(%rsp)
movq %rsi, 0x428(%rsp)
movq 0x108f0(%rip), %rdi     # 0x13fd8
movq %rdi, 0xe0(%rsp)
leaq 0xa965(%rip), %rsi      # 0xe05c
callq 0x3250
movq %rax, %rdi
movq 0x108b2(%rip), %rsi     # 0x13fb8
movq %rsi, 0xe8(%rsp)
callq 0x32a0
callq 0x33a0
movq 0xe0(%rsp), %rdi
movl %eax, 0x424(%rsp)
leaq 0xa944(%rip), %rsi      # 0xe072
callq 0x3250
movq %rax, %rdi
movl 0x424(%rsp), %esi
callq 0x34a0
movq 0xe8(%rsp), %rsi
movq %rax, %rdi
callq 0x32a0
leaq 0x2a8(%rsp), %rdi
movq %rdi, 0xf0(%rsp)
callq 0x3330
movq 0xf0(%rsp), %rdi
leaq 0xa90f(%rip), %rsi      # 0xe085
callq 0x3250
movq %rax, 0xf8(%rsp)
jmp 0x3785
movq 0xf8(%rsp), %rdi
movl 0x424(%rsp), %esi
callq 0x34a0
jmp 0x379b
leaq 0x270(%rsp), %rdi
leaq 0x2a8(%rsp), %rsi
callq 0x3290
jmp 0x37b2
leaq 0x270(%rsp), %rdi
movq %rdi, 0xc8(%rsp)
callq 0x30e0
movq 0xc8(%rsp), %rdi
movq %rax, 0xd0(%rsp)
callq 0x3150
movq 0xd0(%rsp), %rax
movq %rax, 0x290(%rsp)
movq 0x290(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x3f50
jmp 0x380b
movq 0xd8(%rsp), %rsi
leaq 0x250(%rsp), %rdi
movl $0x14, %edx
movl $0xc8, %ecx
leaq 0x248(%rsp), %r8
callq 0x3f60
jmp 0x3834
movq 0x1079d(%rip), %rdi     # 0x13fd8
leaq 0xa84c(%rip), %rsi      # 0xe08e
callq 0x3250
movq %rax, 0xc0(%rsp)
jmp 0x3851
movq 0xc0(%rsp), %rdi
movq 0x290(%rsp), %rsi
callq 0x3250
movq %rax, 0xb8(%rsp)
jmp 0x3870
movq 0xb8(%rsp), %rdi
leaq 0xa881(%rip), %rsi      # 0xe100
callq 0x3250
movq %rax, 0xb0(%rsp)
jmp 0x388e
movq 0xb0(%rsp), %rdi
movq 0x1071b(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x38a4
movl $0x0, 0x240(%rsp)
movq 0x10722(%rip), %rdi     # 0x13fd8
leaq 0xa7ef(%rip), %rsi      # 0xe0ac
callq 0x3250
movq %rax, 0xa8(%rsp)
jmp 0x38cc
movq 0xa8(%rsp), %rdi
movq 0x106dd(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x38e2
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x220(%rsp)
movaps %xmm0, 0x210(%rsp)
movaps %xmm0, 0x200(%rsp)
movaps %xmm0, 0x1f0(%rsp)
movaps %xmm0, 0x1e0(%rsp)
movaps %xmm0, 0x1d0(%rsp)
movaps %xmm0, 0x1c0(%rsp)
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm0, 0x190(%rsp)
movaps %xmm0, 0x180(%rsp)
movaps %xmm0, 0x170(%rsp)
movq $0x0, 0x230(%rsp)
movq 0x10678(%rip), %rdi     # 0x13fd8
leaq 0xa766(%rip), %rsi      # 0xe0cd
callq 0x3250
movq %rax, 0xa0(%rsp)
jmp 0x3976
movq 0xa0(%rsp), %rdi
movq 0x10633(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x398c
leaq 0x250(%rsp), %rdi
leaq 0x170(%rsp), %rsi
movl $0xc8, %edx
leaq 0x238(%rsp), %rcx
leaq 0x240(%rsp), %r8
callq 0x3fe0
jmp 0x39b8
movq 0x10619(%rip), %rdi     # 0x13fd8
leaq 0xa707(%rip), %rsi      # 0xe0cd
callq 0x3250
movq %rax, 0x88(%rsp)
jmp 0x39d5
movq 0x88(%rsp), %rdi
movq 0x105d4(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x39eb
leaq 0x170(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x105d6(%rip), %rdi     # 0x13fd8
leaq 0xa6f9(%rip), %rsi      # 0xe102
callq 0x3250
movq %rax, 0x80(%rsp)
jmp 0x3a18
movq 0x80(%rsp), %rdi
movq 0x168(%rsp), %rsi
callq 0x3250
movq %rax, 0x78(%rsp)
jmp 0x3a34
movq 0x78(%rsp), %rdi
movq 0x10578(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x3a47
movq 0x168(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x3f50
jmp 0x3a63
movq 0x70(%rsp), %rsi
leaq 0x148(%rsp), %rdi
movl $0x14, %edx
movl $0xc8, %ecx
leaq 0x140(%rsp), %r8
callq 0x3f60
jmp 0x3a89
movq 0x290(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x30f0
movq 0x68(%rsp), %rsi
movq %rax, %rdx
incq %rdx
leaq 0x148(%rsp), %rdi
xorl %ecx, %ecx
callq 0x4070
jmp 0x3ab7
movq 0x1051a(%rip), %rdi     # 0x13fd8
leaq 0xa653(%rip), %rsi      # 0xe118
callq 0x3250
movq %rax, 0x60(%rsp)
jmp 0x3ad1
movq 0x60(%rsp), %rdi
movq 0x104db(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x3ae4
movl $0x0, 0x12c(%rsp)
movl $0x989680, 0x128(%rsp) # imm = 0x989680
movl $0x0, 0x124(%rsp)
movl $0x0, 0x120(%rsp)
cmpl $0x3e8, 0x120(%rsp) # imm = 0x3E8
jge 0x3d4f
leaq 0x250(%rsp), %rdi
leaq 0x170(%rsp), %rsi
movl $0xc8, %edx
leaq 0x238(%rsp), %rcx
leaq 0x240(%rsp), %r8
callq 0x3fe0
jmp 0x3b4d
leaq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3420
leaq 0x170(%rsp), %rdi
leaq 0xa5bd(%rip), %rsi      # 0xe12a
leaq 0x118(%rsp), %rdx
movb $0x0, %al
callq 0x3240
cvttsd2si 0x118(%rsp), %eax
movl %eax, 0x114(%rsp)
movsd 0x118(%rsp), %xmm0
cvtsi2sdl 0x114(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0xa466(%rip), %xmm1     # 0xe010
mulsd %xmm1, %xmm0
movsd 0xa462(%rip), %xmm1     # 0xe018
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x110(%rsp)
cvtsi2sdl 0x114(%rsp), %xmm0
cvtsi2sdl 0x110(%rsp), %xmm1
movsd 0xa429(%rip), %xmm2     # 0xe008
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rsp)
cvtsi2sdq 0x130(%rsp), %xmm0
cvtsi2sdq 0x138(%rsp), %xmm1
movsd 0xa3fc(%rip), %xmm2     # 0xe008
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x108(%rsp)
movsd 0x108(%rsp), %xmm0
subsd 0x118(%rsp), %xmm0
movsd 0xa3d1(%rip), %xmm1     # 0xe008
mulsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x100(%rsp), %xmm0
jbe 0x3cc6
movsd 0xa3ae(%rip), %xmm0     # 0xe008
addsd 0x100(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
jmp 0x3cc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
jmp 0x3f1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
jmp 0x3f0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a0(%rsp)
movl %eax, 0x29c(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x40e0
jmp 0x3f0d
movsd 0x100(%rsp), %xmm1
cvtsi2sdl 0x124(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x124(%rsp)
movsd 0x100(%rsp), %xmm0
cvtsi2sdl 0x12c(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3d0f
cvttsd2si 0x100(%rsp), %eax
movl %eax, 0x12c(%rsp)
movsd 0x100(%rsp), %xmm1
cvtsi2sdl 0x128(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3d37
cvttsd2si 0x100(%rsp), %eax
movl %eax, 0x128(%rsp)
jmp 0x3d39
movl 0x120(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x120(%rsp)
jmp 0x3b10
movq 0x10282(%rip), %rdi     # 0x13fd8
leaq 0xa3d1(%rip), %rsi      # 0xe12e
callq 0x3250
movq %rax, 0x58(%rsp)
jmp 0x3d69
movq 0x58(%rsp), %rdi
movq 0x10243(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x3d7c
movq 0x10255(%rip), %rdi     # 0x13fd8
leaq 0xa3b9(%rip), %rsi      # 0xe143
callq 0x3250
movq %rax, 0x50(%rsp)
jmp 0x3d96
movq 0x50(%rsp), %rdi
movslq 0x124(%rsp), %rax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rax
shrq $0x3f, %rax
sarq $0x26, %rcx
movl %ecx, %esi
addl %eax, %esi
callq 0x34a0
movq %rax, 0x48(%rsp)
jmp 0x3dc5
movq 0x48(%rsp), %rdi
leaq 0xa37b(%rip), %rsi      # 0xe14c
callq 0x3250
movq %rax, 0x40(%rsp)
jmp 0x3ddd
movq 0x40(%rsp), %rdi
movq 0x101cf(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x3df0
movq 0x101e1(%rip), %rdi     # 0x13fd8
leaq 0xa352(%rip), %rsi      # 0xe150
callq 0x3250
movq %rax, 0x38(%rsp)
jmp 0x3e0a
movq 0x38(%rsp), %rdi
movl 0x12c(%rsp), %esi
callq 0x34a0
movq %rax, 0x30(%rsp)
jmp 0x3e22
movq 0x30(%rsp), %rdi
leaq 0xa31e(%rip), %rsi      # 0xe14c
callq 0x3250
movq %rax, 0x28(%rsp)
jmp 0x3e3a
movq 0x28(%rsp), %rdi
movq 0x10172(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x3e4d
movq 0x10184(%rip), %rdi     # 0x13fd8
leaq 0xa2ff(%rip), %rsi      # 0xe15a
callq 0x3250
movq %rax, 0x20(%rsp)
jmp 0x3e67
movq 0x20(%rsp), %rdi
movl 0x128(%rsp), %esi
callq 0x34a0
movq %rax, 0x18(%rsp)
jmp 0x3e7f
movq 0x18(%rsp), %rdi
leaq 0xa2c1(%rip), %rsi      # 0xe14c
callq 0x3250
movq %rax, 0x10(%rsp)
jmp 0x3e97
movq 0x10(%rsp), %rdi
movq 0x10115(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x3eaa
movq 0x10127(%rip), %rdi     # 0x13fd8
leaq 0xa2ac(%rip), %rsi      # 0xe164
callq 0x3250
movq %rax, 0x8(%rsp)
jmp 0x3ec4
movq 0x8(%rsp), %rdi
movq 0x100e8(%rip), %rsi     # 0x13fb8
callq 0x32a0
jmp 0x3ed7
leaq 0x148(%rsp), %rdi
callq 0x40e0
leaq 0x250(%rsp), %rdi
callq 0x40e0
leaq 0x2a8(%rsp), %rdi
callq 0x3080
movl 0x434(%rsp), %eax
addq $0x438, %rsp            # imm = 0x438
retq
leaq 0x250(%rsp), %rdi
callq 0x40e0
leaq 0x2a8(%rsp), %rdi
callq 0x3080
movq 0x2a0(%rsp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)
nop