// File: /workspace/github_c_makefile_star2_O1/vincenthsu[P]nweb/nweb23.c
// Function: main
int main(int argc, char **argv)
{
	int i, port, pid, listenfd, socketfd, hit;
	socklen_t length;
	static struct sockaddr_in cli_addr; /* static = initialised to zeros */
	static struct sockaddr_in serv_addr; /* static = initialised to zeros */

	if( argc < 3  || argc > 3 || !strcmp(argv[1], "-?") ) {
		(void)printf("hint: nweb Port-Number Top-Directory\t\tversion %d\n\n"
	"\tnweb is a small and very safe mini web server\n"
	"\tnweb only servers out file/web pages with extensions named below\n"
	"\t and only from the named directory or its sub-directories.\n"
	"\tThere is no fancy features = safe and secure.\n\n"
	"\tExample: nweb 8181 /home/nwebdir &\n\n"
	"\tOnly Supports:", VERSION);
		for(i=0;extensions[i].ext != 0;i++)
			(void)printf(" %s",extensions[i].ext);

		(void)printf("\n\tNot Supported: URLs including \"..\", Java, Javascript, CGI\n"
	"\tNot Supported: directories / /etc /bin /lib /tmp /usr /dev /sbin \n"
	"\tNo warranty given or implied\n\tNigel Griffiths nag@uk.ibm.com\n"  );
		exit(0);
	}
	if( !strncmp(argv[2],"/"   ,2 ) || !strncmp(argv[2],"/etc", 5 ) ||
	    !strncmp(argv[2],"/bin",5 ) || !strncmp(argv[2],"/lib", 5 ) ||
	    !strncmp(argv[2],"/tmp",5 ) || !strncmp(argv[2],"/usr", 5 ) ||
	    !strncmp(argv[2],"/dev",5 ) || !strncmp(argv[2],"/sbin",6) ){
		(void)printf("ERROR: Bad top directory %s, see nweb -?\n",argv[2]);
		exit(3);
	}
	if(chdir(argv[2]) == -1){ 
		(void)printf("ERROR: Can't Change to directory %s\n",argv[2]);
		exit(4);
	}
	/* Become deamon + unstopable and no zombies children (= no wait()) */
	if(fork() != 0)
		return 0; /* parent returns OK to shell */
	(void)signal(SIGCLD, SIG_IGN); /* ignore child death */
	(void)signal(SIGHUP, SIG_IGN); /* ignore terminal hangups */
	for(i=0;i<32;i++)
		(void)close(i);		/* close open files */
	(void)setpgrp();		/* break away from process group */
	logger(LOG,"nweb starting",argv[1],getpid());
	/* setup the network socket */
	if((listenfd = socket(AF_INET, SOCK_STREAM,0)) <0)
		logger(ERROR, "system call","socket",0);
	port = atoi(argv[1]);
	if(port < 0 || port >60000)
		logger(ERROR,"Invalid port number (try 1->60000)",argv[1],0);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_port = htons(port);
	if(bind(listenfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)) <0)
		logger(ERROR,"system call","bind",0);
	if( listen(listenfd,64) <0)
		logger(ERROR,"system call","listen",0);
	for(hit=1; ;hit++) {
		length = sizeof(cli_addr);
		if((socketfd = accept(listenfd, (struct sockaddr *)&cli_addr, &length)) < 0)
			logger(ERROR,"system call","accept",0);
		if((pid = fork()) < 0) {
			logger(ERROR,"system call","fork",0);
		}
		else {
			if(pid == 0) { 	/* child */
				(void)close(listenfd);
				web(socketfd,hit); /* never returns */
			} else { 	/* parent */
				(void)close(socketfd);
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x3, %edi
jne 0x1860
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
leaq 0xc9d(%rip), %rsi       # 0x23d2
callq 0x10e0
testl %eax, %eax
je 0x1860
movq 0x10(%rbx), %r14
leaq 0xdd0(%rip), %rsi       # 0x251d
movl $0x2, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
leaq 0xdb6(%rip), %rsi       # 0x251f
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
leaq 0xd9f(%rip), %rsi       # 0x2524
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
leaq 0xd88(%rip), %rsi       # 0x2529
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
leaq 0xd71(%rip), %rsi       # 0x252e
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
leaq 0xd5e(%rip), %rsi       # 0x2533
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
leaq 0xd4b(%rip), %rsi       # 0x2538
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
leaq 0xd38(%rip), %rsi       # 0x253d
movl $0x6, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x1845
movq %r14, %rdi
callq 0x1090
cmpl $-0x1, %eax
je 0x18b6
callq 0x1190
testl %eax, %eax
je 0x18d2
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcf7(%rip), %rdi       # 0x2543
movq %r14, %rsi
xorl %eax, %eax
callq 0x10a0
movl $0x3, %edi
callq 0x1160
leaq 0xb6e(%rip), %rdi       # 0x23d5
movl $0x17, %esi
xorl %eax, %eax
callq 0x10a0
movq 0x2876(%rip), %rsi      # 0x40f0
testq %rsi, %rsi
je 0x18a3
leaq 0x287a(%rip), %r14      # 0x4100
leaq 0xc8c(%rip), %rbx       # 0x2519
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
movq (%r14), %rsi
addq $0x10, %r14
testq %rsi, %rsi
jne 0x188d
leaq 0xd44(%rip), %rdi       # 0x25ee
callq 0x1050
xorl %edi, %edi
callq 0x1160
movq 0x10(%rbx), %rsi
leaq 0xcac(%rip), %rdi       # 0x256d
xorl %eax, %eax
callq 0x10a0
movl $0x4, %edi
callq 0x1160
movl $0x1, %esi
movl $0x11, %edi
callq 0x10f0
movl $0x1, %esi
movl $0x1, %edi
callq 0x10f0
xorl %ebp, %ebp
movl %ebp, %edi
callq 0x10c0
incl %ebp
cmpl $0x20, %ebp
jne 0x18f2
callq 0x1180
movq 0x8(%rbx), %r14
callq 0x1070
leaq 0xc7d(%rip), %rsi       # 0x2592
movl $0x2c, %edi
movq %r14, %rdx
movl %eax, %ecx
callq 0x12ac
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11a0
movl %eax, %ebp
testl %eax, %eax
jns 0x1955
leaq 0xc5e(%rip), %rsi       # 0x25a0
leaq 0xc63(%rip), %rdx       # 0x25ac
movl $0x2a, %edi
xorl %ecx, %ecx
callq 0x12ac
movq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x1140
movl %eax, %ebx
cmpl $0xea61, %eax           # imm = 0xEA61
jb 0x1980
leaq 0xc42(%rip), %rsi       # 0x25b3
movl $0x2a, %edi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x12ac
movw $0x2, 0x47db(%rip)      # 0x6164
leaq 0x47d4(%rip), %rsi      # 0x6164
movl $0x0, 0x47ce(%rip)      # 0x6168
rolw $0x8, %bx
movw %bx, 0x47c1(%rip)       # 0x6166
movl %ebp, %edi
movl $0x10, %edx
callq 0x1110
testl %eax, %eax
jns 0x19cf
leaq 0xbe4(%rip), %rsi       # 0x25a0
leaq 0xc13(%rip), %rdx       # 0x25d6
movl $0x2a, %edi
xorl %ecx, %ecx
callq 0x12ac
movl %ebp, %edi
movl $0x40, %esi
callq 0x1100
testl %eax, %eax
jns 0x19f9
leaq 0xbba(%rip), %rsi       # 0x25a0
leaq 0xbee(%rip), %rdx       # 0x25db
movl $0x2a, %edi
xorl %ecx, %ecx
callq 0x12ac
movl $0x1, %ebx
leaq 0x474f(%rip), %r14      # 0x6154
leaq 0x4(%rsp), %r15
leaq 0xb8f(%rip), %r12       # 0x25a0
movl $0x10, 0x4(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1130
movl %eax, %r13d
testl %eax, %eax
jns 0x1a43
movl $0x2a, %edi
movq %r12, %rsi
leaq 0xba6(%rip), %rdx       # 0x25e2
xorl %ecx, %ecx
callq 0x12ac
callq 0x1190
testl %eax, %eax
js 0x1a58
je 0x1a72
movl %r13d, %edi
callq 0x10c0
jmp 0x1a6e
movl $0x2a, %edi
movq %r12, %rsi
leaq 0xb82(%rip), %rdx       # 0x25e9
xorl %ecx, %ecx
callq 0x12ac
incl %ebx
jmp 0x1a11
movl %ebp, %edi
callq 0x10c0
movl %r13d, %edi
movl %ebx, %esi
callq 0x13f1