// File: csMOOC[P]UIUC/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;
	}

	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));
	memcpy(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 0x3070
movl $0x64, %ecx
cltd
idivl %ecx
movq -0x50(%rbp), %rax
movl %edx, -0x3c(%rbp)
cmpl $0x7530, 0x1b7e4a4(%rax) # imm = 0x7530
jge 0x6ec5
movq -0x50(%rbp), %rcx
movl -0x2c(%rbp), %eax
addl $0x10, %eax
movq 0x8(%rcx), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x6ec5
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x0, 0x24(%rax)
je 0x6ed1
movq -0x50(%rbp), %rcx
movl -0x3c(%rbp), %eax
movq 0x8(%rcx), %rcx
movsd 0x8228(%rip), %xmm0     # 0xf0e0
mulsd 0x8(%rcx), %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %eax
jge 0x6ed1
movl $0x0, -0x4(%rbp)
jmp 0x7039
movslq -0x2c(%rbp), %rdi
addq $0x10, %rdi
callq 0x32f0
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 0x3190
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 0x99f0
movl %eax, -0x44(%rbp)
cmpl $0x3e8, -0x40(%rbp)     # imm = 0x3E8
jg 0x6f6e
jmp 0x6f8d
leaq 0x8173(%rip), %rdi      # 0xf0e8
leaq 0x817d(%rip), %rsi      # 0xf0f9
movl $0x6c, %edx
leaq 0x817d(%rip), %rcx      # 0xf105
callq 0x3130
cmpl $0xe10, -0x44(%rbp)     # imm = 0xE10
jge 0x6f98
jmp 0x6fb7
leaq 0x819d(%rip), %rdi      # 0xf13c
leaq 0x8153(%rip), %rsi      # 0xf0f9
movl $0x6d, %edx
leaq 0x8153(%rip), %rcx      # 0xf105
callq 0x3130
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 0xe658(%rip), %rdi      # 0x15670
leaq 0x812d(%rip), %rsi      # 0xf14c
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x3060
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)