// File: /workspace/github_c_makefile_star2_O0/eric-lo[P]3150/7-Threading/pthread_game.c
// Function: game_play
void *game_play(void *nothing)
{
	int i, input, sum, life = LIFE_INIT, point = 0, level = 0;
	int dice[DICE_COUNT];

	srand(time(NULL));
	while(1)
	{
		for(i = 0; i < DICE_COUNT; i++)
			dice[i] = rand() % 6 + 1;

		display(dice, life, point, level);
		input = wait_for_input(level);

		if(input == 0)	// 0 - means no input until timeout.
		{
			life--;
			printf("\nTIMEOUT\n");
		}
		else	// has an input.
		{
			if(input != 'B' && input != 'b' &&
			   input != 's' && input != 'S' &&
			   input != 'n' && input != 'N')
			{
				life--;
				printf("WRONG\n");
			}
			else
			{
				if(input == 'B')
					input = 'b';
				if(input == 'S')
					input = 's';
				if(input == 'N')
					input = 'n';

				for(i = 0, sum = 0; i < DICE_COUNT; i++)
					sum += dice[i];

				if((IS_BIG(sum) && input != 'b') ||
				   (IS_SMALL(sum) && input != 's') ||
				   (all_the_same(dice) && input != 'n') )
				{
					life--;
					printf("WRONG\n");
				}
				else
				{
					point++;
					printf("CORRECT\n");
				}
			}
		}

		if(life == 0)
		{
			printf("End of game. Bye\n");
			exit(0);
		}

		if(point > 0 && point % 10 == 0)
			level++;

		usleep(500000);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl $0x3, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1080
movl %eax, %edi
callq 0x1050
movl $0x0, -0xc(%rbp)
cmpl $0x3, -0xc(%rbp)
jge 0x14e1
callq 0x1110
movl $0x6, %ecx
cltd
idivl %ecx
addl $0x1, %edx
movslq -0xc(%rbp), %rax
movl %edx, -0x2c(%rbp,%rax,4)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x14b8
leaq -0x2c(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
callq 0x1310
movl -0x20(%rbp), %edi
callq 0x13c0
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x1520
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
leaq 0xb56(%rip), %rdi       # 0x206a
movb $0x0, %al
callq 0x1040
jmp 0x1614
cmpl $0x42, -0x10(%rbp)
je 0x1560
cmpl $0x62, -0x10(%rbp)
je 0x1560
cmpl $0x73, -0x10(%rbp)
je 0x1560
cmpl $0x53, -0x10(%rbp)
je 0x1560
cmpl $0x6e, -0x10(%rbp)
je 0x1560
cmpl $0x4e, -0x10(%rbp)
je 0x1560
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
leaq 0xb20(%rip), %rdi       # 0x2074
movb $0x0, %al
callq 0x1040
jmp 0x1612
cmpl $0x42, -0x10(%rbp)
jne 0x156d
movl $0x62, -0x10(%rbp)
cmpl $0x53, -0x10(%rbp)
jne 0x157a
movl $0x73, -0x10(%rbp)
cmpl $0x4e, -0x10(%rbp)
jne 0x1587
movl $0x6e, -0x10(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x3, -0xc(%rbp)
jge 0x15b4
movslq -0xc(%rbp), %rax
movl -0x2c(%rbp,%rax,4), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x1595
cmpl $0xb, -0x14(%rbp)
jl 0x15c0
cmpl $0x62, -0x10(%rbp)
jne 0x15e0
cmpl $0xb, -0x14(%rbp)
jge 0x15cc
cmpl $0x73, -0x10(%rbp)
jne 0x15e0
leaq -0x2c(%rbp), %rdi
callq 0x12b0
cmpl $0x0, %eax
je 0x15f9
cmpl $0x6e, -0x10(%rbp)
je 0x15f9
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
leaq 0xa84(%rip), %rdi       # 0x2074
movb $0x0, %al
callq 0x1040
jmp 0x1610
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
leaq 0xa72(%rip), %rdi       # 0x207b
movb $0x0, %al
callq 0x1040
jmp 0x1612
jmp 0x1614
cmpl $0x0, -0x18(%rbp)
jne 0x162f
leaq 0xa63(%rip), %rdi       # 0x2084
movb $0x0, %al
callq 0x1040
xorl %edi, %edi
callq 0x10d0
cmpl $0x0, -0x1c(%rbp)
jle 0x164e
movl -0x1c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x164e
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x1120
jmp 0x14b1
nopl (%rax)