// File: /workspace/github_c_makefile_star2_O3/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 0x183a
movq %rsi, %rbx
movq 0x8(%rsi), %rax
cmpb $0x2d, (%rax)
jne 0x1727
cmpb $0x3f, 0x1(%rax)
jne 0x1727
cmpb $0x0, 0x2(%rax)
je 0x183a
movq 0x10(%rbx), %r14
cmpb $0x2f, (%r14)
jne 0x173c
cmpb $0x0, 0x1(%r14)
je 0x181f
leaq 0xdd7(%rip), %rsi       # 0x251a
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x181f
leaq 0xdc0(%rip), %rsi       # 0x251f
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x181f
leaq 0xda9(%rip), %rsi       # 0x2524
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x181f
leaq 0xd92(%rip), %rsi       # 0x2529
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x181f
leaq 0xd7f(%rip), %rsi       # 0x252e
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x181f
leaq 0xd6c(%rip), %rsi       # 0x2533
movl $0x5, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x181f
leaq 0xd59(%rip), %rsi       # 0x2538
movl $0x6, %edx
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x181f
movq %r14, %rdi
callq 0x1090
cmpl $-0x1, %eax
je 0x1890
callq 0x1180
testl %eax, %eax
je 0x18ac
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd18(%rip), %rdi       # 0x253e
movq %r14, %rsi
xorl %eax, %eax
callq 0x10a0
movl $0x3, %edi
callq 0x1150
leaq 0xb91(%rip), %rdi       # 0x23d2
movl $0x17, %esi
xorl %eax, %eax
callq 0x10a0
movq 0x288c(%rip), %rsi      # 0x40e0
testq %rsi, %rsi
je 0x187d
leaq 0x2890(%rip), %r14      # 0x40f0
leaq 0xcaf(%rip), %rbx       # 0x2516
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
movq (%r14), %rsi
addq $0x10, %r14
testq %rsi, %rsi
jne 0x1867
leaq 0xd65(%rip), %rdi       # 0x25e9
callq 0x1050
xorl %edi, %edi
callq 0x1150
movq 0x10(%rbx), %rsi
leaq 0xccd(%rip), %rdi       # 0x2568
xorl %eax, %eax
callq 0x10a0
movl $0x4, %edi
callq 0x1150
movl $0x1, %esi
movl $0x11, %edi
callq 0x10e0
movl $0x1, %esi
movl $0x1, %edi
callq 0x10e0
xorl %ebp, %ebp
movl %ebp, %edi
callq 0x10c0
incl %ebp
cmpl $0x20, %ebp
jne 0x18cc
callq 0x1170
movq 0x8(%rbx), %r14
callq 0x1070
leaq 0xc9e(%rip), %rsi       # 0x258d
movl $0x2c, %edi
movq %r14, %rdx
movl %eax, %ecx
callq 0x129c
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1190
movl %eax, %ebp
testl %eax, %eax
jns 0x192f
leaq 0xc7f(%rip), %rsi       # 0x259b
leaq 0xc84(%rip), %rdx       # 0x25a7
movl $0x2a, %edi
xorl %ecx, %ecx
callq 0x129c
movq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x1130
movl %eax, %ebx
cmpl $0xea61, %eax           # imm = 0xEA61
jb 0x195a
leaq 0xc63(%rip), %rsi       # 0x25ae
movl $0x2a, %edi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x129c
movw $0x2, 0x47f1(%rip)      # 0x6154
leaq 0x47ea(%rip), %rsi      # 0x6154
movl $0x0, 0x47e4(%rip)      # 0x6158
rolw $0x8, %bx
movw %bx, 0x47d7(%rip)       # 0x6156
movl %ebp, %edi
movl $0x10, %edx
callq 0x1100
testl %eax, %eax
jns 0x19a9
leaq 0xc05(%rip), %rsi       # 0x259b
leaq 0xc34(%rip), %rdx       # 0x25d1
movl $0x2a, %edi
xorl %ecx, %ecx
callq 0x129c
movl %ebp, %edi
movl $0x40, %esi
callq 0x10f0
testl %eax, %eax
jns 0x19d3
leaq 0xbdb(%rip), %rsi       # 0x259b
leaq 0xc0f(%rip), %rdx       # 0x25d6
movl $0x2a, %edi
xorl %ecx, %ecx
callq 0x129c
movl $0x1, %ebx
leaq 0x4765(%rip), %r14      # 0x6144
leaq 0x4(%rsp), %r15
leaq 0xbb0(%rip), %r12       # 0x259b
movl $0x10, 0x4(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1120
movl %eax, %r13d
testl %eax, %eax
jns 0x1a1d
movl $0x2a, %edi
movq %r12, %rsi
leaq 0xbc7(%rip), %rdx       # 0x25dd
xorl %ecx, %ecx
callq 0x129c
callq 0x1180
testl %eax, %eax
js 0x1a32
je 0x1a4c
movl %r13d, %edi
callq 0x10c0
jmp 0x1a48
movl $0x2a, %edi
movq %r12, %rsi
leaq 0xba3(%rip), %rdx       # 0x25e4
xorl %ecx, %ecx
callq 0x129c
incl %ebx
jmp 0x19eb
movl %ebp, %edi
callq 0x10c0
movl %r13d, %edi
movl %ebx, %esi
callq 0x13e1