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

    std::ostringstream producer_queue_name_builder;
    producer_queue_name_builder << "producer" << pid;
    const char *producer_queue_name = producer_queue_name_builder.str().c_str();

    boost::interprocess::message_queue producer_mq(boost::interprocess::open_or_create, producer_queue_name, 20, 200);
    std::cout << "Obtained producer queue." << std::endl;

    char *consumer_pid = argv[1];
    std::cout << "Synchronizing with consumer " << consumer_pid << std::endl;

    std::ostringstream consumer_queue_name_builder;
    consumer_queue_name_builder << "consumer" << consumer_pid;
    const char *consumer_queue_name = consumer_queue_name_builder.str().c_str();
    boost::interprocess::message_queue consumer_mq(boost::interprocess::open_or_create, consumer_queue_name, 20, 200);

    std::cout << "Obtained consumer queue " << consumer_queue_name << std::endl;
    consumer_mq.send(producer_queue_name, std::strlen(producer_queue_name) + 1, 0);

    char in_buffer[200] = "";
    size_t recvd_size;
    unsigned int priority = 0;
    producer_mq.receive(in_buffer, 200, recvd_size, priority);
    boost::interprocess::message_queue::remove(producer_queue_name);
    std::cout << "Synchronized" << std::endl;

    std::cout << "Latency test" << std::endl;
    struct timeval st;
    for (int i = 0; i < LATENCY_PROBES; i++)
    {
        gettimeofday(&st, NULL);
        std::ostringstream os;
        os << st.tv_sec << "." << st.tv_usec + 1000000;
        std::string mystr = os.str();
        const char *time = mystr.c_str();
        consumer_mq.send(time, std::strlen(time) + 1, 0);
        usleep(250000);
    }

    std::cout << "Finished" << std::endl;
}

