// File: /workspace/github_c_makefile_star2_O2/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 $0x100a8, %rsp          # imm = 0x100A8
movq %rsi, %rbx
movl %edi, %ebp
movb $0x1, 0xb(%rsp)
movl $0x1, 0x3c(%rsp)
callq 0x10b0
cmpl $0x2, %ebp
jge 0x1323
callq 0x1272
xorl %eax, %eax
addq $0x100a8, %rsp          # imm = 0x100A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x100, 0xc(%rsp)       # imm = 0x100
leaq 0xd16(%rip), %r15       # 0x2055
movq 0x2c9a(%rip), %r12      # 0x3fe0
pushq $0xf
popq %r13
leaq 0xccf(%rip), %r14       # 0x2020
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10d0
leal -0x6c(%rax), %ecx
cmpl $0x8, %ecx
ja 0x138d
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
andw $0x0, 0x18(%rsp)
andl $0x0, 0x14(%rsp)
movq (%r12), %rsi
leaq 0x14(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x1030
jmp 0x1351
cmpl $0x67, %eax
jne 0x13cf
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq (%r12), %rsi
leaq 0x50(%rsp), %rdi
jmp 0x13c5
movq (%r12), %rdi
callq 0x10e0
movb %al, 0xb(%rsp)
jmp 0x1351
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq (%r12), %rsi
leaq 0x60(%rsp), %rdi
movq %r13, %rdx
callq 0x1030
jmp 0x1351
cmpl $-0x1, %eax
je 0x1401
cmpl $0x63, %eax
jne 0x130a
movq (%r12), %rdi
callq 0x10e0
movl %eax, 0x1c(%rsp)
jmp 0x1351
movq (%r12), %rdi
callq 0x10e0
movl %eax, 0xc(%rsp)
jmp 0x1351
cmpl $0x5ce, 0xc(%rsp)       # imm = 0x5CE
jl 0x1423
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, %r14d
testl %eax, %eax
jns 0x1459
jmp 0x1712
pushq $0x2
popq %rdi
pushq $0x3
popq %rsi
pushq $0x11
popq %rdx
callq 0x1100
testl %eax, %eax
js 0x1712
movl %eax, %r14d
pushq $0x3
popq %rdx
leaq 0x3c(%rsp), %rcx
pushq $0x4
popq %r8
movl %eax, %edi
xorl %esi, %esi
callq 0x1050
testl %eax, %eax
jne 0x1724
leaq 0x60(%rsp), %rdi
callq 0x10a0
leaq 0x9c(%rsp), %rbx
movl %eax, (%rbx)
andq $0x0, 0x48(%rsp)
movw $0x2, 0x40(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x10a0
movl %eax, 0x44(%rsp)
leaq 0x14(%rsp), %rdi
callq 0x10e0
rolw $0x8, %ax
movw %ax, 0x42(%rsp)
pushq $0x20
popq %rdx
pushq $0x4
popq %r8
movl %r14d, %edi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x1050
testl %eax, %eax
jne 0x1709
pushq $0x21
popq %rdx
leaq 0xb(%rsp), %rcx
pushq $0x1
popq %r15
movl %r14d, %edi
xorl %esi, %esi
movl %r15d, %r8d
callq 0x1050
testl %eax, %eax
jne 0x1709
movl %r14d, 0x2c(%rsp)
movl 0xc(%rsp), %eax
movl %eax, %ecx
rolw $0x8, %cx
movw %cx, 0x12(%rsp)
movq 0x20(%rsp), %rcx
rolw $0x8, %cx
movq %rcx, 0x20(%rsp)
movslq %eax, %rsi
leaq -0x14(%rsi), %rcx
movl %ecx, %edx
rolw $0x8, %dx
movw %dx, 0x10(%rsp)
movq %rsi, 0x90(%rsp)
leaq -0x1c(%rsi), %rdx
movq %rdx, 0x80(%rsp)
shrq %rcx
movq %rcx, 0x88(%rsp)
movl %eax, %eax
movq %rax, 0x78(%rsp)
leaq 0xa0(%rsp), %r12
leaq 0x40(%rsp), %r14
pushq $0x10
popq %rbx
movl 0x1c(%rsp), %ecx
testl %ecx, %ecx
setne %al
cmpl %ecx, %r15d
setg %cl
testb %cl, %al
jne 0x130f
movl $0xffff, %edx           # imm = 0xFFFF
movq %r12, %rdi
xorl %esi, %esi
callq 0x1080
cmpl $0x5cd, 0xc(%rsp)       # imm = 0x5CD
jle 0x159d
movq %r12, %rdi
movq 0x78(%rsp), %rsi
leaq 0xaff(%rip), %rdx       # 0x2080
leaq 0x60(%rsp), %rcx
leaq 0x50(%rsp), %r8
movl %r15d, %r9d
xorl %eax, %eax
callq 0x1070
movq %r12, %r13
jmp 0x16d0
movw $0x45, 0xa0(%rsp)
movzwl 0x12(%rsp), %eax
movw %ax, 0xa2(%rsp)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0xa4(%rsp)
andw $0x0, 0xa6(%rsp)
movb 0xb(%rsp), %al
movb %al, 0xa8(%rsp)
movb $0x11, 0xa9(%rsp)
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x10a0
movl %eax, 0xac(%rsp)
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x10a0
movl %eax, %r13d
movl %eax, 0xb0(%rsp)
movq %r12, %rdi
pushq $0xa
popq %rsi
callq 0x120c
movw %ax, 0xaa(%rsp)
movq 0x20(%rsp), %rax
movw %ax, 0xb4(%rsp)
leaq 0x14(%rsp), %rdi
callq 0x10e0
rolw $0x8, %ax
movw %ax, 0xb6(%rsp)
movzwl 0x10(%rsp), %ecx
movw %cx, 0xb8(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x30(%rsp)
movl %r13d, 0x34(%rsp)
movb $0x0, 0x38(%rsp)
movb 0xa9(%rsp), %al
movb %al, 0x39(%rsp)
movw %cx, 0x3a(%rsp)
leaq 0xbc(%rsp), %r13
movq %r13, %rdi
movq 0x80(%rsp), %rsi
leaq 0x9f4(%rip), %rdx       # 0x2080
movq %r14, %rcx
leaq 0x40(%rsp), %r14
movq %rbp, %r8
movl %r15d, %r9d
xorl %eax, %eax
callq 0x1070
leaq 0x30(%rsp), %rdi
leaq 0xb4(%rsp), %rsi
movq 0x88(%rsp), %rdx
callq 0x1236
cmpw $0x1, %ax
movl $0x0, %ecx
sbbl %ecx, %ecx
orl %eax, %ecx
movw %cx, 0xba(%rsp)
movl 0x2c(%rsp), %edi
movq %r12, %rsi
movq 0x90(%rsp), %rdx
xorl %ecx, %ecx
movq %r14, %r8
movl %ebx, %r9d
callq 0x1090
testq %rax, %rax
js 0x171b
incl %r15d
movq %r13, %rdi
callq 0x1040
pushq $0x1
popq %rdi
callq 0x10f0
jmp 0x1542
leaq 0x965(%rip), %rdi       # 0x2075
jmp 0x172b
leaq 0x949(%rip), %rdi       # 0x2062
jmp 0x172b
leaq 0x970(%rip), %rdi       # 0x2092
jmp 0x172b
leaq 0x93e(%rip), %rdi       # 0x2069
callq 0x10c0
pushq $-0x1
popq %rax
jmp 0x1311