// File: rohitrangan[P]3PC/src/client.cpp
// Function: main
int main(int argc, char* argv[])
{
    if (argc != 5)
    {
        cout << "Not enough parameters.\n";
        return 1;
    }

    int transactionId = stoi (string (argv[1]));
    int add_v = stoi (string (argv[2]));
    int sleep_time = stoi (string (argv[3]));
    double send_till = stod (string (argv[4]));
    int site_id = 1231;
    double time_elapsed = 0.0;
    int mod_sites = 3;
    map<int, int> sites;
    sites[0] = 6666;
    sites[1] = 6667;
    sites[2] = 6668;

    while ((int)time_elapsed < (int)send_till)
    {
        auto t1 = chrono::steady_clock::now ();
        Socket transaction_gen;
        Message msg (TRANSACTION,transactionId, site_id);
        string str = msg.createMessage();
        srand (time(NULL));
        if (mod_sites == 0)
        {
            cout << "Cannot connect to any server. Exiting.." << endl;
            exit (0);
        }
        int cur_site = rand() % mod_sites;
        cout << "Transaction ID: " << transactionId << ", Site ID :" <<cur_site;
        cout << ", message: " << str << endl;
        if (transaction_gen.connect("localhost", sites[cur_site]) < 0)
        {
            sites.erase (cur_site);
            --mod_sites;
            continue;
        }
        transaction_gen.send(str);
        transactionId += add_v;
        transaction_gen.close ();
        this_thread::sleep_for (chrono::milliseconds(sleep_time));
        auto t2 = chrono::steady_clock::now ();
        chrono::duration<double> time_span = chrono::\
                        duration_cast<chrono::duration<double>>(t2 - t1);
        time_elapsed += time_span.count ();
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x5, -0x8(%rbp)
je 0x3b5e
movq 0xa482(%rip), %rdi      # 0xdfc8
leaq 0x54c3(%rip), %rsi      # 0x9010
callq 0x32f0
movl $0x1, -0x4(%rbp)
jmp 0x4331
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x200(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x34b0
movq -0x200(%rbp), %rsi
movq -0x1f8(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x4390
jmp 0x3b96
xorl %eax, %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
movl $0xa, %edx
callq 0x4350
movl %eax, -0x204(%rbp)
jmp 0x3bb0
leaq -0x38(%rbp), %rdi
callq 0x31c0
leaq -0x39(%rbp), %rdi
callq 0x3370
movl -0x204(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x218(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x34b0
movq -0x218(%rbp), %rsi
movq -0x210(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x4390
jmp 0x3c03
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
movl $0xa, %edx
callq 0x4350
movl %eax, -0x21c(%rbp)
jmp 0x3c1d
leaq -0x70(%rbp), %rdi
callq 0x31c0
leaq -0x71(%rbp), %rdi
callq 0x3370
movl -0x21c(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x230(%rbp)
leaq -0x99(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x34b0
movq -0x230(%rbp), %rsi
movq -0x228(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0x4390
jmp 0x3c76
xorl %eax, %eax
movl %eax, %esi
leaq -0x98(%rbp), %rdi
movl $0xa, %edx
callq 0x4350
movl %eax, -0x234(%rbp)
jmp 0x3c93
leaq -0x98(%rbp), %rdi
callq 0x31c0
leaq -0x99(%rbp), %rdi
callq 0x3370
movl -0x234(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x248(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x34b0
movq -0x248(%rbp), %rsi
movq -0x240(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
callq 0x4390
jmp 0x3cf2
xorl %eax, %eax
movl %eax, %esi
leaq -0xc8(%rbp), %rdi
callq 0x4440
movsd %xmm0, -0x250(%rbp)
jmp 0x3d0c
leaq -0xc8(%rbp), %rdi
callq 0x31c0
leaq -0xc9(%rbp), %rdi
callq 0x3370
movsd -0x250(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movl $0x4cf, -0xd0(%rbp)     # imm = 0x4CF
movq $0x0, -0xd8(%rbp)
movl $0x3, -0xdc(%rbp)
leaq -0x110(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x4480
movq -0x260(%rbp), %rdi
movl $0x0, -0x114(%rbp)
leaq -0x114(%rbp), %rsi
callq 0x44a0
movq %rax, -0x258(%rbp)
jmp 0x3d8c
movq -0x258(%rbp), %rax
movl $0x1a0a, (%rax)         # imm = 0x1A0A
movl $0x1, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x44a0
movq %rax, -0x268(%rbp)
jmp 0x3dbf
movq -0x268(%rbp), %rax
movl $0x1a0b, (%rax)         # imm = 0x1A0B
movl $0x2, -0x11c(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x11c(%rbp), %rsi
callq 0x44a0
movq %rax, -0x270(%rbp)
jmp 0x3df2
movq -0x270(%rbp), %rax
movl $0x1a0c, (%rax)         # imm = 0x1A0C
cvttsd2si -0xd8(%rbp), %eax
cvttsd2si -0xa8(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4304
callq 0x3420
movq %rax, -0x128(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x6fe0
jmp 0x3e31
movl -0x14(%rbp), %edx
movl -0xd0(%rbp), %ecx
leaq -0x160(%rbp), %rdi
xorl %esi, %esi
callq 0x7ae0
jmp 0x3e4a
leaq -0x180(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x7d10
jmp 0x3e5f
xorl %eax, %eax
movl %eax, %edi
callq 0x32b0
movl %eax, %edi
callq 0x32d0
cmpl $0x0, -0xdc(%rbp)
jne 0x3fc8
movq 0xa145(%rip), %rdi      # 0xdfc8
leaq 0x519e(%rip), %rsi      # 0x9028
callq 0x32f0
movq %rax, -0x278(%rbp)
jmp 0x3e98
movq -0x278(%rbp), %rdi
movq 0xa10a(%rip), %rsi      # 0xdfb0
callq 0x3340
jmp 0x3ead
xorl %edi, %edi
callq 0x33d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3ed7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31c0
leaq -0x39(%rbp), %rdi
callq 0x3370
jmp 0x433d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3f08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31c0
leaq -0x71(%rbp), %rdi
callq 0x3370
jmp 0x433d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3f3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x31c0
leaq -0x99(%rbp), %rdi
callq 0x3370
jmp 0x433d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3f73
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x31c0
leaq -0xc9(%rbp), %rdi
callq 0x3370
jmp 0x433d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4323
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x42f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x42ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x42de
callq 0x30a0
movl -0xdc(%rbp), %ecx
cltd
idivl %ecx
movl %edx, -0x184(%rbp)
movq 0x9fe5(%rip), %rdi      # 0xdfc8
leaq 0x5066(%rip), %rsi      # 0x9050
callq 0x32f0
movq %rax, -0x280(%rbp)
jmp 0x3ff8
movq -0x280(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x3490
movq %rax, -0x288(%rbp)
jmp 0x4010
movq -0x288(%rbp), %rdi
leaq 0x5043(%rip), %rsi      # 0x9061
callq 0x32f0
movq %rax, -0x290(%rbp)
jmp 0x402c
movq -0x290(%rbp), %rdi
movl -0x184(%rbp), %esi
callq 0x3490
jmp 0x4040
movq 0x9f81(%rip), %rdi      # 0xdfc8
leaq 0x501f(%rip), %rsi      # 0x906d
callq 0x32f0
movq %rax, -0x298(%rbp)
jmp 0x405c
movq -0x298(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x32a0
movq %rax, -0x2a0(%rbp)
jmp 0x4078
movq -0x2a0(%rbp), %rdi
movq 0x9f2a(%rip), %rsi      # 0xdfb0
callq 0x3340
jmp 0x408d
leaq -0x1a9(%rbp), %rdi
movq %rdi, -0x2a8(%rbp)
callq 0x34b0
movq -0x2a8(%rbp), %rdx
leaq 0x4fcb(%rip), %rsi      # 0x9079
leaq -0x1a8(%rbp), %rdi
callq 0x4390
jmp 0x40bc
leaq -0x110(%rbp), %rdi
leaq -0x184(%rbp), %rsi
callq 0x4580
movq %rax, -0x2b0(%rbp)
jmp 0x40d8
movq -0x2b0(%rbp), %rax
movl (%rax), %edx
leaq -0x130(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x7020
movl %eax, -0x2b4(%rbp)
jmp 0x40fc
leaq -0x1a8(%rbp), %rdi
callq 0x31c0
leaq -0x1a9(%rbp), %rdi
callq 0x3370
movl -0x2b4(%rbp), %eax
cmpl $0x0, %eax
jge 0x4189
leaq -0x110(%rbp), %rdi
leaq -0x184(%rbp), %rsi
callq 0x4660
jmp 0x4134
movl -0xdc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xdc(%rbp)
movl $0x2, -0x1b0(%rbp)
jmp 0x428c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4178
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x31c0
leaq -0x1a9(%rbp), %rdi
callq 0x3370
jmp 0x42de
leaq -0x1d0(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x3120
jmp 0x419e
leaq -0x130(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x7220
jmp 0x41b3
leaq -0x1d0(%rbp), %rdi
callq 0x31c0
movl -0x50(%rbp), %ecx
movl -0x14(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x14(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x77e0
jmp 0x41d8
leaq -0x1d8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4740
jmp 0x41ea
leaq -0x1d8(%rbp), %rdi
callq 0x4690
jmp 0x41f8
callq 0x3420
movq %rax, -0x1e0(%rbp)
leaq -0x1e0(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x4790
movq %rax, -0x2c0(%rbp)
jmp 0x4220
movq -0x2c0(%rbp), %rax
movq %rax, -0x1f0(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x4760
movsd %xmm0, -0x2c8(%rbp)
jmp 0x4244
movsd -0x2c8(%rbp), %xmm0
movsd %xmm0, -0x1e8(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x47e0
movsd %xmm0, -0x2d0(%rbp)
jmp 0x426a
movsd -0x2d0(%rbp), %xmm0
addsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movl $0x0, -0x1b0(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x31c0
leaq -0x160(%rbp), %rdi
callq 0x4800
leaq -0x130(%rbp), %rdi
callq 0x7800
movl -0x1b0(%rbp), %eax
testl %eax, %eax
je 0x42c1
jmp 0x42bc
jmp 0x3dff
jmp 0x3dff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x31c0
leaq -0x180(%rbp), %rdi
callq 0x31c0
leaq -0x160(%rbp), %rdi
callq 0x4800
leaq -0x130(%rbp), %rdi
callq 0x7800
jmp 0x4323
movl $0x0, -0x4(%rbp)
movl $0x1, -0x1b0(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x4820
jmp 0x4331
leaq -0x110(%rbp), %rdi
callq 0x4820
jmp 0x433d
movl -0x4(%rbp), %eax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x34a0
nopw %cs:(%rax,%rax)