// File: /workspace/github_c_makefile_star2_O1/dropletzhu[P]msuite/msender.c
// Function: main
int
main (int argc, char *argv[])
{
	struct sockaddr_in addr;
	int fd, ch, i = 1;
	char ttl = 1;
	char msgbuf[BUF_LEN], source[IP_LEN], group[IP_LEN], port[PORT_LEN];
	struct in_addr interface_addr;
	int length = 256;
	int count = 0;
	struct ip *iph = NULL;
	struct udphdr *udp = NULL;
	char *payload = NULL;
	int hdrincl = 1;
	int retcode;
	int manual_header = 0;
	pseudo_header_t pseudo;
	unsigned short sport = (unsigned short)random();

	if (argc <= 1)
	{
	    usage();
	    return 0;
	}

	while ((ch = getopt (argc, argv, "s:g:p:t:l:c:")) != -1)
	{
		switch (ch)
		{
		case 's':
			memset(source,0,IP_LEN);
			strncpy(source, optarg, IP_LEN - 1);
			break;
		case 'g':
			memset (group, 0, IP_LEN);
			strncpy (group, optarg, IP_LEN - 1);
			break;
		case 'p':
			memset (port, 0, PORT_LEN);
			strncpy (port, optarg, PORT_LEN - 1);
			break;
		case 't':
			ttl = atoi (optarg);
			break;
		case 'l':
			length = atoi(optarg);
			break;
		case 'c':
			count = atoi(optarg);
			break;
		default:
			usage();
			return 0;
		}
	}

	/* raw socket can not send large packet with HDRINCL option */
	if( length < MTU ) {
		manual_header = 1;
	} else {
		manual_header = 0;
	}

	if( manual_header ) {
		/* set raw socket */
		fd = socket (AF_INET, SOCK_RAW, IPPROTO_UDP);
	} else {
		fd = socket (AF_INET, SOCK_DGRAM, 0 );
	}

	if (fd < 0)
	{
		perror ("socket");
		return -1;
	}

	if( manual_header ) {
		/* we need to build ip header manually */
		retcode = setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &hdrincl, sizeof(hdrincl));
		if( retcode != 0 ) {
			perror("setsockopt\n");
			return -1;
		}
	}

	/* set up source address */
	interface_addr.s_addr = inet_addr (source);

	/* set up destination address */
	memset (&addr, 0, sizeof (addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr (group);
	addr.sin_port = htons (atoi (port));

	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr,
		 sizeof (interface_addr)))
	{
		perror ("setsockopt");
		return -1;
	}

	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof (ttl)))
	{
		perror ("setsockopt");
		return -1;
	}

	/* now just sendto() our destination! */
	while (1)
	{
		if ( count && ( i > count ) ) {
			break;
		}
		memset(msgbuf,0,BUF_LEN);

		if( manual_header ) {
			iph = (struct ip*)msgbuf;
			udp = (struct udphdr*)(msgbuf + sizeof(struct ip));
			payload = msgbuf + sizeof(struct ip) + 
				sizeof(struct udphdr);

			/* ip header */
    		iph->ip_hl = 5;
    		iph->ip_v = 4;
    		iph->ip_tos = 0;
    		iph->ip_len = htons((short)length);
			if ( i == 0 )
				iph->ip_id = 0;
			else
    			iph->ip_id = htons((short)i);
    		iph->ip_off = 0;
    		iph->ip_ttl = ttl;
    		iph->ip_p = IPPROTO_UDP;
    		iph->ip_src.s_addr = inet_addr (source);
    		iph->ip_dst.s_addr = inet_addr (group);
    		iph->ip_sum = csum((unsigned short*)iph,
					sizeof(struct ip)>>1);

			/* udp header */
			udp->source = htons(sport);
			udp->dest = htons((short)atoi(port));
			udp->len = htons((short)(length - sizeof(struct ip)));
			pseudo.src = iph->ip_src.s_addr;
			pseudo.dst = iph->ip_dst.s_addr;
			pseudo.zero = 0;
			pseudo.proto = iph->ip_p;
			pseudo.length = udp->len;

			/* payload */
			snprintf(payload,
				length-sizeof(struct ip)-sizeof(struct udphdr),
				"Sender %s->%s: %d", source, group, i++);
			udp->check = udp_csum((unsigned short*)&pseudo,
				(unsigned short*)udp, 
				(length - sizeof(struct ip))>>1);

			if (udp->check == 0)
				udp->check = 0xFFFF;
		} else {
			payload = msgbuf;
			snprintf(payload,length,"Sender %s->%s: %d", source, group, i++);
		}

		if (sendto(fd, msgbuf, length, 0, (struct sockaddr *) &addr, 
					sizeof (addr)) < 0)
		{
			perror ("sendto");
			return -1;
		}
		else
		{
			printf ("%s\n",payload);
		}
		sleep (1);
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x100b8, %rsp          # imm = 0x100B8
movq %rsi, %rbx
movl %edi, %ebp
movb $0x1, 0xf(%rsp)
movl $0x1, 0x4c(%rsp)
callq 0x10b0
movq %rax, 0x40(%rsp)
cmpl $0x2, %ebp
jge 0x1327
callq 0x1280
jmp 0x17f1
movl $0x0, 0x2c(%rsp)
movl $0x100, 0x10(%rsp)      # imm = 0x100
leaq 0xd17(%rip), %r15       # 0x2055
movq 0x2c9b(%rip), %r12      # 0x3fe0
leaq 0xcd4(%rip), %r13       # 0x2020
leaq 0x60(%rsp), %r14
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10d0
cmpl $0x6b, %eax
jle 0x1399
addl $-0x6c, %eax
cmpl $0x8, %eax
ja 0x131d
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movw $0x0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movq (%r12), %rsi
movl $0x5, %edx
leaq 0x34(%rsp), %rdi
callq 0x1030
jmp 0x1351
cmpl $0x67, %eax
jne 0x13eb
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq (%r12), %rsi
movl $0xf, %edx
leaq 0x50(%rsp), %rdi
callq 0x1030
jmp 0x1351
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq (%r12), %rsi
movl $0xf, %edx
movq %r14, %rdi
callq 0x1030
jmp 0x1351
movq (%r12), %rdi
callq 0x10e0
movb %al, 0xf(%rsp)
jmp 0x1351
cmpl $-0x1, %eax
je 0x141d
cmpl $0x63, %eax
jne 0x131d
movq (%r12), %rdi
callq 0x10e0
movl %eax, 0x2c(%rsp)
jmp 0x1351
movq (%r12), %rdi
callq 0x10e0
movl %eax, 0x10(%rsp)
jmp 0x1351
cmpl $0x5cd, 0x10(%rsp)      # imm = 0x5CD
jle 0x1435
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
jmp 0x1444
movl $0x2, %edi
movl $0x3, %esi
movl $0x11, %edx
callq 0x1100
movl %eax, %ebp
testl %eax, %eax
js 0x180e
cmpl $0x5cd, 0x10(%rsp)      # imm = 0x5CD
jg 0x147e
leaq 0x4c(%rsp), %rcx
movl %ebp, %edi
xorl %esi, %esi
movl $0x3, %edx
movl $0x4, %r8d
callq 0x1050
testl %eax, %eax
jne 0x1840
leaq 0x60(%rsp), %rdi
callq 0x10a0
leaq 0xac(%rsp), %rbx
movl %eax, (%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movw $0x2, 0x70(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x10a0
movl %eax, 0x74(%rsp)
leaq 0x34(%rsp), %rdi
callq 0x10e0
rolw $0x8, %ax
movw %ax, 0x72(%rsp)
movl %ebp, %edi
xorl %esi, %esi
movl $0x20, %edx
movq %rbx, %rcx
movl $0x4, %r8d
callq 0x1050
testl %eax, %eax
jne 0x1805
leaq 0xf(%rsp), %rcx
movl %ebp, %edi
xorl %esi, %esi
movl $0x21, %edx
movl $0x1, %r8d
callq 0x1050
testl %eax, %eax
jne 0x1805
movl 0x14(%rsp), %eax
cmpl $0x0, 0x2c(%rsp)
js 0x17ed
movl %eax, 0x24(%rsp)
movl %ebp, 0x3c(%rsp)
movl 0x10(%rsp), %ecx
movl %ecx, %eax
rolw $0x8, %ax
movw %ax, 0x32(%rsp)
movq 0x40(%rsp), %rax
rolw $0x8, %ax
movq %rax, 0x40(%rsp)
movslq %ecx, %rax
leaq -0x14(%rax), %rdx
movl %edx, %esi
rolw $0x8, %si
movw %si, 0x30(%rsp)
movq %rax, 0xa0(%rsp)
addq $-0x1c, %rax
movq %rax, 0x90(%rsp)
shrq %rdx
movl %ecx, %eax
movq %rax, 0x88(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x28(%rsp)
movb 0x1c(%rsp), %al
movl %eax, 0x20(%rsp)
movb 0x1d(%rsp), %al
movb %al, 0xe(%rsp)
movq %rdx, 0x98(%rsp)
leal 0x1(%rdx), %eax
movl %eax, 0x48(%rsp)
movl $0x1, %r13d
leaq 0xb0(%rsp), %rbx
movl 0x2c(%rsp), %r15d
movl $0xffff, %edx           # imm = 0xFFFF
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1080
cmpl $0x5cd, 0x10(%rsp)      # imm = 0x5CD
jle 0x15ee
movq %rbx, %rdi
movq 0x88(%rsp), %rsi
leaq 0xab2(%rip), %rdx       # 0x2080
leaq 0x60(%rsp), %rcx
leaq 0x50(%rsp), %r8
movl %r13d, %r9d
xorl %eax, %eax
callq 0x1070
movq %rbx, %r14
movl 0x3c(%rsp), %ebp
jmp 0x177c
movw $0x45, 0xb0(%rsp)
movzwl 0x32(%rsp), %eax
movw %ax, 0xb2(%rsp)
movl %r13d, %eax
rolw $0x8, %ax
movw %ax, 0xb4(%rsp)
movw $0x0, 0xb6(%rsp)
movb 0xf(%rsp), %al
movb %al, 0xb8(%rsp)
movb $0x11, 0xb9(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x10a0
movl %eax, 0xbc(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x10a0
movl %eax, 0x28(%rsp)
movl %eax, 0xc0(%rsp)
movl $0xb, %ecx
xorl %eax, %eax
movq %rbx, %rdx
movl 0x3c(%rsp), %ebp
movzwl (%rdx), %esi
addq $0x2, %rdx
addq %rsi, %rax
decl %ecx
cmpl $0x1, %ecx
ja 0x1665
movq %rax, %rcx
shrq $0x10, %rcx
movzwl %ax, %eax
addl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
addl %ecx, %eax
notl %eax
movw %ax, 0xba(%rsp)
movq 0x40(%rsp), %rax
movw %ax, 0xc4(%rsp)
leaq 0x34(%rsp), %rdi
callq 0x10e0
rolw $0x8, %ax
movw %ax, 0xc6(%rsp)
movzwl 0x30(%rsp), %eax
movw %ax, 0xc8(%rsp)
movl 0xbc(%rsp), %ecx
movl %ecx, 0x24(%rsp)
movb 0xb9(%rsp), %cl
movb %cl, 0xe(%rsp)
movw %ax, 0x1e(%rsp)
leaq 0xcc(%rsp), %rdi
movq 0x90(%rsp), %rsi
leaq 0x98b(%rip), %rdx       # 0x2080
leaq 0x60(%rsp), %rcx
leaq 0x50(%rsp), %r8
movl %r13d, %r9d
xorl %eax, %eax
callq 0x1070
xorl %ecx, %ecx
xorl %eax, %eax
movzwl 0x14(%rsp,%rcx,2), %edx
addq %rdx, %rax
incq %rcx
cmpl $0x6, %ecx
jne 0x170d
movl 0x48(%rsp), %ecx
leaq 0xc4(%rsp), %rdx
cmpl $0x0, 0x98(%rsp)
jle 0x1744
movzwl (%rdx), %esi
addq $0x2, %rdx
addq %rsi, %rax
decl %ecx
cmpl $0x1, %ecx
jg 0x1733
movq %rax, %rcx
shrq $0x10, %rcx
movl $0xffff, %edx           # imm = 0xFFFF
andl %edx, %eax
addl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
addl %ecx, %eax
cmpw $-0x1, %ax
notl %eax
cmovel %edx, %eax
movw %ax, 0xca(%rsp)
movl $0x0, 0x20(%rsp)
leaq 0xcc(%rsp), %r14
movl %ebp, %edi
movq %rbx, %rsi
movq 0xa0(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x70(%rsp), %r8
movl $0x10, %r9d
callq 0x1090
testq %rax, %rax
js 0x1817
testl %r15d, %r15d
setne %r12b
leal 0x1(%r13), %ebp
movq %r14, %rdi
callq 0x1040
movl $0x1, %edi
callq 0x10f0
cmpl %r15d, %r13d
setge %al
andb %r12b, %al
movl %ebp, %r13d
cmpb $0x1, %al
jne 0x15a3
movl 0x28(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x20(%rsp), %eax
movb %al, 0x1c(%rsp)
movb 0xe(%rsp), %al
movb %al, 0x1d(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
addq $0x100b8, %rsp          # imm = 0x100B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x869(%rip), %rdi       # 0x2075
jmp 0x1847
leaq 0x84d(%rip), %rdi       # 0x2062
jmp 0x1847
movl 0x28(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x20(%rsp), %eax
movb %al, 0x1c(%rsp)
movb 0xe(%rsp), %al
movb %al, 0x1d(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq 0x854(%rip), %rdi       # 0x2092
jmp 0x1847
leaq 0x822(%rip), %rdi       # 0x2069
callq 0x10c0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x17f3