// Assembly:
subq $0x718, %rsp            # imm = 0x718
movl $0x0, 0x714(%rsp)
movl %edi, 0x710(%rsp)
movq %rsi, 0x708(%rsp)
movq 0x108d0(%rip), %rdi     # 0x13fd8
movq %rdi, 0xa8(%rsp)
leaq 0xa929(%rip), %rsi      # 0xe040
callq 0x3240
movq %rax, %rdi
movq 0x10892(%rip), %rsi     # 0x13fb8
movq %rsi, 0xb0(%rsp)
callq 0x3290
callq 0x33a0
movq 0xa8(%rsp), %rdi
movl %eax, 0x704(%rsp)
leaq 0xa903(%rip), %rsi      # 0xe051
callq 0x3240
movq %rax, %rdi
movl 0x704(%rsp), %esi
callq 0x34a0
movq 0xb0(%rsp), %rsi
movq %rax, %rdi
callq 0x3290
leaq 0x588(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
callq 0x3320
movq 0xb8(%rsp), %rdi
leaq 0xa8c9(%rip), %rsi      # 0xe05f
callq 0x3240
movq %rax, 0xc0(%rsp)
jmp 0x37a5
movq 0xc0(%rsp), %rdi
movl 0x704(%rsp), %esi
callq 0x34a0
jmp 0x37bb
leaq 0x550(%rsp), %rdi
leaq 0x588(%rsp), %rsi
callq 0x3280
jmp 0x37d2
leaq 0x550(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x30e0
movq 0x90(%rsp), %rdi
movq %rax, 0x98(%rsp)
callq 0x3150
movq 0x98(%rsp), %rax
movq %rax, 0x570(%rsp)
movq 0x570(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0x528(%rsp), %rdi
callq 0x3dc0
jmp 0x382b
movq 0xa0(%rsp), %rsi
leaq 0x530(%rsp), %rdi
movl $0x14, %edx
movl $0xc8, %ecx
leaq 0x528(%rsp), %r8
callq 0x3dd0
jmp 0x3854
movq 0x1077d(%rip), %rdi     # 0x13fd8
leaq 0xa806(%rip), %rsi      # 0xe068
callq 0x3240
movq %rax, 0x88(%rsp)
jmp 0x3871
movq 0x88(%rsp), %rdi
movq 0x10738(%rip), %rsi     # 0x13fb8
callq 0x3290
jmp 0x3887
movq 0x708(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x520(%rsp)
movq 0x10736(%rip), %rdi     # 0x13fd8
leaq 0xa7d8(%rip), %rsi      # 0xe081
callq 0x3240
movq %rax, 0x80(%rsp)
jmp 0x38b8
movq 0x80(%rsp), %rdi
movq 0x520(%rsp), %rsi
callq 0x3240
movq %rax, 0x78(%rsp)
jmp 0x38d4
movq 0x78(%rsp), %rdi
movq 0x106d8(%rip), %rsi     # 0x13fb8
callq 0x3290
jmp 0x38e7
leaq 0x3a8(%rsp), %rdi
callq 0x3320
jmp 0x38f6
leaq 0xa7a1(%rip), %rsi      # 0xe09e
leaq 0x3a8(%rsp), %rdi
callq 0x3240
movq %rax, 0x70(%rsp)
jmp 0x3911
movq 0x70(%rsp), %rdi
movq 0x520(%rsp), %rsi
callq 0x3240
jmp 0x3925
leaq 0x380(%rsp), %rdi
leaq 0x3a8(%rsp), %rsi
callq 0x3280
jmp 0x393c
leaq 0x380(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x30e0
movq 0x58(%rsp), %rdi
movq %rax, 0x60(%rsp)
callq 0x3150
movq 0x60(%rsp), %rax
movq %rax, 0x3a0(%rsp)
movq 0x3a0(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x358(%rsp), %rdi
callq 0x3dc0
jmp 0x3986
movq 0x68(%rsp), %rsi
leaq 0x360(%rsp), %rdi
movl $0x14, %edx
movl $0xc8, %ecx
leaq 0x358(%rsp), %r8
callq 0x3dd0
jmp 0x39ac
movq 0x10625(%rip), %rdi     # 0x13fd8
leaq 0xa6ed(%rip), %rsi      # 0xe0a7
callq 0x3240
movq %rax, 0x50(%rsp)
jmp 0x39c6
movq 0x50(%rsp), %rdi
movq 0x3a0(%rsp), %rsi
callq 0x3240
movq %rax, 0x48(%rsp)
jmp 0x39df
movq 0x48(%rsp), %rdi
movq 0x105cd(%rip), %rsi     # 0x13fb8
callq 0x3290
jmp 0x39f2
movq 0x570(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x30f0
movq 0x40(%rsp), %rsi
movq %rax, %rdx
incq %rdx
leaq 0x360(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3e50
jmp 0x3a20
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x340(%rsp)
movaps %xmm0, 0x330(%rsp)
movaps %xmm0, 0x320(%rsp)
movaps %xmm0, 0x310(%rsp)
movaps %xmm0, 0x300(%rsp)
movaps %xmm0, 0x2f0(%rsp)
movaps %xmm0, 0x2e0(%rsp)
movaps %xmm0, 0x2d0(%rsp)
movaps %xmm0, 0x2c0(%rsp)
movaps %xmm0, 0x2b0(%rsp)
movaps %xmm0, 0x2a0(%rsp)
movaps %xmm0, 0x290(%rsp)
movq $0x0, 0x350(%rsp)
movl $0x0, 0x284(%rsp)
leaq 0x530(%rsp), %rdi
leaq 0x290(%rsp), %rsi
movl $0xc8, %edx
leaq 0x288(%rsp), %rcx
leaq 0x284(%rsp), %r8
callq 0x3ec0
jmp 0x3acb
movq 0x570(%rsp), %rdi
callq 0x3f50
jmp 0x3ada
movq 0x104f7(%rip), %rdi     # 0x13fd8
leaq 0xa5d8(%rip), %rsi      # 0xe0c0
callq 0x3240
movq %rax, 0x28(%rsp)
jmp 0x3af4
movq 0x28(%rsp), %rdi
movq 0x104b8(%rip), %rsi     # 0x13fb8
callq 0x3290
jmp 0x3b07
movq 0x104ca(%rip), %rdi     # 0x13fd8
leaq 0xa5b8(%rip), %rsi      # 0xe0cd
callq 0x3240
movq %rax, 0x20(%rsp)
jmp 0x3b21
movq 0x20(%rsp), %rdi
movq 0x1048b(%rip), %rsi     # 0x13fb8
callq 0x3290
jmp 0x3b34
movl $0x0, 0x26c(%rsp)
cmpl $0x3e8, 0x26c(%rsp) # imm = 0x3E8
jge 0x3cff
xorl %eax, %eax
movl %eax, %esi
leaq 0x270(%rsp), %rdi
callq 0x3420
leaq 0xf0(%rsp), %rdi
callq 0x3320
jmp 0x3b70
movq 0x270(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x34f0
movq %rax, 0x18(%rsp)
jmp 0x3b8c
movq 0x18(%rsp), %rdi
leaq 0xa4e7(%rip), %rsi      # 0xe07f
callq 0x3240
movq %rax, 0x10(%rsp)
jmp 0x3ba4
movq 0x10(%rsp), %rdi
movq 0x278(%rsp), %rsi
addq $0xf4240, %rsi          # imm = 0xF4240
callq 0x34f0
jmp 0x3bbf
leaq 0xd0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x3280
jmp 0x3bd6
leaq 0xd0(%rsp), %rdi
callq 0x30e0
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x30f0
movq 0x8(%rsp), %rsi
movq %rax, %rdx
incq %rdx
leaq 0x360(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3e50
jmp 0x3c19
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x3370
jmp 0x3c25
leaq 0xd0(%rsp), %rdi
callq 0x3150
leaq 0xf0(%rsp), %rdi
callq 0x3080
movl 0x26c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x26c(%rsp)
jmp 0x3b3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x580(%rsp)
movl %eax, 0x57c(%rsp)
jmp 0x3d94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x580(%rsp)
movl %eax, 0x57c(%rsp)
jmp 0x3d87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x580(%rsp)
movl %eax, 0x57c(%rsp)
jmp 0x3d7a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x580(%rsp)
movl %eax, 0x57c(%rsp)
jmp 0x3d6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x580(%rsp)
movl %eax, 0x57c(%rsp)
jmp 0x3cf0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x580(%rsp)
movl %eax, 0x57c(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x3150
leaq 0xf0(%rsp), %rdi
callq 0x3080
jmp 0x3d6d
movq 0x102d2(%rip), %rdi     # 0x13fd8
leaq 0xa3cd(%rip), %rsi      # 0xe0da
callq 0x3240
movq %rax, (%rsp)
jmp 0x3d18
movq (%rsp), %rdi
movq 0x10295(%rip), %rsi     # 0x13fb8
callq 0x3290
jmp 0x3d2a
leaq 0x360(%rsp), %rdi
callq 0x3f70
leaq 0x3a8(%rsp), %rdi
callq 0x3080
leaq 0x530(%rsp), %rdi
callq 0x3f70
leaq 0x588(%rsp), %rdi
callq 0x3080
movl 0x714(%rsp), %eax
addq $0x718, %rsp            # imm = 0x718
retq
leaq 0x360(%rsp), %rdi
callq 0x3f70
leaq 0x3a8(%rsp), %rdi
callq 0x3080
leaq 0x530(%rsp), %rdi
callq 0x3f70
leaq 0x588(%rsp), %rdi
callq 0x3080
movq 0x580(%rsp), %rdi
callq 0x34b0
nop