// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
movl $0x0, 0x34(%rsp)
movl %edi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
cmpl $0x3, 0x30(%rsp)
jl 0x19bd
cmpl $0x3, 0x30(%rsp)
jg 0x19bd
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0xa29(%rip), %rsi       # 0x23dc
callq 0x1100
cmpl $0x0, %eax
jne 0x1a37
leaq 0xa1b(%rip), %rdi       # 0x23df
movl $0x17, %esi
movb $0x0, %al
callq 0x10b0
movl $0x0, 0x24(%rsp)
movslq 0x24(%rsp), %rcx
leaq 0x271c(%rip), %rax      # 0x4100
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x1a22
movslq 0x24(%rsp), %rcx
leaq 0x2703(%rip), %rax      # 0x4100
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
leaq 0xb15(%rip), %rdi       # 0x2523
movb $0x0, %al
callq 0x10b0
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x19d8
leaq 0xafe(%rip), %rdi       # 0x2527
movb $0x0, %al
callq 0x10b0
xorl %edi, %edi
callq 0x1180
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xb9e(%rip), %rsi       # 0x25e5
movl $0x2, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1b3b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xb7d(%rip), %rsi       # 0x25e7
movl $0x5, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1b3b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xb5f(%rip), %rsi       # 0x25ec
movl $0x5, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1b3b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xb41(%rip), %rsi       # 0x25f1
movl $0x5, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1b3b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xb27(%rip), %rsi       # 0x25f6
movl $0x5, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1b3b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xb0d(%rip), %rsi       # 0x25fb
movl $0x5, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1b3b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xaf3(%rip), %rsi       # 0x2600
movl $0x5, %edx
callq 0x1040
cmpl $0x0, %eax
je 0x1b3b
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xad9(%rip), %rsi       # 0x2605
movl $0x6, %edx
callq 0x1040
cmpl $0x0, %eax
jne 0x1b5c
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0xac0(%rip), %rdi       # 0x260b
movb $0x0, %al
callq 0x10b0
movl $0x3, %edi
callq 0x1180
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1090
cmpl $-0x1, %eax
jne 0x1b90
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0xab6(%rip), %rdi       # 0x2635
movb $0x0, %al
callq 0x10b0
movl $0x4, %edi
callq 0x1180
callq 0x11b0
cmpl $0x0, %eax
je 0x1ba1
xorl %eax, %eax
addq $0x38, %rsp
retq
movl $0x11, %edi
movl $0x1, %esi
callq 0x1110
movl $0x1, %edi
movl $0x1, %esi
callq 0x1110
movl $0x0, 0x24(%rsp)
cmpl $0x20, 0x24(%rsp)
jge 0x1be4
movl 0x24(%rsp), %edi
callq 0x10e0
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x1bc7
callq 0x11a0
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
callq 0x1070
movq (%rsp), %rdx
movl %eax, %ecx
movl $0x2c, %edi
leaq 0xa4d(%rip), %rsi       # 0x265a
callq 0x12d0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11c0
movl %eax, 0x18(%rsp)
cmpl $0x0, %eax
jge 0x1c46
movl $0x2a, %edi
leaq 0xa30(%rip), %rsi       # 0x2668
leaq 0xa35(%rip), %rdx       # 0x2674
xorl %ecx, %ecx
callq 0x12d0
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x1160
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x20(%rsp)
jl 0x1c69
cmpl $0xea60, 0x20(%rsp)     # imm = 0xEA60
jle 0x1c85
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdx
movl $0x2a, %edi
leaq 0x9fd(%rip), %rsi       # 0x267b
xorl %ecx, %ecx
callq 0x12d0
movw $0x2, 0x44e6(%rip)      # 0x6174
xorl %edi, %edi
callq 0x10d0
movl %eax, 0x44dd(%rip)      # 0x6178
movl 0x20(%rsp), %eax
movzwl %ax, %edi
callq 0x10a0
movw %ax, 0x44c8(%rip)       # 0x6176
movl 0x18(%rsp), %edi
leaq 0x44bb(%rip), %rsi      # 0x6174
movl $0x10, %edx
callq 0x1130
cmpl $0x0, %eax
jge 0x1ce2
movl $0x2a, %edi
leaq 0x994(%rip), %rsi       # 0x2668
leaq 0x9c3(%rip), %rdx       # 0x269e
xorl %ecx, %ecx
callq 0x12d0
movl 0x18(%rsp), %edi
movl $0x40, %esi
callq 0x1120
cmpl $0x0, %eax
jge 0x1d0f
movl $0x2a, %edi
leaq 0x967(%rip), %rsi       # 0x2668
leaq 0x99b(%rip), %rdx       # 0x26a3
xorl %ecx, %ecx
callq 0x12d0
movl $0x1, 0x10(%rsp)
movl $0x10, 0xc(%rsp)
movl 0x18(%rsp), %edi
leaq 0x443a(%rip), %rsi      # 0x6164
leaq 0xc(%rsp), %rdx
callq 0x1150
movl %eax, 0x14(%rsp)
cmpl $0x0, %eax
jge 0x1d57
movl $0x2a, %edi
leaq 0x91f(%rip), %rsi       # 0x2668
leaq 0x95a(%rip), %rdx       # 0x26aa
xorl %ecx, %ecx
callq 0x12d0
callq 0x11b0
movl %eax, 0x1c(%rsp)
cmpl $0x0, %eax
jge 0x1d81
movl $0x2a, %edi
leaq 0x8f7(%rip), %rsi       # 0x2668
leaq 0x939(%rip), %rdx       # 0x26b1
xorl %ecx, %ecx
callq 0x12d0
jmp 0x1dab
cmpl $0x0, 0x1c(%rsp)
jne 0x1da0
movl 0x18(%rsp), %edi
callq 0x10e0
movl 0x14(%rsp), %edi
movl 0x10(%rsp), %esi
callq 0x14f0
jmp 0x1da9
movl 0x14(%rsp), %edi
callq 0x10e0
jmp 0x1dab
jmp 0x1dad
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x1d17