// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
callq 0x30a0
movl %eax, %ecx
movslq 0x1b7e4a4(%r12), %rbp
xorl %eax, %eax
cmpq $0x752f, %rbp           # imm = 0x752F
jg 0x683b
leal 0x10(%rbx), %esi
movq 0x8(%r12), %rdx
cmpl 0x1c(%rdx), %esi
jge 0x683b
cmpl $0x0, 0x24(%rdx)
je 0x675f
movslq %ecx, %rcx
imulq $0x51eb851f, %rcx, %rsi # imm = 0x51EB851F
movq %rsi, %rdi
shrq $0x3f, %rdi
sarq $0x25, %rsi
addl %edi, %esi
imull $0x64, %esi, %esi
movsd 0x8(%rdx), %xmm0
mulsd 0x10987(%rip), %xmm0    # 0x170d8
subl %esi, %ecx
cvttsd2si %xmm0, %edx
cmpl %edx, %ecx
jl 0x683b
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rax), %rdi
callq 0x33a0
movq %r15, %rsi
movq %rax, %r15
movl %ebx, (%rax)
movl (%r13), %eax
movl %eax, 0x4(%r15)
movzwl 0x4(%r13), %eax
movw %ax, 0x8(%r15)
movl (%r14), %eax
movl %eax, 0xa(%r15)
movzwl 0x4(%r14), %eax
movw %ax, 0xe(%r15)
leaq 0x10(%r15), %rdi
movq %rsi, 0x8(%rsp)
movq 0x10(%rsp), %rdx
callq 0x3220
leal 0x1(%rbp), %eax
movl %eax, 0x1b7e4a4(%r12)
movq %r15, 0x1b7e4b0(%r12,%rbp,8)
movslq (%r13), %r15
movq 0x8(%r12), %rdi
callq 0xc87e
cmpq $0x3e9, %r15            # imm = 0x3E9
jge 0x684a
cmpl $0xe10, %eax            # imm = 0xE10
jge 0x6869
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 0x19f4f(%rip), %rdi     # 0x20770
leaq 0x1091c(%rip), %rsi     # 0x17144
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
callq 0x3080
addq $0x20, %rsp
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1088f(%rip), %rdi     # 0x170e0
leaq 0x10899(%rip), %rsi     # 0x170f1
leaq 0x1089e(%rip), %rcx     # 0x170fd
movl $0x6e, %edx
callq 0x31a0
leaq 0x108c4(%rip), %rdi     # 0x17134
leaq 0x1087a(%rip), %rsi     # 0x170f1
leaq 0x1087f(%rip), %rcx     # 0x170fd
movl $0x6f, %edx
callq 0x31a0