// File: /workspace/github_c_makefile_star2_O1/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 $0x10038, %rsp          # imm = 0x10038
cmpl $0x2, %edi
jge 0x1246
callq 0x11dc
xorl %eax, %eax
addq $0x10038, %rsp          # imm = 0x10038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
movl %edi, %ebp
leaq 0xdc6(%rip), %rdx       # 0x2018
callq 0x10b0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x12c2
movl $0x100, %r12d           # imm = 0x100
movq 0x2d72(%rip), %r13      # 0x3fe0
leaq 0xda3(%rip), %r14       # 0x2018
leaq 0x10(%rsp), %r15
sarl $0x18, %eax
cmpl $0x6c, %eax
je 0x129a
cmpl $0x72, %eax
jne 0x122d
movq (%r13), %rsi
movl $0xf, %edx
movq %r15, %rdi
callq 0x1030
jmp 0x12a6
movq (%r13), %rdi
callq 0x10c0
movl %eax, %r12d
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10b0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x127a
movslq %r12d, %rbx
jmp 0x12c7
movl $0x100, %ebx            # imm = 0x100
movl $0x2, %edi
movl $0x3, %esi
movl $0x67, %edx
callq 0x10d0
testl %eax, %eax
jns 0x12eb
leaq 0xd37(%rip), %rdi       # 0x201d
jmp 0x1429
movl %eax, %ebp
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r14
movaps %xmm0, (%r14)
movw $0x2, (%r14)
leaq 0x10(%rsp), %rdi
callq 0x1070
movl %eax, 0x4(%r14)
movl %ebp, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x1090
testl %eax, %eax
jns 0x132c
leaq 0xcfe(%rip), %rdi       # 0x2025
jmp 0x1429
leaq 0xc(%rsp), %r14
movl $0x10, (%r14)
leaq 0x30(%rsp), %r15
movl $0xffff, %edx           # imm = 0xFFFF
movq %r15, %rdi
xorl %esi, %esi
callq 0x1060
leaq 0x20(%rsp), %r8
movl %ebp, %edi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movq %r14, %r9
callq 0x1080
testl %eax, %eax
js 0x1422
leaq 0x30(%rsp), %r12
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r14d, %r14d
movzbl 0x44(%rsp), %esi
andl $0xf, %esi
cmpl $0x2, %esi
je 0x13b3
cmpl $0x1, %esi
jne 0x13c7
movzwl 0x48(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x48(%rsp)
btl $0xe, %eax
jb 0x13d9
incl %r14d
leaq 0xcaf(%rip), %rdi       # 0x2058
leaq 0x10(%rsp), %rsi
movl %r14d, %edx
jmp 0x13eb
incl %r13d
leaq 0xcb9(%rip), %rdi       # 0x2076
leaq 0x10(%rsp), %rsi
movl %r13d, %edx
jmp 0x13eb
leaq 0xccb(%rip), %rdi       # 0x2099
movl %eax, %edx
xorl %eax, %eax
callq 0x1050
jmp 0x13f2
incl %r15d
leaq 0xc52(%rip), %rdi       # 0x2035
leaq 0x10(%rsp), %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1050
movl $0xffff, %edx           # imm = 0xFFFF
movq %r12, %rdi
xorl %esi, %esi
callq 0x1060
movl %ebp, %edi
movq %r12, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
leaq 0x20(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x1080
testl %eax, %eax
jns 0x1379
leaq 0xc02(%rip), %rdi       # 0x202b
callq 0x10a0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1234