// File: /workspace/github_c_makefile_star2_O0/hansvanstaveren[P]BigDeal/main.c
// Function: main
int
main (int argc, char *argv[])
{
	int i;
	char message[MESSLEN];
	byte *hashcode;
	unsigned long seqno;
	dl_num dnumber;
	char filename[MESSLEN] = "";
	int c;
	char *formats = 0;
	char *ownerp;
#ifdef BIGDEALX
	int only_arg_entropy = 0;
	char *hashstr = 0;
	char *wizard = 0;
	int dangerous_code_used = 0;
#else
#define only_arg_entropy 0
#define wizard 0
#endif

	/*
	 * Some systems (looking at you MinGW) suck at unbuffering stdout
	 */

	setbuf(stdout, NULL);

	/*
	 * Say hi to operator
	 */
	printf("Big Deal version %d.%d%s%s\n\n",
		VERSION_MAJOR, VERSION_MINOR, VERSION_PAREN, VERSION_COMMENT);

#ifdef BIGDEALX
	flog = fopen(LOGNAME, "a");
#endif
	os_start();
	collect_start();

	while ((c = getopt(argc, argv, OPTION_STRING)) != -1) {
		switch(c) {
#ifdef BIGDEALX
		case 'E':
			read_entropy_from_file(optarg);
			dangerous_code_used = 1;
			break;
		case 'e':
			collect_more((byte *) optarg, strlen(optarg), 4*strlen(optarg));
			dangerous_code_used = 1;
			break;
		case 'h':
			hashstr = optarg;
			/* fall through */
		case 'o':
			only_arg_entropy = 1;
			dangerous_code_used = 1;
			break;
		case 'W': /* Wizard mode */
			only_arg_entropy = 1;
			wizard = optarg;
			break;
#endif
		case 'p':
			strncpy(filename, optarg, MESSLEN-1);
			break;
		case 'f':
			formats = optarg;
			break;
		case 'n':
			setboards(optarg);
			break;
		case 'R':
			read_init_file(os_init_file_name(), 0);
			write_init_file(os_init_file_name());
			exit(0);
		case '?':
			fprintf(stderr, "Usage: %s %s\n", argv[0], USAGE_STRING);
			exit(-1);
		}
	}

	if (!only_arg_entropy)
		os_collect();

	read_init_file(os_init_file_name(), 1);
	binomial_start();
	init_goedel();
	/*
	 * Read number of boards to generate
	 */
	while(parameters.pp_nboards <= 0 || parameters.pp_nboards > MAXDEALS) {
		/*
		 * If we are asked to generate more than 100 boards refuse
		 * We only have 160 bits entropy, and no one plays sessions
		 * of more than 100 boards anyhow
		 */
		if (parameters.pp_nboards > MAXDEALS) {
			parameters.pp_nboards = 0;
			printf("The maximum is %d, run program again for more\n",
				MAXDEALS);
		}
		printf("Number of boards to deal(1-%d): ", MAXDEALS);
		(void) readline(stdin, message, MESSLEN);
		setboards(message);
		/*
		 * Collect the string, no entropy assumed
		 */
		if (!only_arg_entropy)
			collect_more( (byte*)message, strlen(message), 0);
	}

	/*
	 * Read part of filename before the .
	 */
	while(!legal_filename_prefix(filename)) { 
		printf("Output filename(without suffix): ");
		(void) readline(stdin, message, MESSLEN);
		/*
		 * Collect the string, 5 bits entropy assumed
		 */
		if (!only_arg_entropy)
			collect_more( (byte*)message, strlen(message), 5);
		strcpy(filename,message);
	}

	/*
	 * Get output formats
	 */
	if (formats == 0) {
		/*
		 * Not specified on command line
		 */
		if (!wizard && strcmp(default_askformats, "yes")==0) {
			printf("Hand format(s) to generate: [%s] ",
				default_formats);
			(void) readline(stdin, message, MESSLEN);
			if (message[0]) {
				strcpy(default_formats, message);
			}
		}
		formats = default_formats;
	}

	/*
	 * If we do not have enough entropy collected (very likely)
	 * let the user supply it by rattling his keyboard
	 */
	if (!only_arg_entropy && nrandombits < ENTROPY_TO_COLLECT) {
		get_entropy_from_keyboard();
	}


	/*
	 * Extract the entropy into the random part of the seed
	 */
	collect_finish(seed.seed_random);
#ifdef BIGDEALX
	if (!wizard && nrandombits < ENTROPY_TO_COLLECT) {
		/*
		 * Can only happen when only_arg_entropy is set
		 */
		printf("WARNING: entropy supplied is dangerously low!!!\n");
	}
	if (!wizard && dangerous_code_used) {
		printf("You used features in this extended version of Big Deal\n");
		printf("that might defeat the purpose(generating unique sequences).\n");
		printf("Use hands for actual play only with permission from your national authority.\n\n");
	}

	/*
	 * Did someone use the -h flag ?
	 */
	if (hashstr)
		random_set(hashstr, seed.seed_random);

	/*
	 * Start checking for duplicate hashes, and log current hash
	 */
	for (i=0; i<RMDbytes; i++) {
		message[2*i  ] = "0123456789ABCDEF"[seed.seed_random[i]/16];
		message[2*i+1] = "0123456789ABCDEF"[seed.seed_random[i]%16];
	}
	message[2*RMDbytes] = 0;
	if (!hashstr && !wizard)
		checkduphash(message);		/* If this fails .... */

	fprintf(flog, "Deal %d boards to file %s, hash = %s\n",
		parameters.pp_nboards, filename, message);
#endif
	(void) output_specify_formats(formats, 1);
	output_createfiles(filename, &parameters);

	/*
	 * Generate a sequence of 160 bit pseudo random numbers
	 * by running the random bits with an increasing
	 * counter through the cryptographical hash.
	 * This according to Carl Ellison's recommendation for P1363
	 *
	 * But first put hash of owner ident into seed
	 *
	 * In Wizard mode use wizard string iso owner
	 */
	ownerp = wizard ? wizard : default_owner;
	hashcode = RMDhash((byte *) ownerp, strlen(ownerp));
	memcpy(seed.seed_owner, hashcode, RMDbytes);

	seqno = 0;
	for(i = parameters.pp_lowboard; i <= parameters.pp_highboard; i++) { 
		/*
		 * Next do loop executed as long as we have the misfortune
		 * to make 96 bit numbers above the number of hands
		 */
		do {
			seqno++;
			/*
			 * The next code guarantees the same sequence on
			 * litle endian and big endian machines, which
			 * makes it possible to reproduce sequences on each
			 */
			seed.seed_sequence[0] = seqno & 0xFF;
			seed.seed_sequence[1] = (seqno>>8) & 0xFF;
			seed.seed_sequence[2] = (seqno>>16) & 0xFF;
			seed.seed_sequence[3] = (seqno>>24) & 0xFF;
			/*
			 * Run all the bits through the hash
			 */
			hashcode = RMDhash((byte *) &seed, sizeof(seed));
			/*
			 * Take the first L bytes(96 bits) as a candidate
			 * hand number
			 */
			memcpy(dnumber.dn_num, hashcode, L);
		} while(!goedel(dnumber.dn_num));
		/*
		 * Ok, got one
		 * Print it in all desired formats
		 */
		/* fprintf(stderr, "Hand %d made from hash %ld\n", i, seqno); */
		output_hand(i, &dnumber);
	}

	/*
	 * Finished, close output files
	 */
	output_closefiles();
#ifdef BIGDEALX
	fclose(flog);
#endif

	/*
	 * Do whatever our OS wants us to do at the end, and then exit
	 */
	os_finish();
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x64, %edx
callq 0x20d0
movq $0x0, -0x110(%rbp)
movq 0x5f2a(%rip), %rax      # 0xbfc8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2080
leaq 0x3282(%rip), %rdi      # 0x9333
movl $0x2, %esi
xorl %edx, %edx
leaq 0x33d1(%rip), %r8       # 0x9490
movq %r8, %rcx
movb $0x0, %al
callq 0x20a0
callq 0x8690
callq 0x67b0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x326f(%rip), %rdx      # 0x9350
callq 0x2190
movl %eax, -0x104(%rbp)
cmpl $-0x1, %eax
je 0x61c0
movl -0x104(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0x120(%rbp)
subl $0x31, %eax
ja 0x61bb
movq -0x120(%rbp), %rax
leaq 0x2ee6(%rip), %rcx      # 0x9004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x100(%rbp), %rdi
movq 0x5ea3(%rip), %rax      # 0xbfd8
movq (%rax), %rsi
movl $0x63, %edx
callq 0x2030
jmp 0x61bb
movq 0x5e8d(%rip), %rax      # 0xbfd8
movq (%rax), %rax
movq %rax, -0x110(%rbp)
jmp 0x61bb
movq 0x5e7a(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
callq 0x5fd0
jmp 0x61bb
callq 0x8750
movq %rax, %rdi
xorl %esi, %esi
callq 0x5e00
callq 0x8750
movq %rax, %rdi
callq 0x5b70
xorl %edi, %edi
callq 0x21c0
movq 0x5e66(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x31b5(%rip), %rsi      # 0x9358
leaq 0x31bc(%rip), %rcx      # 0x9366
movb $0x0, %al
callq 0x2150
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21c0
jmp 0x60d3
callq 0x8500
callq 0x8750
movq %rax, %rdi
movl $0x1, %esi
callq 0x5e00
callq 0x5820
callq 0x6730
movb $0x1, %al
cmpl $0x0, 0x8a9e(%rip)      # 0xec88
movb %al, -0x121(%rbp)
jle 0x6202
cmpl $0x64, 0x8a8f(%rip)     # 0xec88
setg %al
movb %al, -0x121(%rbp)
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0x6211
jmp 0x6294
cmpl $0x64, 0x8a70(%rip)     # 0xec88
jle 0x6237
movl $0x0, 0x8a64(%rip)      # 0xec88
leaq 0x318d(%rip), %rdi      # 0x93b8
movl $0x64, %esi
movb $0x0, %al
callq 0x20a0
leaq 0x31a9(%rip), %rdi      # 0x93e7
movl $0x64, %esi
movb $0x0, %al
callq 0x20a0
movq 0x5d7f(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
leaq -0x80(%rbp), %rsi
movl $0x64, %edx
callq 0x5d90
leaq -0x80(%rbp), %rdi
callq 0x5fd0
leaq -0x80(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x2070
movq -0x130(%rbp), %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x67e0
jmp 0x61e1
jmp 0x6296
leaq -0x100(%rbp), %rdi
callq 0x86c0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x62b0
jmp 0x630f
leaq 0x3151(%rip), %rdi      # 0x9408
movb $0x0, %al
callq 0x20a0
movq 0x5d0b(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
leaq -0x80(%rbp), %rsi
movl $0x64, %edx
callq 0x5d90
leaq -0x80(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x2070
movq -0x138(%rbp), %rdi
movl %eax, %esi
movl $0x5, %edx
callq 0x67e0
leaq -0x100(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2040
jmp 0x6296
cmpq $0x0, -0x110(%rbp)
jne 0x6384
leaq 0x5e50(%rip), %rdi      # 0xc170
leaq 0x2eed(%rip), %rsi      # 0x9214
callq 0x2120
cmpl $0x0, %eax
jne 0x6376
leaq 0x30f2(%rip), %rdi      # 0x942a
leaq 0x5dc1(%rip), %rsi      # 0xc100
movb $0x0, %al
callq 0x20a0
movq 0x5c83(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
leaq -0x80(%rbp), %rsi
movl $0x64, %edx
callq 0x5d90
cmpb $0x0, -0x80(%rbp)
je 0x6374
leaq -0x80(%rbp), %rsi
leaq 0x5d91(%rip), %rdi      # 0xc100
callq 0x2040
jmp 0x6376
leaq 0x5d83(%rip), %rax      # 0xc100
movq %rax, -0x110(%rbp)
leaq 0x8981(%rip), %rax      # 0xed0c
cmpl $0xd5, (%rax)
jge 0x6398
callq 0x6550
leaq 0x88ed(%rip), %rdi      # 0xec8c
addq $0x4, %rdi
callq 0x6900
movq -0x110(%rbp), %rdi
movl $0x1, %esi
callq 0x6b50
leaq -0x100(%rbp), %rdi
leaq 0x88b9(%rip), %rsi      # 0xec80
callq 0x6e70
leaq 0x5e0d(%rip), %rax      # 0xc1e0
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x118(%rbp), %rdi
callq 0x2070
movq -0x140(%rbp), %rdi
movl %eax, %esi
callq 0x65d0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %rcx
movq %rcx, 0x888a(%rip)      # 0xeca4
movq 0x8(%rax), %rcx
movq %rcx, 0x8887(%rip)      # 0xecac
movl 0x10(%rax), %eax
movl %eax, 0x8886(%rip)      # 0xecb4
movq $0x0, -0x90(%rbp)
movl 0x8841(%rip), %eax      # 0xec80
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x8839(%rip), %eax      # 0xec84
jg 0x652f
jmp 0x6453
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
andq $0xff, %rax
movb %al, 0x8814(%rip)       # 0xec8c
movq -0x90(%rbp), %rax
shrq $0x8, %rax
andq $0xff, %rax
movb %al, 0x87fe(%rip)       # 0xec8d
movq -0x90(%rbp), %rax
shrq $0x10, %rax
andq $0xff, %rax
movb %al, 0x87e8(%rip)       # 0xec8e
movq -0x90(%rbp), %rax
shrq $0x18, %rax
andq $0xff, %rax
movb %al, 0x87d2(%rip)       # 0xec8f
leaq 0x87c8(%rip), %rdi      # 0xec8c
movl $0x2c, %esi
callq 0x65d0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x9c(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x94(%rbp)
leaq -0x9c(%rbp), %rdi
leaq 0x87bb(%rip), %rsi      # 0xecb8
callq 0x56d0
cmpl $0x0, %eax
setl %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x6453
movl -0x14(%rbp), %edi
leaq -0x9c(%rbp), %rsi
callq 0x7130
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x6442
callq 0x70b0
callq 0x86b0
xorl %eax, %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)