// File: /workspace/github_c_makefile_star2_O2/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 0x17d5
movq %rsi, %rbx
movq 0x8(%rsi), %rax
cmpb $0x2d, (%rax)
jne 0x16d2
cmpb $0x3f, 0x1(%rax)
jne 0x16d2
cmpb $0x0, 0x2(%rax)
je 0x17d5
movq 0x10(%rbx), %r14
cmpb $0x2f, (%r14)
jne 0x16e7
cmpb $0x0, 0x1(%r14)
je 0x17bc
leaq 0xe2c(%rip), %rsi       # 0x251a
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x17bc
leaq 0xe17(%rip), %rsi       # 0x251f
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x17bc
leaq 0xe02(%rip), %rsi       # 0x2524
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x17bc
leaq 0xded(%rip), %rsi       # 0x2529
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x17bc
leaq 0xddc(%rip), %rsi       # 0x252e
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x17bc
leaq 0xdcb(%rip), %rsi       # 0x2533
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x17bc
leaq 0xdba(%rip), %rsi       # 0x2538
pushq $0x6
popq %rdx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x17bc
movq %r14, %rdi
callq 0x1090
cmpl $-0x1, %eax
je 0x181f
callq 0x1190
testl %eax, %eax
je 0x1835
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd7b(%rip), %rdi       # 0x253e
movq %r14, %rsi
xorl %eax, %eax
callq 0x10a0
pushq $0x3
popq %rdi
callq 0x1160
leaq 0xbf6(%rip), %rdi       # 0x23d2
pushq $0x17
popq %rsi
xorl %eax, %eax
callq 0x10a0
leaq 0x2903(%rip), %r14      # 0x40f0
leaq 0xd22(%rip), %rbx       # 0x2516
movq (%r14), %rsi
testq %rsi, %rsi
je 0x180c
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
addq $0x10, %r14
jmp 0x17f4
leaq 0xdd6(%rip), %rdi       # 0x25e9
callq 0x1050
xorl %edi, %edi
callq 0x1160
movq 0x10(%rbx), %rsi
leaq 0xd3e(%rip), %rdi       # 0x2568
xorl %eax, %eax
callq 0x10a0
pushq $0x4
jmp 0x17cf
pushq $0x11
popq %rdi
pushq $0x1
popq %r14
movq %r14, %rsi
callq 0x10e0
pushq $0x1
popq %rdi
movq %r14, %rsi
callq 0x10e0
xorl %ebp, %ebp
cmpl $0x20, %ebp
je 0x1861
movl %ebp, %edi
callq 0x10c0
incl %ebp
jmp 0x1851
callq 0x1180
movq 0x8(%rbx), %r14
callq 0x1070
leaq 0xd17(%rip), %rsi       # 0x258d
pushq $0x2c
popq %rdi
movq %r14, %rdx
movl %eax, %ecx
callq 0x12ac
pushq $0x2
popq %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
xorl %edx, %edx
callq 0x11a0
movl %eax, %r14d
testl %eax, %eax
jns 0x18b1
leaq 0xcfb(%rip), %rsi       # 0x259b
leaq 0xd00(%rip), %rdx       # 0x25a7
pushq $0x2a
popq %rdi
xorl %ecx, %ecx
callq 0x12ac
movq 0x8(%rbx), %r15
movq %r15, %rdi
callq 0x1140
movl %eax, %ebx
cmpl $0xea61, %eax           # imm = 0xEA61
jb 0x18da
leaq 0xce1(%rip), %rsi       # 0x25ae
pushq $0x2a
popq %rdi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x12ac
movw $0x2, 0x4881(%rip)      # 0x6164
andl $0x0, 0x487e(%rip)      # 0x6168
leaq 0x4873(%rip), %rsi      # 0x6164
rolw $0x8, %bx
movw %bx, 0x486a(%rip)       # 0x6166
pushq $0x10
popq %rdx
movl %r14d, %edi
callq 0x1110
testl %eax, %eax
jns 0x1923
leaq 0xc89(%rip), %rsi       # 0x259b
leaq 0xcb8(%rip), %rdx       # 0x25d1
pushq $0x2a
popq %rdi
xorl %ecx, %ecx
callq 0x12ac
pushq $0x40
popq %rsi
movl %r14d, %edi
callq 0x10f0
testl %eax, %eax
jns 0x194a
leaq 0xc62(%rip), %rsi       # 0x259b
leaq 0xc96(%rip), %rdx       # 0x25d6
pushq $0x2a
popq %rdi
xorl %ecx, %ecx
callq 0x12ac
leaq 0x4803(%rip), %rbx      # 0x6154
leaq 0x4(%rsp), %r15
pushq $0x2a
popq %r13
movl $0x10, 0x4(%rsp)
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1130
movl %eax, %r12d
testl %eax, %eax
jns 0x198f
movl %r13d, %edi
leaq 0xc1a(%rip), %rsi       # 0x259b
leaq 0xc55(%rip), %rdx       # 0x25dd
xorl %ecx, %ecx
callq 0x12ac
callq 0x1190
testl %eax, %eax
js 0x19a4
je 0x19c0
movl %r12d, %edi
callq 0x10c0
jmp 0x19bc
movl %r13d, %edi
leaq 0xbed(%rip), %rsi       # 0x259b
leaq 0xc2f(%rip), %rdx       # 0x25e4
xorl %ecx, %ecx
callq 0x12ac
incl %ebp
jmp 0x195a
movl %r14d, %edi
callq 0x10c0
movl %r12d, %edi
movl %ebp, %esi
callq 0x13e2