// File: /workspace/github_c_makefile_star2_O3/jelaas[P]egetty/egetty.c
// Function: main
int main(int argc, char **argv, char **arge)
{
	int s;
	struct sockaddr_ll from;
	socklen_t fromlen = sizeof(from);
	int ifindex=-1;
	uint8_t *buf, *p;
	ssize_t n;
	unsigned int len;
	int count=1;
	int timeout = -1;
	int loginfd = -1;
	pid_t pid=-1;
	struct sk_buff *skb;
	
	envp = arge;
	conf.debug = 0;
	conf.device = "eth0";
	conf.devsocket = -1;
	
	while(--argc > 0) {
		if(strcmp(argv[argc], "debug")==0) {
			printf("Debug mode\n");
			conf.debug = 1;
			continue;
		}
		if(strcmp(argv[argc], "console")==0) {
			conf.kmsg = 1;
			continue;
		}
		if(strcmp(argv[argc], "waitif")==0) {
			conf.waitif = 1;
			continue;
		}
		if( (strlen(argv[argc]) < 3) && isdigit(*argv[argc])) {
			conf.console = atoi(argv[argc]);
			continue;
		}
		conf.device = argv[argc];
	}

	conf.devsocket = devsocket();
	
	if(conf.waitif) {
		/* wait for interface to become available */
		while(check_flag(conf.device, (IFF_UP | IFF_RUNNING))) {
			sleep(1);
		}
	} else {
		/* active interface */
		while(set_flag(conf.device, (IFF_UP | IFF_RUNNING))) {
			sleep(1);
		}
	}
	
	memset(conf.client.sll_addr, 255, 6);
	
	s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_EGETTY));
	if(s == -1)
	{
		fprintf(stderr, "socket(): %s\n", strerror(errno));
		exit(1);
	}
	
	if(conf.device)
	{
		ifindex = if_nametoindex(conf.device);
		if(!ifindex)
		{
			fprintf(stderr, "Not an interface\n");
			exit(1);
		}
	}
	
	
	if(ifindex >= 0)
	{
		struct sockaddr_ll addr;
		memset(&addr, 0, sizeof(addr));
		
		addr.sll_family = AF_PACKET;
		addr.sll_protocol = htons(ETH_P_EGETTY);
		addr.sll_ifindex = ifindex;
		
		if(bind(s, (const struct sockaddr *)&addr, sizeof(addr)))
		{
			fprintf(stderr, "bind() to interface failed\n");
			exit(1);
		}
	}
	
	skb = alloc_skb(1500);

	skb_reset(skb);
	skb_reserve(skb, 4);
	console_hello(s, ifindex, skb);
	
	while(count)
	{
		struct pollfd fds[2];

		if(pid) {
			int status;
			if(waitpid(-1, &status, WNOHANG)>0) {
				pid = -1;
				close(loginfd);
			}
		}
		if(pid == -1) {
			pid = login(&loginfd);
			if(pid == -1)
				exit(1);
			if(conf.debug) {
				printf("child pid = %d\n", pid);
				printf("loginfd = %d\n", loginfd);
			}
		}
		
		fds[0].fd = s;
		fds[0].events = POLLIN;
		fds[0].revents = 0;
		
		fds[1].fd = loginfd;
		fds[1].events = POLLIN;
		fds[1].revents = 0;

		n = poll(fds, 2, timeout);
		if(n == 0) {
			printf("timeout\n");
			exit(1);
		}

		if(fds[1].revents & POLLIN) {
			if(conf.debug) printf("POLLIN child\n");
			skb_reset(skb);
			skb_reserve(skb, 4);
			buf = skb_put(skb, 0);
			n = read(loginfd, buf, skb_tailroom(skb));
			if(n == -1) {
				fprintf(stderr, "read() failed\n");
				exit(1);
			}
			
			buf[n] = 0;
			if(conf.debug)
				printf("child: %d bytes\n", n);
			skb_put(skb, n);
			console_put(s, ifindex, skb);
		}
		if(fds[0].revents) {
			skb_reset(skb);
			buf = skb_put(skb, 0);
			n = recvfrom(s, buf, skb_tailroom(skb), 0, (struct sockaddr *)&from, &fromlen);
			if(n == -1) {
				fprintf(stderr, "recvfrom() failed. ifconfig up?\n");
				exit(1);
			}

			skb_put(skb, n);
			if(conf.debug) printf("received packet %d bytes\n", skb->len);
			
			if(ntohs(from.sll_protocol) == ETH_P_EGETTY) {
				if(conf.debug)
					printf("Received EGETTY\n");
				
				p = skb->data;
				if(*p == EGETTY_HUP) {
					if(pid != -1) kill(pid, 9);
					continue;
				}
				
				if(*p == EGETTY_SCAN) {
					skb_reset(skb);
					skb_reserve(skb, 4);
					console_hello(s, ifindex, skb);
					continue;
				}
				
				if(*p == EGETTY_WINCH) {
					p++;
					if(*p != conf.console) {
						if(conf.debug)
							printf("Wrong console %d not %d\n", *p, conf.console);
						continue;
					}
					p++;
					{
						struct winsize winp;
						winp.ws_row = *p++;
						winp.ws_col = *p++;
						winp.ws_xpixel = 0;
						winp.ws_ypixel = 0;
						ioctl(loginfd, TIOCSWINSZ, &winp);
						if(conf.debug)
							printf("WINCH to %d, %d\n", winp.ws_row, winp.ws_col);
					}
					continue;
				}
				
				if(*p != EGETTY_IN) {
					if(conf.debug)
						printf("Not EGETTY_IN: %d\n", *p);
					continue;
				}
				p++;
				if(*p != conf.console) {
					if(conf.debug)
						printf("Wrong console %d not %d\n", *p, conf.console);
					continue;
				}
				memcpy(conf.client.sll_addr, from.sll_addr, 6);
				p++;
				len = *p++ << 8;
				len += *p;
				if(len > n) {
					printf("Length field too long: %d\n", len);
					continue;
				}
				skb_trim(skb, len);
				skb_pull(skb, 4);
				if(conf.debug) printf("Sent %d bytes to child\n", skb->len);
				write(loginfd, skb->data, skb->len);
				continue;
			}
		}
		
	}
  exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl $0x14, 0x1c(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0xc(%rsp)
movq %rdx, 0xd3821(%rip)     # 0x4d5340
movl $0x0, 0xd37f7(%rip)     # 0x4d5320
leaq 0xa0568(%rip), %rcx     # 0x4a2098
movq %rcx, 0xd37d9(%rip)     # 0x4d5310
movl %eax, 0xd37fb(%rip)     # 0x4d5338
cmpl $0x1, %edi
jle 0x401c0c
movq %rsi, %rbx
movl %edi, %r15d
incq %r15
leaq 0xa641f(%rip), %r14     # 0x4a7f75
leaq 0xa0540(%rip), %r12     # 0x4a209d
leaq 0xa0541(%rip), %r13     # 0x4a20a5
movq -0x10(%rbx,%r15,8), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401bc1
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401bd9
movq %rbp, %rdi
movq %r13, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401be5
movq %rbp, %rdi
callq 0x4010d8
cmpq $0x2, %rax
ja 0x401bb8
callq 0x405170
movq (%rax), %rax
movsbq (%rbp), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x401bf1
movq %rbp, 0xd3751(%rip)     # 0x4d5310
jmp 0x401bff
leaq 0xa063c(%rip), %rdi     # 0x4a2204
callq 0x413790
movl $0x1, 0xd3749(%rip)     # 0x4d5320
jmp 0x401bff
movl $0x1, 0xd3735(%rip)     # 0x4d5318
jmp 0x401bff
movl $0x1, 0xd372d(%rip)     # 0x4d531c
jmp 0x401bff
movq %rbp, %rdi
callq 0x40ad50
movl %eax, 0xd3709(%rip)     # 0x4d5308
decq %r15
cmpq $0x2, %r15
ja 0x401b64
xorl %ebx, %ebx
leaq 0xa040b(%rip), %r14     # 0x4a2020
movl (%rbx,%r14), %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x4538a0
testl %eax, %eax
jns 0x401c3a
addq $0x4, %rbx
cmpq $0xc, %rbx
jne 0x401c15
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x401c3c
movl %eax, %ebp
movl %ebp, 0xd36f6(%rip)     # 0x4d5338
cmpl $0x0, 0xd36d3(%rip)     # 0x4d531c
jne 0x401cd0
leaq 0x30(%rsp), %rbx
movq 0xd36b5(%rip), %r14     # 0x4d5310
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x50(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401020
movl $0x8913, %esi           # imm = 0x8913
movl %ebp, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4517d0
testl %eax, %eax
js 0x401cbe
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401020
orb $0x41, 0x40(%rsp)
movl 0xd3691(%rip), %edi     # 0x4d5338
movl $0x8914, %esi           # imm = 0x8914
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4517d0
testl %eax, %eax
jns 0x401d56
movl $0x1, %edi
callq 0x44ffc0
movl 0xd366a(%rip), %ebp     # 0x4d5338
jmp 0x401c54
movq 0xd3639(%rip), %rsi     # 0x4d5310
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rbx
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movq $0x0, 0x20(%rbx)
movq %rbx, %rdi
callq 0x401020
movl $0x8913, %esi           # imm = 0x8913
movl %ebp, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4517d0
testl %eax, %eax
js 0x401d19
movzwl 0x40(%rsp), %eax
andl $0x41, %eax
cmpw $0x41, %ax
je 0x401d56
movl $0x1, %edi
callq 0x44ffc0
movq 0xd35e6(%rip), %rsi     # 0x4d5310
leaq 0x30(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movq $0x0, 0x20(%rbx)
movq %rbx, %rdi
callq 0x401020
movl 0xd35e9(%rip), %edi     # 0x4d5338
movl $0x8913, %esi           # imm = 0x8913
jmp 0x401cfd
movw $0xffff, 0xd35d5(%rip)  # imm = 0xFFFF
movl $0xffffffff, 0xd35c7(%rip) # imm = 0xFFFFFFFF
movl $0x11, %edi
movl $0x2, %esi
movl $0x1168, %edx           # imm = 0x1168
callq 0x4538a0
movl %eax, 0x8(%rsp)
cmpl $-0x1, %eax
jne 0x401db5
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rbx
callq 0x404fb0
movl (%rax), %edi
callq 0x423460
leaq 0xa0309(%rip), %rsi     # 0x4a20ac
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40c970
jmp 0x40222e
movq 0xd3554(%rip), %rdi     # 0x4d5310
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rdi, %rdi
jne 0x402171
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x402238
movq %rax, %r15
movq %rax, %rdi
callq 0x402289
movq %r15, %rdi
movl $0x4, %esi
callq 0x40241c
movl 0x8(%rsp), %edi
movl %ebp, 0x10(%rsp)
movl %ebp, %esi
movq %r15, %rdx
callq 0x401a47
leaq 0x20(%rsp), %rbx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rsi
movl $0x1, %edx
callq 0x44ff00
testl %eax, %eax
jle 0x401e75
movl %r14d, %edi
callq 0x450bf0
leaq 0xc(%rsp), %rdi
callq 0x401713
cmpl $-0x1, %eax
je 0x40222e
movl %eax, %r13d
cmpl $0x0, 0xd34d6(%rip)     # 0x4d5320
je 0x401e7d
leaq 0xa0295(%rip), %rdi     # 0x4a20e8
movl %r13d, %esi
xorl %eax, %eax
callq 0x40ca30
movl 0xc(%rsp), %r14d
leaq 0xa028f(%rip), %rdi     # 0x4a20f8
movl %r14d, %esi
xorl %eax, %eax
callq 0x40ca30
jmp 0x401e82
cmpl $-0x1, %r13d
jne 0x401e82
jmp 0x401e2d
movl 0xc(%rsp), %r14d
movl 0x8(%rsp), %eax
movl %eax, 0x20(%rsp)
movl $0x1, %eax
movl %eax, 0x24(%rsp)
movl %r14d, 0x28(%rsp)
movl %eax, 0x2c(%rsp)
movl $0x2, %esi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4513a0
testl %eax, %eax
je 0x4021e8
testb $0x1, 0x2e(%rsp)
je 0x401f54
cmpl $0x0, 0xd3458(%rip)     # 0x4d5320
je 0x401ed6
leaq 0xa030e(%rip), %rdi     # 0x4a21df
callq 0x413790
movq %r15, %rdi
callq 0x402289
movq %r15, %rdi
movl $0x4, %esi
callq 0x40241c
movq %r15, %rdi
xorl %esi, %esi
callq 0x40242f
movq %rax, %r12
movq %r15, %rdi
callq 0x402428
movslq %eax, %rdx
movl %r14d, %edi
movq %r12, %rsi
callq 0x450a80
cmpq $-0x1, %rax
je 0x40220e
movq %rax, %rbp
movb $0x0, (%r12,%rax)
cmpl $0x0, 0xd33f9(%rip)     # 0x4d5320
je 0x401f3a
leaq 0xa01e5(%rip), %rdi     # 0x4a2115
movq %rbp, %rsi
xorl %eax, %eax
callq 0x40ca30
movq %r15, %rdi
movl %ebp, %esi
callq 0x40242f
movl 0x8(%rsp), %edi
movl 0x10(%rsp), %esi
movq %r15, %rdx
callq 0x40198e
cmpw $0x0, 0x26(%rsp)
je 0x401e0f
movq %r15, %rdi
callq 0x402289
movq %r15, %rdi
xorl %esi, %esi
callq 0x40242f
movq %rax, %r12
movq %r15, %rdi
callq 0x402428
movslq %eax, %rdx
movl 0x8(%rsp), %edi
movq %r12, %rsi
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0x453700
cmpq $-0x1, %rax
je 0x4021f6
movq %rax, %rbp
movq %r15, %rdi
movl %ebp, %esi
callq 0x40242f
cmpl $0x0, 0xd336a(%rip)     # 0x4d5320
je 0x401fc9
movl (%r15), %esi
leaq 0xa0185(%rip), %rdi     # 0x4a2147
xorl %eax, %eax
callq 0x40ca30
cmpw $0x1168, 0x32(%rsp)     # imm = 0x1168
jne 0x401e0f
cmpl $0x0, 0xd3343(%rip)     # 0x4d5320
je 0x401feb
leaq 0xa0206(%rip), %rdi     # 0x4a21ec
callq 0x413790
movq 0x38(%r15), %rax
movzbl (%rax), %esi
cmpq $0x6, %rsi
ja 0x40210e
leaq 0xa0001(%rip), %rdx     # 0x4a2004
movslq (%rdx,%rsi,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %r15, %rdi
callq 0x402289
movq %r15, %rdi
movl $0x4, %esi
callq 0x40241c
movl 0x8(%rsp), %edi
movl 0x10(%rsp), %esi
movq %r15, %rdx
callq 0x401a47
jmp 0x401e0f
movzbl 0x1(%rax), %esi
movl 0xd32c8(%rip), %edx     # 0x4d5308
cmpl %esi, %edx
jne 0x4020ee
leaq 0x3c(%rsp), %rdx
movzwl 0x4(%rdx), %ecx
movw %cx, 0xd32dc(%rip)      # 0x4d5334
movl (%rdx), %ecx
movl %ecx, 0xd32d0(%rip)     # 0x4d5330
movzwl 0x2(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %esi
cmpq %rsi, %rbp
jge 0x40212e
leaq 0xa0123(%rip), %rdi     # 0x4a219e
jmp 0x402122
movl %r13d, %edi
movl $0x9, %esi
callq 0x40ad20
jmp 0x401e0f
movzbl 0x1(%rax), %esi
movl 0xd326c(%rip), %edx     # 0x4d5308
cmpl %esi, %edx
jne 0x4020ee
movzbl 0x2(%rax), %ecx
movw %cx, 0x14(%rsp)
movzbl 0x3(%rax), %eax
movw %ax, 0x16(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x5414, %esi           # imm = 0x5414
movl %r14d, %edi
leaq 0x14(%rsp), %rdx
xorl %eax, %eax
callq 0x4517d0
cmpl $0x0, 0xd324b(%rip)     # 0x4d5320
je 0x401e0f
movzwl 0x14(%rsp), %esi
movzwl 0x16(%rsp), %edx
leaq 0xa008e(%rip), %rdi     # 0x4a217a
jmp 0x402102
cmpl $0x0, 0xd322b(%rip)     # 0x4d5320
je 0x401e0f
leaq 0xa005f(%rip), %rdi     # 0x4a2161
xorl %eax, %eax
callq 0x40ca30
jmp 0x401e0f
cmpl $0x0, 0xd320b(%rip)     # 0x4d5320
je 0x401e0f
leaq 0xa0069(%rip), %rdi     # 0x4a218b
xorl %eax, %eax
callq 0x40ca30
jmp 0x401e0f
movq %r15, %rdi
callq 0x40245c
movq %r15, %rdi
movl $0x4, %esi
callq 0x40244f
cmpl $0x0, 0xd31d6(%rip)     # 0x4d5320
je 0x40215d
movl (%r15), %esi
leaq 0xa0063(%rip), %rdi     # 0x4a21b9
xorl %eax, %eax
callq 0x40ca30
movq 0x38(%r15), %rsi
movl (%r15), %edx
movl %r14d, %edi
callq 0x450b20
jmp 0x401e0f
callq 0x453c30
testl %eax, %eax
jne 0x402195
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0x9ff2f(%rip), %rdi     # 0x4a20ba
movl $0x11, %esi
jmp 0x402224
movl %eax, %ebp
js 0x401dd0
leaq 0x30(%rsp), %rsi
movl $0x0, 0x10(%rsi)
movq $0x0, 0x8(%rsi)
movl $0x11680011, (%rsi)     # imm = 0x11680011
movl %ebp, 0x4(%rsi)
movl 0x8(%rsp), %edi
movl $0x14, %edx
callq 0x4536d0
testl %eax, %eax
je 0x401dd0
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0x9feeb(%rip), %rdi     # 0x4a20cc
movl $0x1b, %esi
jmp 0x402224
leaq 0xa000d(%rip), %rdi     # 0x4a21fc
callq 0x413790
jmp 0x40222e
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0x9ff1f(%rip), %rdi     # 0x4a2126
movl $0x20, %esi
jmp 0x402224
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0x9fee7(%rip), %rdi     # 0x4a2106
movl $0xe, %esi
movl $0x1, %edx
callq 0x4134c0
movl $0x1, %edi
callq 0x40b8d0