// File: /workspace/github_c_makefile_star2_O3/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 $0x10098, %rsp          # imm = 0x10098
movq %rsi, %rbx
movl %edi, %ebp
movb $0x1, 0x7(%rsp)
movl $0x1, 0x2c(%rsp)
callq 0x10b0
movq %rax, 0x20(%rsp)
cmpl $0x2, %ebp
jge 0x1357
callq 0x129f
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x10098, %rsp          # imm = 0x10098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0xc(%rsp)
movl $0x100, 0x8(%rsp)       # imm = 0x100
leaq 0xce7(%rip), %r15       # 0x2055
movq 0x2c6b(%rip), %r12      # 0x3fe0
leaq 0xca4(%rip), %r13       # 0x2020
leaq 0x40(%rsp), %r14
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10d0
cmpl $0x6b, %eax
jle 0x13c9
addl $-0x6c, %eax
cmpl $0x8, %eax
ja 0x133c
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movw $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movq (%r12), %rsi
movl $0x5, %edx
leaq 0x14(%rsp), %rdi
callq 0x1030
jmp 0x1381
cmpl $0x67, %eax
jne 0x141f
pxor %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
movq (%r12), %rsi
movl $0xf, %edx
leaq 0x30(%rsp), %rdi
callq 0x1030
jmp 0x1381
pxor %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movq (%r12), %rsi
movl $0xf, %edx
movq %r14, %rdi
callq 0x1030
jmp 0x1381
movq (%r12), %rdi
callq 0x10e0
movb %al, 0x7(%rsp)
jmp 0x1381
cmpl $-0x1, %eax
je 0x1451
cmpl $0x63, %eax
jne 0x133c
movq (%r12), %rdi
callq 0x10e0
movl %eax, 0xc(%rsp)
jmp 0x1381
movq (%r12), %rdi
callq 0x10e0
movl %eax, 0x8(%rsp)
jmp 0x1381
cmpl $0x5ce, 0x8(%rsp)       # imm = 0x5CE
jl 0x1477
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, %ebp
testl %eax, %eax
jns 0x14b6
jmp 0x1800
movl $0x2, %edi
movl $0x3, %esi
movl $0x11, %edx
callq 0x1100
testl %eax, %eax
js 0x1800
movl %eax, %ebp
leaq 0x2c(%rsp), %rcx
movl %eax, %edi
xorl %esi, %esi
movl $0x3, %edx
movl $0x4, %r8d
callq 0x1050
testl %eax, %eax
jne 0x1812
leaq 0x40(%rsp), %rdi
callq 0x10a0
leaq 0x80(%rsp), %rbx
movl %eax, (%rbx)
movq $0x0, 0x58(%rsp)
movw $0x2, 0x50(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x10a0
movl %eax, 0x54(%rsp)
leaq 0x14(%rsp), %rdi
callq 0x10e0
rolw $0x8, %ax
movw %ax, 0x52(%rsp)
movl %ebp, %edi
xorl %esi, %esi
movl $0x20, %edx
movq %rbx, %rcx
movl $0x4, %r8d
callq 0x1050
testl %eax, %eax
jne 0x17f7
xorl %ebx, %ebx
leaq 0x7(%rsp), %rcx
movl %ebp, %edi
xorl %esi, %esi
movl $0x21, %edx
movl $0x1, %r8d
callq 0x1050
testl %eax, %eax
jne 0x17f7
cmpl $0x0, 0xc(%rsp)
js 0x1343
movl %ebp, 0x1c(%rsp)
movl 0x8(%rsp), %ecx
movl %ecx, %eax
rolw $0x8, %ax
movw %ax, 0x12(%rsp)
movq 0x20(%rsp), %rax
rolw $0x8, %ax
movq %rax, 0x20(%rsp)
movslq %ecx, %rax
leaq -0x14(%rax), %rdx
movl %edx, %esi
rolw $0x8, %si
movw %si, 0x10(%rsp)
movq %rax, 0x78(%rsp)
addq $-0x1c, %rax
movq %rax, 0x68(%rsp)
shrq %rdx
movl %ecx, %eax
movq %rax, 0x60(%rsp)
movq %rdx, 0x70(%rsp)
leal 0x1(%rdx), %eax
movl %eax, 0x28(%rsp)
movl $0x1, %ebp
leaq 0x90(%rsp), %rbx
movl 0xc(%rsp), %r12d
movl $0xffff, %edx           # imm = 0xFFFF
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1080
cmpl $0x5cd, 0x8(%rsp)       # imm = 0x5CD
jle 0x15fa
movq %rbx, %rdi
movq 0x60(%rsp), %rsi
leaq 0xaa7(%rip), %rdx       # 0x2080
leaq 0x40(%rsp), %rcx
leaq 0x30(%rsp), %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1070
movq %rbx, %r14
movl 0x1c(%rsp), %r15d
jmp 0x179f
movw $0x45, 0x90(%rsp)
movzwl 0x12(%rsp), %eax
movw %ax, 0x92(%rsp)
movl %ebp, %eax
rolw $0x8, %ax
movw %ax, 0x94(%rsp)
movw $0x0, 0x96(%rsp)
movb 0x7(%rsp), %al
movb %al, 0x98(%rsp)
movb $0x11, 0x99(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x10a0
movl %eax, 0x9c(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x10a0
movl %eax, 0xa0(%rsp)
pxor %xmm0, %xmm0
xorl %eax, %eax
movl 0x1c(%rsp), %r15d
pxor %xmm2, %xmm2
movd 0x90(%rsp,%rax,2), %xmm1
punpcklwd %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
paddq %xmm1, %xmm0
addq $0x2, %rax
cmpq $0xa, %rax
jne 0x166d
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddq %xmm0, %xmm1
movq %xmm1, %rax
movzwl %ax, %ecx
shrq $0x10, %rax
addl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
addl %eax, %ecx
notl %ecx
movw %cx, 0x9a(%rsp)
movq 0x20(%rsp), %rax
movw %ax, 0xa4(%rsp)
leaq 0x14(%rsp), %rdi
callq 0x10e0
rolw $0x8, %ax
movw %ax, 0xa6(%rsp)
movzwl 0x10(%rsp), %eax
movw %ax, 0xa8(%rsp)
movw %ax, 0x8e(%rsp)
leaq 0xac(%rsp), %rdi
movq 0x68(%rsp), %rsi
leaq 0x980(%rip), %rdx       # 0x2080
leaq 0x40(%rsp), %rcx
leaq 0x30(%rsp), %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1070
pxor %xmm2, %xmm2
pxor %xmm0, %xmm0
xorl %eax, %eax
movd 0x84(%rsp,%rax,2), %xmm1
punpcklwd %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
paddq %xmm1, %xmm0
addq $0x2, %rax
cmpq $0x6, %rax
jne 0x171e
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddq %xmm0, %xmm1
movq %xmm1, %rax
cmpl $0x0, 0x70(%rsp)
jle 0x176f
movl 0x28(%rsp), %ecx
leaq 0xa4(%rsp), %rdx
movzwl (%rdx), %esi
addq $0x2, %rdx
addq %rsi, %rax
decl %ecx
cmpl $0x1, %ecx
ja 0x175e
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, 0xaa(%rsp)
leaq 0xac(%rsp), %r14
movl %r15d, %edi
movq %rbx, %rsi
movq 0x78(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x50(%rsp), %r8
movl $0x10, %r9d
callq 0x1090
testq %rax, %rax
js 0x1809
testl %r12d, %r12d
setne %r15b
leal 0x1(%rbp), %r13d
movq %r14, %rdi
callq 0x1040
movl $0x1, %edi
callq 0x10f0
cmpl %r12d, %ebp
setge %al
andb %r15b, %al
movl %r13d, %ebp
cmpb $0x1, %al
jne 0x15b1
jmp 0x1341
leaq 0x877(%rip), %rdi       # 0x2075
jmp 0x1819
leaq 0x85b(%rip), %rdi       # 0x2062
jmp 0x1819
leaq 0x882(%rip), %rdi       # 0x2092
jmp 0x1819
leaq 0x850(%rip), %rdi       # 0x2069
callq 0x10c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x1343