// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
callq 0x40e0
movl $0x64, %ecx
cltd
idivl %ecx
movq -0x50(%rbp), %rax
movl %edx, -0x3c(%rbp)
cmpl $0x7530, 0x1b7e4a4(%rax) # imm = 0x7530
jge 0xefe5
movq -0x50(%rbp), %rcx
movl -0x2c(%rbp), %eax
addl $0x10, %eax
movq 0x8(%rcx), %rcx
cmpl 0x1c(%rcx), %eax
jge 0xefe5
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x0, 0x24(%rax)
je 0xeff1
movq -0x50(%rbp), %rcx
movl -0x3c(%rbp), %eax
movq 0x8(%rcx), %rcx
movsd 0x1d158(%rip), %xmm0    # 0x2c130
mulsd 0x8(%rcx), %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %eax
jge 0xeff1
movl $0x0, -0x4(%rbp)
jmp 0xf159
movslq -0x2c(%rbp), %rdi
addq $0x10, %rdi
callq 0x4490
movq %rax, -0x38(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %edx
movl %edx, 0x4(%rax)
movw 0x4(%rcx), %cx
movw %cx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %edx
movl %edx, 0xa(%rax)
movw 0x4(%rcx), %cx
movw %cx, 0xe(%rax)
movq -0x38(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x42a0
movq -0x50(%rbp), %rax
movq -0x38(%rbp), %rdx
movl 0x1b7e4a4(%rax), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x1b7e4a4(%rax)
movslq %ecx, %rcx
movq %rdx, 0x1b7e4b0(%rax,%rcx,8)
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, -0x40(%rbp)
movq 0x8(%rax), %rdi
callq 0x18c60
movl %eax, -0x44(%rbp)
cmpl $0x3e8, -0x40(%rbp)     # imm = 0x3E8
jg 0xf08e
jmp 0xf0ad
leaq 0x1d0a3(%rip), %rdi     # 0x2c138
leaq 0x1d0ad(%rip), %rsi     # 0x2c149
movl $0x6e, %edx
leaq 0x1d0ad(%rip), %rcx     # 0x2c155
callq 0x4210
cmpl $0xe10, -0x44(%rbp)     # imm = 0xE10
jge 0xf0b8
jmp 0xf0d7
leaq 0x1d0cd(%rip), %rdi     # 0x2c18c
leaq 0x1d083(%rip), %rsi     # 0x2c149
movl $0x6f, %edx
leaq 0x1d083(%rip), %rcx     # 0x2c155
callq 0x4210
movq -0x50(%rbp), %rax
addq $0x10, %rax
movslq -0x40(%rbp), %rcx
imulq $0x3840, %rcx, %rcx     # imm = 0x3840
addq %rcx, %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %edx
subl $0x4, %edx
movq -0x28(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movsbl (%rax), %r8d
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %r9d
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %r11d
movq -0x20(%rbp), %rax
movsbl 0x3(%rax), %r10d
movq -0x20(%rbp), %rax
movswl 0x4(%rax), %eax
leaq 0x2f6e8(%rip), %rdi     # 0x3e820
leaq 0x1d05d(%rip), %rsi     # 0x2c19c
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x40a0
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)