// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10070, %rsp          # imm = 0x10070
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x10048(%rbp)
movl $0x0, -0x1004c(%rbp)
movl $0x0, -0x10050(%rbp)
movl $0x100, -0x10064(%rbp)  # imm = 0x100
cmpl $0x1, -0x8(%rbp)
jg 0x1288
callq 0x11e0
movl $0x0, -0x4(%rbp)
jmp 0x154e
jmp 0x128a
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xe01(%rip), %rdx       # 0x2099
callq 0x10b0
movb %al, -0x11(%rbp)
movsbl %al, %eax
cmpl $-0x1, %eax
je 0x1310
movsbl -0x11(%rbp), %eax
movl %eax, -0x10068(%rbp)
subl $0x6c, %eax
je 0x12e3
jmp 0x12b9
movl -0x10068(%rbp), %eax
subl $0x72, %eax
jne 0x12fa
jmp 0x12c6
leaq -0x10020(%rbp), %rdi
movq 0x2d0c(%rip), %rax      # 0x3fe0
movq (%rax), %rsi
movl $0xf, %edx
callq 0x1030
jmp 0x130b
movq 0x2cf6(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10c0
movl %eax, -0x10064(%rbp)
jmp 0x130b
callq 0x11e0
movl $0x0, -0x4(%rbp)
jmp 0x154e
jmp 0x128a
movl $0x2, %edi
movl $0x3, %esi
movl $0x67, %edx
callq 0x10d0
movl %eax, -0x10044(%rbp)
cmpl $0x0, -0x10044(%rbp)
jge 0x134b
leaq 0xd64(%rip), %rdi       # 0x209e
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x154e
leaq -0x10030(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
movw $0x2, -0x10030(%rbp)
leaq -0x10020(%rbp), %rdi
callq 0x1060
movl %eax, -0x1002c(%rbp)
movl -0x10044(%rbp), %edi
leaq -0x10030(%rbp), %rsi
movl $0x10, %edx
callq 0x1090
cmpl $0x0, %eax
jge 0x13ad
leaq 0xd0a(%rip), %rdi       # 0x20a6
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x154e
movl $0x10, -0x10058(%rbp)
leaq -0x10010(%rbp), %rdi
xorl %esi, %esi
movl $0xffff, %edx           # imm = 0xFFFF
callq 0x1050
movl -0x10044(%rbp), %edi
leaq -0x10010(%rbp), %rsi
movslq -0x10064(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x10030(%rbp), %r8
leaq -0x10058(%rbp), %r9
callq 0x1070
movl %eax, -0x10054(%rbp)
cmpl $0x0, %eax
jge 0x1416
leaq 0xca7(%rip), %rdi       # 0x20ac
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x154e
leaq -0x10010(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x10038(%rbp)
movq -0x10038(%rbp), %rax
movb (%rax), %al
andb $0xf, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x14ea
leaq -0x10010(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x10040(%rbp)
leaq -0x10010(%rbp), %rax
addq $0x14, %rax
addq $0x4, %rax
movq %rax, -0x10060(%rbp)
movq -0x10060(%rbp), %rax
movzwl (%rax), %edi
callq 0x1080
movw %ax, %cx
movq -0x10060(%rbp), %rax
movw %cx, (%rax)
movq -0x10040(%rbp), %rax
movw 0x4(%rax), %ax
shrw $0xe, %ax
andw $0x1, %ax
movzwl %ax, %eax
cmpl $0x1, %eax
jne 0x14c4
leaq -0x10020(%rbp), %rsi
movl -0x10048(%rbp), %edx
addl $0x1, %edx
movl %edx, -0x10048(%rbp)
leaq 0xbfb(%rip), %rdi       # 0x20b6
movb $0x0, %al
callq 0x1040
jmp 0x14e8
leaq -0x10020(%rbp), %rsi
movl -0x1004c(%rbp), %edx
addl $0x1, %edx
movl %edx, -0x1004c(%rbp)
leaq 0xbf8(%rip), %rdi       # 0x20d9
movb $0x0, %al
callq 0x1040
jmp 0x1547
movq -0x10038(%rbp), %rax
movb (%rax), %al
andb $0xf, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0x1523
leaq -0x10020(%rbp), %rsi
movl -0x10050(%rbp), %edx
addl $0x1, %edx
movl %edx, -0x10050(%rbp)
leaq 0xbdd(%rip), %rdi       # 0x20f7
movb $0x0, %al
callq 0x1040
jmp 0x1545
movq -0x10038(%rbp), %rax
movb (%rax), %al
andb $0xf, %al
movzbl %al, %esi
movl -0x10054(%rbp), %edx
leaq 0xbdc(%rip), %rdi       # 0x211a
movb $0x0, %al
callq 0x1040
jmp 0x1547
jmp 0x1549
jmp 0x13b7
movl -0x4(%rbp), %eax
addq $0x10070, %rsp          # imm = 0x10070
popq %rbp
retq