// File: /workspace/github_cpp_makefile_star2_O2/tonymuu[P]distributed-kv-store/EmulNet.cpp
// Function: EmulNet::ENsend(Address*, Address*, char*, int)
int EmulNet::ENsend(Address *myaddr, Address *toaddr, char *data, int size) {
	en_msg *em;
	static char temp[2048];
	int sendmsg = rand() % 100;

	if( (emulnet.currbuffsize >= ENBUFFSIZE) || (size + (int)sizeof(en_msg) >= par->MAX_MSG_SIZE) || (par->dropmsg && sendmsg < (int) (par->MSG_DROP_PROB * 100)) ) {
		return 0;
	}

	// Allocate enough space for an en_msg AND the data that goes after it
	em = (en_msg *)malloc(sizeof(en_msg) + size);
	em->size = size;

	memcpy(&(em->from.addr), &(myaddr->addr), sizeof(em->from.addr));
	memcpy(&(em->to.addr), &(toaddr->addr), sizeof(em->from.addr));
	// Write data into the memory just beyond the en_msg
	memcpy((char*)(em + 1), data, size);

	emulnet.buff[emulnet.currbuffsize++] = em;

	int src = *(int *)(myaddr->addr);
	int time = par->getcurrtime();

	assert(src <= MAX_NODES);
	assert(time < MAX_TIME);

	sent_msgs[src][time]++;

	#ifdef DEBUGLOG
		sprintf(temp, "Sending 4+%d B msg type %d to %d.%d.%d.%d:%d ", size-4, *(int *)data, toaddr->addr[0], toaddr->addr[1], toaddr->addr[2], toaddr->addr[3], *(short *)&toaddr->addr[4]);
	#endif

	return size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %ebx
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
callq 0x30a0
pushq $0x64
popq %rcx
cltd
idivl %ecx
movslq 0x1b7e4a4(%r12), %r15
xorl %eax, %eax
cmpq $0x752f, %r15           # imm = 0x752F
jg 0x65ba
leal 0x10(%rbx), %esi
movq 0x8(%r12), %rcx
cmpl 0x1c(%rcx), %esi
jge 0x65ba
cmpl $0x0, 0x24(%rcx)
je 0x64e1
movsd 0x8(%rcx), %xmm0
mulsd 0xcc0b(%rip), %xmm0     # 0x130e0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %edx
jl 0x65ba
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rax), %rdi
callq 0x3390
movq %rbp, %rsi
movq %rax, %rbp
movl %ebx, (%rax)
movl (%r13), %eax
movl %eax, 0x4(%rbp)
movzwl 0x4(%r13), %eax
movw %ax, 0x8(%rbp)
movl (%r14), %eax
movl %eax, 0xa(%rbp)
movzwl 0x4(%r14), %eax
movw %ax, 0xe(%rbp)
leaq 0x10(%rbp), %rdi
movq %rsi, 0x8(%rsp)
movq 0x10(%rsp), %rdx
callq 0x3220
leal 0x1(%r15), %eax
movl %eax, 0x1b7e4a4(%r12)
movq %rbp, 0x1b7e4b0(%r12,%r15,8)
movslq (%r13), %r15
movq 0x8(%r12), %rdi
callq 0xabe4
cmpq $0x3e9, %r15            # imm = 0x3E9
jge 0x65c9
cmpl $0xe10, %eax            # imm = 0xE10
jge 0x65e2
imulq $0x3840, %r15, %rcx     # imm = 0x3840
addq %rcx, %r12
cltq
incl 0x10(%r12,%rax,4)
leal -0x4(%rbx), %edx
movq 0x8(%rsp), %rax
movl (%rax), %ecx
movsbl (%r14), %r8d
movsbl 0x1(%r14), %r9d
movsbl 0x2(%r14), %r10d
movsbl 0x3(%r14), %r11d
movswl 0x4(%r14), %r14d
subq $0x8, %rsp
leaq 0x171e0(%rip), %rdi     # 0x1d780
leaq 0xcba5(%rip), %rsi      # 0x1314c
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
callq 0x3070
addq $0x20, %rsp
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcb18(%rip), %rdi      # 0x130e8
leaq 0xcb22(%rip), %rsi      # 0x130f9
leaq 0xcb27(%rip), %rcx      # 0x13105
pushq $0x6e
jmp 0x65f9
leaq 0xcb53(%rip), %rdi      # 0x1313c
leaq 0xcb09(%rip), %rsi      # 0x130f9
leaq 0xcb0e(%rip), %rcx      # 0x13105
pushq $0x6f
popq %rdx
callq 0x31b0
nop