// File: /workspace/github_c_makefile_star2_O2/dropletzhu[P]msuite/pim_listener.c
// Function: main
int
main (int argc, char* argv[])
{
	char ch;
	char datagram[BUF_LEN];
	char rp[IP_LEN];
	struct sockaddr_in sin;
	struct _SPimHdr *pim_header;
	struct _SPimRegHdr *pim_reg_header;
	int sockfd;
	int pim_null_reg = 0, pim_reg = 0, pim_reg_stop = 0;
	int nbytes;
	unsigned int len;
	UINT2 *flags;
	int length = 256;

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

    while ((ch = getopt(argc, argv, "r:l:")) != -1)
    {
        switch (ch)
        {
			case 'r':
				strncpy(rp, optarg, IP_LEN -1);
				break;
			case 'l':
				length = atoi(optarg);
				break;
        	default:
				usage();
            	return 0;
		}
    }

	sockfd = socket (PF_INET, SOCK_RAW, IPPROTO_PIM);
	if( sockfd < 0 ) {
		perror("socket\n");
		return -1;
	}

	memset(&sin,0,sizeof(struct sockaddr_in));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = inet_addr (rp);

	if (bind(sockfd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
		perror("bind\n");
		return -1;
	}

	len = sizeof(sin);
	while (1)
    {
		memset (datagram, 0, BUF_LEN);

    	if ( (nbytes = recvfrom(sockfd, datagram,length, 0,(struct sockaddr *) &sin, &len)) < 0) {
			perror("recvfrom\n");
			return -1;
		} else {
			pim_header = (struct _SPimHdr*)(datagram + sizeof(struct ip)); 
			if( pim_header->msgtpe == PIMSM_REGISTER_MSG ) {
				pim_reg_header = (struct _SPimRegHdr*)(datagram + sizeof(struct ip));
				flags = (UINT2*)(datagram + sizeof(struct ip) + 4 );
				*flags = ntohs(*flags);
				if( pim_reg_header->nulbit == 1 ) {
					printf("Receiver PIM null register %s: %d\n",rp,++pim_null_reg);
				} else {
					printf("Receiver PIM register %s: %d\n",rp,++pim_reg);
				}
			} else if( pim_header->msgtpe == PIMSM_REGISTER_STOP_MSG ) {
				printf("Receiver PIM register-stop %s: %d\n",rp,++pim_reg_stop);
			} else {
				printf(" unknown type: %d, nbytes: %d\n", pim_header->msgtpe, nbytes);
			}
		}
    }

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10048, %rsp          # imm = 0x10048
cmpl $0x2, %edi
jge 0x1246
callq 0x11dc
xorl %eax, %eax
addq $0x10048, %rsp          # imm = 0x10048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
movl %edi, %r14d
movl $0x100, 0x4(%rsp)       # imm = 0x100
leaq 0xdbd(%rip), %r15       # 0x2018
movq 0x2d7e(%rip), %rbp      # 0x3fe0
leaq 0x10(%rsp), %r12
pushq $0xf
popq %r13
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10b0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x12b0
sarl $0x18, %eax
cmpl $0x72, %eax
jne 0x129c
movq (%rbp), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x1030
jmp 0x126b
cmpl $0x6c, %eax
jne 0x122d
movq (%rbp), %rdi
callq 0x10c0
movl %eax, 0x4(%rsp)
jmp 0x126b
pushq $0x2
popq %rdi
pushq $0x3
popq %rsi
pushq $0x67
popq %rdx
callq 0x10d0
testl %eax, %eax
jns 0x12ce
leaq 0xd54(%rip), %rdi       # 0x201d
jmp 0x1400
movl %eax, %ebx
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r14
movaps %xmm0, (%r14)
movw $0x2, (%r14)
leaq 0x10(%rsp), %rdi
callq 0x1070
movl %eax, 0x4(%r14)
pushq $0x10
popq %rdx
movl %ebx, %edi
movq %r14, %rsi
callq 0x1090
testl %eax, %eax
jns 0x130d
leaq 0xd1d(%rip), %rdi       # 0x2025
jmp 0x1400
leaq 0x2c(%rsp), %r14
movl $0x10, (%r14)
movslq 0x4(%rsp), %r15
leaq 0x40(%rsp), %r12
leaq 0x30(%rsp), %r13
leaq 0xd6a(%rip), %rbp       # 0x2099
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
movl $0xffff, %edx           # imm = 0xFFFF
movq %r12, %rdi
xorl %esi, %esi
callq 0x1060
movl %ebx, %edi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movq %r13, %r8
movq %r14, %r9
callq 0x1080
testl %eax, %eax
js 0x13f9
movzbl 0x54(%rsp), %esi
andl $0xf, %esi
cmpl $0x2, %esi
je 0x13bf
cmpl $0x1, %esi
je 0x1393
movq %rbp, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1050
jmp 0x1347
movzwl 0x58(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x58(%rsp)
btl $0xe, %eax
jae 0x13e1
movl 0xc(%rsp), %edx
incl %edx
leaq 0xc81(%rip), %rdi       # 0x2035
leaq 0x10(%rsp), %rsi
movl %edx, 0xc(%rsp)
jmp 0x13d5
movl 0x4(%rsp), %edx
incl %edx
leaq 0xcaa(%rip), %rdi       # 0x2076
leaq 0x10(%rsp), %rsi
movl %edx, 0x4(%rsp)
xorl %eax, %eax
callq 0x1050
jmp 0x1347
movl 0x8(%rsp), %edx
incl %edx
leaq 0xc6a(%rip), %rdi       # 0x2058
leaq 0x10(%rsp), %rsi
movl %edx, 0x8(%rsp)
jmp 0x13d5
leaq 0xc2b(%rip), %rdi       # 0x202b
callq 0x10a0
pushq $-0x1
popq %rax
jmp 0x1234