// File: /workspace/github_c_makefile_star2_O1/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, 0xd383e(%rip)     # 0x4d5340
movl $0x0, 0xd3814(%rip)     # 0x4d5320
leaq 0xa0585(%rip), %rcx     # 0x4a2098
movq %rcx, 0xd37f6(%rip)     # 0x4d5310
movl %eax, 0xd3818(%rip)     # 0x4d5338
cmpl $0x1, %edi
jle 0x401bef
movq %rsi, %rbx
movl %edi, %r15d
incq %r15
leaq 0xa643c(%rip), %r14     # 0x4a7f75
leaq 0xa055d(%rip), %r12     # 0x4a209d
leaq 0xa055e(%rip), %r13     # 0x4a20a5
movq -0x10(%rbx,%r15,8), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401ba4
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401bbc
movq %rbp, %rdi
movq %r13, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401bc8
movq %rbp, %rdi
callq 0x4010d8
cmpq $0x2, %rax
ja 0x401b9b
callq 0x405160
movq (%rax), %rax
movsbq (%rbp), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x401bd4
movq %rbp, 0xd376e(%rip)     # 0x4d5310
jmp 0x401be2
leaq 0xa0659(%rip), %rdi     # 0x4a2204
callq 0x413780
movl $0x1, 0xd3766(%rip)     # 0x4d5320
jmp 0x401be2
movl $0x1, 0xd3752(%rip)     # 0x4d5318
jmp 0x401be2
movl $0x1, 0xd374a(%rip)     # 0x4d531c
jmp 0x401be2
movq %rbp, %rdi
callq 0x40ad40
movl %eax, 0xd3726(%rip)     # 0x4d5308
decq %r15
cmpq $0x2, %r15
jg 0x401b47
xorl %ebx, %ebx
leaq 0xa0428(%rip), %r14     # 0x4a2020
movl (%rbx,%r14), %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x4538a0
testl %eax, %eax
jns 0x401c1b
addq $0x4, %rbx
cmpq $0xc, %rbx
jne 0x401bf8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0xd3717(%rip)     # 0x4d5338
cmpl $0x0, 0xd36f4(%rip)     # 0x4d531c
jne 0x401ca5
leaq 0x30(%rsp), %rbx
movq 0xd36da(%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 0xd36db(%rip), %edi     # 0x4d5338
movl $0x8913, %esi           # imm = 0x8913
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4517d0
testl %eax, %eax
js 0x401c99
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401020
orb $0x41, 0x40(%rsp)
movl 0xd36b2(%rip), %edi     # 0x4d5338
movl $0x8914, %esi           # imm = 0x8914
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4517d0
testl %eax, %eax
jns 0x401d0d
movl $0x1, %edi
callq 0x44ffc0
jmp 0x401c2f
movq 0xd3664(%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 0xd3667(%rip), %edi     # 0x4d5338
movl $0x8913, %esi           # imm = 0x8913
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4517d0
testl %eax, %eax
js 0x401cf2
movzwl 0x40(%rsp), %eax
andl $0x41, %eax
cmpw $0x41, %ax
je 0x401d0d
movl $0x1, %edi
callq 0x44ffc0
movq 0xd360d(%rip), %rsi     # 0x4d5310
leaq 0x30(%rsp), %rbx
xorps %xmm0, %xmm0
jmp 0x401cb4
movw $0xffff, 0xd361e(%rip)  # imm = 0xFFFF
movl $0xffffffff, 0xd3610(%rip) # imm = 0xFFFFFFFF
movl $0x11, %edi
movl $0x2, %esi
movl $0x1168, %edx           # imm = 0x1168
callq 0x4538a0
movl %eax, 0x8(%rsp)
cmpl $-0x1, %eax
jne 0x401d6c
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rbx
callq 0x404fa0
movl (%rax), %edi
callq 0x423450
leaq 0xa0352(%rip), %rsi     # 0x4a20ac
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40c960
jmp 0x4021d6
movq 0xd359d(%rip), %rdi     # 0x4d5310
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x401da3
callq 0x453c30
movl %eax, %ebp
testl %eax, %eax
jne 0x401da3
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0xa0321(%rip), %rdi     # 0x4a20ba
movl $0x11, %esi
jmp 0x4021cc
testl %ebp, %ebp
js 0x401dd8
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
movl $0x0, 0x10(%rsi)
movl $0x11680011, (%rsi)     # imm = 0x11680011
movl %ebp, 0x4(%rsi)
movl 0x8(%rsp), %edi
movl $0x14, %edx
callq 0x4536d0
testl %eax, %eax
jne 0x402186
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x4021e0
movq %rax, %r14
movq %rax, %rdi
callq 0x40223e
movq %r14, %rdi
movl $0x4, %esi
callq 0x40240b
movl 0x8(%rsp), %edi
movl %ebp, 0x10(%rsp)
movl %ebp, %esi
movq %r14, %rdx
callq 0x401a2d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
leaq 0x20(%rsp), %rbx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rsi
movl $0x1, %edx
callq 0x44ff00
testl %eax, %eax
jle 0x401e3c
movl 0xc(%rsp), %edi
callq 0x450bf0
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $-0x1, %r13d
jne 0x401e84
leaq 0xc(%rsp), %rdi
callq 0x401713
cmpl $-0x1, %eax
je 0x4021d6
movl %eax, %r13d
cmpl $0x0, 0xd34c1(%rip)     # 0x4d5320
je 0x401e84
leaq 0xa0280(%rip), %rdi     # 0x4a20e8
movl %r13d, %esi
xorl %eax, %eax
callq 0x40ca20
movl 0xc(%rsp), %esi
leaq 0xa027b(%rip), %rdi     # 0x4a20f8
xorl %eax, %eax
callq 0x40ca20
movl 0x8(%rsp), %eax
movl %eax, 0x20(%rsp)
movl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0xc(%rsp), %r12d
movl %r12d, 0x28(%rsp)
movl %eax, 0x2c(%rsp)
movl $0x2, %esi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4513a0
testl %eax, %eax
je 0x402178
testb $0x1, 0x2e(%rsp)
je 0x401f5b
cmpl $0x0, 0xd3451(%rip)     # 0x4d5320
je 0x401edd
leaq 0xa0307(%rip), %rdi     # 0x4a21df
callq 0x413780
movq %r14, %rdi
callq 0x40223e
movq %r14, %rdi
movl $0x4, %esi
callq 0x40240b
movq %r14, %rdi
xorl %esi, %esi
callq 0x40241e
movq %rax, %r15
movq %r14, %rdi
callq 0x402417
movslq %eax, %rdx
movl %r12d, %edi
movq %r15, %rsi
callq 0x450a80
cmpq $-0x1, %rax
je 0x4021b6
movq %rax, %rbp
movb $0x0, (%r15,%rax)
cmpl $0x0, 0xd33f2(%rip)     # 0x4d5320
je 0x401f41
leaq 0xa01de(%rip), %rdi     # 0x4a2115
movq %rbp, %rsi
xorl %eax, %eax
callq 0x40ca20
movq %r14, %rdi
movl %ebp, %esi
callq 0x40241e
movl 0x8(%rsp), %edi
movl 0x10(%rsp), %esi
movq %r14, %rdx
callq 0x401977
cmpw $0x0, 0x26(%rsp)
je 0x401e17
movq %r14, %rdi
callq 0x40223e
movq %r14, %rdi
xorl %esi, %esi
callq 0x40241e
movq %rax, %r15
movq %r14, %rdi
callq 0x402417
movslq %eax, %rdx
movl 0x8(%rsp), %edi
movq %r15, %rsi
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0x453700
cmpq $-0x1, %rax
je 0x40219e
movq %rax, %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x40241e
cmpl $0x0, 0xd3363(%rip)     # 0x4d5320
je 0x401fd0
movl (%r14), %esi
leaq 0xa017e(%rip), %rdi     # 0x4a2147
xorl %eax, %eax
callq 0x40ca20
cmpw $0x1168, 0x32(%rsp)     # imm = 0x1168
jne 0x401e17
cmpl $0x0, 0xd333c(%rip)     # 0x4d5320
je 0x401ff2
leaq 0xa01ff(%rip), %rdi     # 0x4a21ec
callq 0x413780
movq 0x38(%r14), %rax
movzbl (%rax), %esi
cmpq $0x6, %rsi
ja 0x402115
leaq 0x9fffa(%rip), %rdx     # 0x4a2004
movslq (%rdx,%rsi,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %r14, %rdi
callq 0x40223e
movq %r14, %rdi
movl $0x4, %esi
callq 0x40240b
movl 0x8(%rsp), %edi
movl 0x10(%rsp), %esi
movq %r14, %rdx
callq 0x401a2d
jmp 0x401e17
movzbl 0x1(%rax), %esi
movl 0xd32c1(%rip), %edx     # 0x4d5308
cmpl %esi, %edx
jne 0x4020f5
leaq 0x3c(%rsp), %rdx
movzwl 0x4(%rdx), %ecx
movw %cx, 0xd32d5(%rip)      # 0x4d5334
movl (%rdx), %ecx
movl %ecx, 0xd32c9(%rip)     # 0x4d5330
movzwl 0x2(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %esi
cmpq %rsi, %rbp
jge 0x402135
leaq 0xa011c(%rip), %rdi     # 0x4a219e
jmp 0x402129
movl %r13d, %edi
movl $0x9, %esi
callq 0x40ad10
jmp 0x401e17
movzbl 0x1(%rax), %esi
movl 0xd3265(%rip), %edx     # 0x4d5308
cmpl %esi, %edx
jne 0x4020f5
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 %r12d, %edi
leaq 0x14(%rsp), %rdx
xorl %eax, %eax
callq 0x4517d0
cmpl $0x0, 0xd3244(%rip)     # 0x4d5320
je 0x401e17
movzwl 0x14(%rsp), %esi
movzwl 0x16(%rsp), %edx
leaq 0xa0087(%rip), %rdi     # 0x4a217a
jmp 0x402109
cmpl $0x0, 0xd3224(%rip)     # 0x4d5320
je 0x401e17
leaq 0xa0058(%rip), %rdi     # 0x4a2161
xorl %eax, %eax
callq 0x40ca20
jmp 0x401e17
cmpl $0x0, 0xd3204(%rip)     # 0x4d5320
je 0x401e17
leaq 0xa0062(%rip), %rdi     # 0x4a218b
xorl %eax, %eax
callq 0x40ca20
jmp 0x401e17
movq %r14, %rdi
callq 0x40244b
movq %r14, %rdi
movl $0x4, %esi
callq 0x40243e
cmpl $0x0, 0xd31cf(%rip)     # 0x4d5320
je 0x402164
movl (%r14), %esi
leaq 0xa005c(%rip), %rdi     # 0x4a21b9
xorl %eax, %eax
callq 0x40ca20
movq 0x38(%r14), %rsi
movl (%r14), %edx
movl %r12d, %edi
callq 0x450b20
jmp 0x401e17
leaq 0xa007d(%rip), %rdi     # 0x4a21fc
callq 0x413780
jmp 0x4021d6
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0x9ff35(%rip), %rdi     # 0x4a20cc
movl $0x1b, %esi
jmp 0x4021cc
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0x9ff77(%rip), %rdi     # 0x4a2126
movl $0x20, %esi
jmp 0x4021cc
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0x9ff3f(%rip), %rdi     # 0x4a2106
movl $0xe, %esi
movl $0x1, %edx
callq 0x4134b0
movl $0x1, %edi
callq 0x40b8c0