// File: /workspace/github_c_makefile_star2_O1/PARKINHYO[P]BlackjackGame/mqueue/mqueue_server.c
// Function: play_game_one
void* play_game_one(void *data)
{

	struct msqid_ds msq_status;

	int id = (intptr_t)data;
	char buffer[BUFFER_SIZE];
	int nwritten;
	int player_sum, dealer_sum;
	int* player_hand_values = players_hand_values[id], *dealer_hand_values = dealers_hand_values[id];
	int* player_hand_suits = players_hand_suits[id], *dealer_hand_suits = dealers_hand_suits[id];
	int i;
	msg buf;

	msgctl(msgid_Recv_Main, IPC_STAT, &msq_status);
	buf.type = msq_status.msg_lspid;
	printf("%ld\n", buf.type);

	nplayers[id] = 0;
	ndealers[id] = 0;

	pthread_mutex_lock(&card_mutex);
	{
		for (i = 0; i < 2; ++i)
		{
			player_hand_values[nplayers[id]] = card_values[ncard];
			player_hand_suits[nplayers[id]] = card_suits[ncard];
			++ncard;
			++nplayers[id];
		}

		dealer_hand_values[ndealers[id]] = card_values[ncard];
		dealer_hand_suits[ndealers[id]] = card_suits[ncard];
		++ncard;
		++ndealers[id];
	}
	pthread_mutex_unlock(&card_mutex);

	buffer[0] = value_codes[player_hand_values[0]];
	buffer[1] = suit_codes[player_hand_suits[0]];
	buffer[2] = value_codes[player_hand_values[1]];
	buffer[3] = suit_codes[player_hand_suits[1]];
	buffer[4] = value_codes[dealer_hand_values[0]];
	buffer[5] = suit_codes[dealer_hand_suits[0]];
	buffer[6] = 0;

	strncpy(buf.data, buffer, BUFFER_SIZE);
	msgsnd(msgid_Send_Main, (void*)&buf, sizeof(msg), 0);
	printf("send: first cardset\n");

	//첫번째 카드를 뿌려주고 난뒤...
	printf("\n");
	printf("Player 1 Hand: ");
	display_state(player_hand_values, player_hand_suits, nplayers[id]);
	printf("Dealer Hand with player 1: ");
	display_state(dealer_hand_values, dealer_hand_suits, ndealers[id]);
	int k;

	while(1) {
		msgrcv(msgid_Recv_Main, (void*)&buf, sizeof(msg), buf.type, 0);
		printf("buf.data: %s\n", buf.data);

		strncpy(buffer, buf.data, BUFFER_SIZE);
		printf("I received from player 1: %s\n", buffer);
		strncpy(buf.data, "\0", BUFFER_SIZE);

		//hit 결과 전송...
		if (strcmp(buffer, HIT) == 0)
		{
			//뮤텍스로 상호배제...
			pthread_mutex_lock(&card_mutex);
			{
				player_hand_values[nplayers[id]] = card_values[ncard];
				player_hand_suits[nplayers[id]] = card_suits[ncard];

				buffer[0] = value_codes[player_hand_values[nplayers[id]]];
				buffer[1] = suit_codes[player_hand_suits[nplayers[id]]];
				buffer[2] = 0;

				++ncard;
				++nplayers[id];
			}
			pthread_mutex_unlock(&card_mutex);

			strncpy(buf.data, buffer, BUFFER_SIZE);
			msgsnd(msgid_Send_Main, (void*)&buf, sizeof(msg), 0);
			printf("buf.data: %s\n", buf.data);
			printf("I send to player 1: %s\n", buffer);

			buffer[0] = '\0';

			// 플레이어 결과가 21이 넘을 경우
			if (calc_sum(player_hand_values, nplayers[id]) > 21)
			{
				printf("Player 1 busted. Dealer wins.\n");
				return NULL;
			}
			// 플레이어 결과가 21일 경우 이기기 때문에 break
			else if (calc_sum(player_hand_values, nplayers[id]) == 21)
				break;
		}
		// 플레이어의 signal이 STAND인 경우 stop
		else if (strcmp(buffer, STAND) == 0) {
			break;
		}
	}
	i = 0;
	while (calc_sum(dealer_hand_values, ndealers[id]) < 17)
	{
		dealer_hand_values[ndealers[id]] = card_values[ncard];
		dealer_hand_suits[ndealers[id]] = card_suits[ncard];
		buffer[i++] = value_codes[dealer_hand_values[ndealers[id]]];
		buffer[i++] = suit_codes[dealer_hand_suits[ndealers[id]]];
		++ncard;
		++ndealers[id];
	}
	buffer[i] = 0;


	printf("\n");
	printf("Player 1 Hand: ");
	display_state(player_hand_values, player_hand_suits, nplayers[id]);
	printf("Dealer 1 Hand: ");
	display_state(dealer_hand_values, dealer_hand_suits, ndealers[id]);

	strncpy(buf.data, buffer, BUFFER_SIZE);
	msgsnd(msgid_Send_Main, (void*)&buf, sizeof(msg), 0);

	player_sum = calc_sum(player_hand_values, nplayers[id]);
	dealer_sum = calc_sum(dealer_hand_values, ndealers[id]);

	if (dealer_sum > 21)
		printf("\nDealer busted! Player %d wins!\n", id);
	else if (player_sum == dealer_sum)
		printf("\nPlayer %d and dealer have the same score. It's a push!\n", id);
	else if (player_sum < dealer_sum)
		printf("\nDealer has a higher score than player %d. Dealer wins!\n", id);
	else
		printf("\nPlayer %d has a higher score. Player wins!\n", id);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, -0x78(%rbp)
movslq %edi, %rbx
movq %rbx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %r15
leaq 0x2c7f(%rip), %rax      # 0x4160
leaq (%rax,%r15), %r13
movq %r13, -0x48(%rbp)
leaq 0x32b0(%rip), %rax      # 0x47a0
addq %r15, %rax
movq %rax, -0x38(%rbp)
movl 0x2c57(%rip), %edi      # 0x4154
leaq -0x140(%rbp), %r14
movl $0x2, %esi
movq %r14, %rdx
callq 0x10e0
movslq 0x60(%r14), %rsi
leaq -0xc0(%rbp), %r12
movq %rsi, -0x8(%r12)
leaq 0xb53(%rip), %rdi       # 0x207b
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x1070
leaq 0x38a7(%rip), %rax      # 0x4de0
movl %r14d, (%rax,%rbx,4)
leaq 0x38cc(%rip), %rax      # 0x4e10
movl %r14d, (%rax,%rbx,4)
leaq 0x38e9(%rip), %r14      # 0x4e38
movq %r14, %rdi
callq 0x1120
movslq 0x39d2(%rip), %rax      # 0x4f30
leaq 0x387b(%rip), %r9       # 0x4de0
movslq (%r9,%rbx,4), %rcx
leaq 0x38f0(%rip), %rsi      # 0x4e60
movq (%rsi,%rax,4), %rdx
movq %rdx, (%r13,%rcx,4)
leaq 0x39c0(%rip), %rdi      # 0x4f40
movq (%rdi,%rax,4), %rdx
movq -0x38(%rbp), %r8
movq %rdx, (%r8,%rcx,4)
leal 0x2(%rax), %edx
leaq 0x2eea(%rip), %r8       # 0x4480
leaq (%r8,%r15), %r10
movq %r10, -0x30(%rbp)
leaq 0x351b(%rip), %r11      # 0x4ac0
leal 0x2(%rcx), %ecx
movl %edx, 0x3982(%rip)      # 0x4f30
movl %ecx, (%r9,%rbx,4)
movslq %edx, %rcx
movl (%rsi,%rcx,4), %edx
leaq 0x3851(%rip), %r8       # 0x4e10
movslq (%r8,%rbx,4), %rsi
movl %edx, (%r10,%rsi,4)
leaq (%r11,%r15), %rdx
movq %rdx, -0x80(%rbp)
movl (%rdi,%rcx,4), %ecx
movl %ecx, (%rdx,%rsi,4)
addl $0x3, %eax
movl %eax, 0x3952(%rip)      # 0x4f30
leal 0x1(%rsi), %eax
movl %eax, (%r8,%rbx,4)
movq %r14, %rdi
callq 0x10d0
leaq 0x2b6c(%rip), %rdi      # 0x4160
movslq (%rdi,%r15), %rax
leaq 0xc5a(%rip), %rcx       # 0x2259
movb (%rax,%rcx), %al
leaq -0x70(%rbp), %r13
movb %al, (%r13)
leaq 0x318f(%rip), %rsi      # 0x47a0
movslq (%rsi,%r15), %rax
leaq 0xc39(%rip), %rdx       # 0x2255
movb (%rax,%rdx), %al
movb %al, 0x1(%r13)
movslq 0x4(%rdi,%r15), %rax
movb (%rax,%rcx), %al
movb %al, 0x2(%r13)
movslq 0x4(%rsi,%r15), %rax
movb (%rax,%rdx), %al
movb %al, 0x3(%r13)
leaq 0x2e3e(%rip), %rax      # 0x4480
movslq (%rax,%r15), %rax
leaq 0x3473(%rip), %r14      # 0x4ac0
movb (%rax,%rcx), %al
movb %al, 0x4(%r13)
movslq (%r14,%r15), %rax
movb (%rax,%rdx), %al
movb %al, 0x5(%r13)
movb $0x0, 0x6(%r13)
movl $0x20, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x1050
movl 0x2ad6(%rip), %edi      # 0x4150
leaq -0xc8(%rbp), %r15
movl $0x28, %edx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x1090
leaq 0xb38(%rip), %rdi       # 0x21cf
callq 0x1060
movl $0xa, %edi
callq 0x1040
leaq 0x9d3(%rip), %rdi       # 0x2080
xorl %eax, %eax
callq 0x1070
leaq 0x3725(%rip), %rax      # 0x4de0
movl (%rax,%rbx,4), %edx
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1300
leaq 0x9be(%rip), %rdi       # 0x2090
xorl %eax, %eax
callq 0x1070
movq %rbx, -0x40(%rbp)
leaq 0x372c(%rip), %rax      # 0x4e10
movl (%rax,%rbx,4), %edx
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq -0x80(%rbp), %rsi
callq 0x1300
movl 0x2a57(%rip), %edi      # 0x4154
movq -0xc8(%rbp), %rcx
movl $0x28, %edx
movq %r15, %rsi
xorl %r8d, %r8d
callq 0x1110
leaq 0x991(%rip), %rdi       # 0x20ac
movq %r12, %rsi
xorl %eax, %eax
callq 0x1070
movl $0x20, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x1050
leaq 0x97e(%rip), %rdi       # 0x20ba
movq %r13, %rsi
xorl %eax, %eax
callq 0x1070
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movups %xmm0, 0x10(%r12)
cmpl $0x544948, -0x70(%rbp)  # imm = 0x544948
jne 0x1875
leaq 0x36d0(%rip), %rdi      # 0x4e38
callq 0x1120
movslq 0x37bc(%rip), %rax      # 0x4f30
leaq 0x36e5(%rip), %rcx      # 0x4e60
movslq (%rcx,%rax,4), %rcx
movq -0x40(%rbp), %r14
movq %r12, %rbx
movq %r15, %r12
leaq 0x3650(%rip), %r15      # 0x4de0
movslq (%r15,%r14,4), %rdx
movq -0x48(%rbp), %r13
movl %ecx, (%r13,%rdx,4)
leaq 0x379c(%rip), %rsi      # 0x4f40
movslq (%rsi,%rax,4), %rsi
movq -0x38(%rbp), %rdi
movl %esi, (%rdi,%rdx,4)
leaq 0xaa3(%rip), %rdi       # 0x2259
movb (%rcx,%rdi), %cl
movb %cl, -0x70(%rbp)
leaq 0xa92(%rip), %rcx       # 0x2255
movb (%rsi,%rcx), %cl
movb %cl, -0x6f(%rbp)
xorl %ecx, %ecx
movb %cl, -0x6e(%rbp)
incl %eax
movl %eax, 0x375a(%rip)      # 0x4f30
leal 0x1(%rdx), %eax
movl %eax, (%r15,%r14,4)
leaq 0x3654(%rip), %rdi      # 0x4e38
callq 0x10d0
movl $0x20, %edx
movq %rbx, %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movl 0x2950(%rip), %edi      # 0x4150
movl $0x28, %edx
movq %r12, %r15
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x1090
leaq 0x893(%rip), %rdi       # 0x20ac
movq %rbx, %r12
movq %rbx, %rsi
movq %r13, %rbx
leaq -0x70(%rbp), %r13
xorl %eax, %eax
callq 0x1070
leaq 0x8a4(%rip), %rdi       # 0x20d8
movq %r13, %rsi
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
movb %al, -0x70(%rbp)
leaq 0x3596(%rip), %rax      # 0x4de0
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
callq 0x12ab
cmpl $0x16, %eax
jge 0x19ae
cmpl $0x15, %eax
movq -0x30(%rbp), %rbx
leaq 0x3253(%rip), %r14      # 0x4ac0
jne 0x16f7
jmp 0x188e
movl -0x70(%rbp), %eax
movl $0x4e415453, %ecx       # imm = 0x4E415453
xorl %ecx, %eax
movzwl -0x6c(%rbp), %ecx
xorl $0x44, %ecx
orl %eax, %ecx
jne 0x16f7
movq -0x40(%rbp), %r15
leaq 0x3577(%rip), %r13      # 0x4e10
movl (%r13,%r15,4), %esi
movq %rbx, %rdi
movq %rsi, %rbx
callq 0x12ab
cmpl $0x10, %eax
jg 0x19bf
movslq %ebx, %rax
movslq 0x3674(%rip), %r13      # 0x4f30
leal 0x1(%r13), %ecx
movq %rcx, -0x98(%rbp)
shlq $0x2, %r13
movq %r13, -0x90(%rbp)
leaq (%r15,%r15,4), %rcx
shlq $0x4, %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x88(%rbp)
incl %ebx
movq %rbx, -0xa0(%rbp)
xorl %r12d, %r12d
movq %r14, %r13
xorl %r14d, %r14d
movq -0x30(%rbp), %rbx
leaq 0x953(%rip), %r15       # 0x2255
movq -0x90(%rbp), %rax
leaq (%rax,%r12,4), %rax
leaq 0x354c(%rip), %rcx      # 0x4e60
movslq (%rcx,%rax), %rcx
movq -0x88(%rbp), %rdx
leaq (%rdx,%r12,4), %rdx
leaq 0x2b56(%rip), %rsi      # 0x4480
movl %ecx, (%rsi,%rdx)
leaq 0x360c(%rip), %rsi      # 0x4f40
movslq (%rsi,%rax), %rax
movl %eax, (%r13,%rdx)
leaq 0x915(%rip), %rdx       # 0x2259
movb (%rcx,%rdx), %cl
movb %cl, -0x70(%rbp,%r12,2)
movb (%rax,%r15), %al
addq $0x2, %r14
movb %al, -0x6f(%rbp,%r12,2)
movq -0x98(%rbp), %rax
addl %r12d, %eax
movl %eax, 0x35c7(%rip)      # 0x4f30
movq -0xa0(%rbp), %rax
leal (%rax,%r12), %esi
leaq 0x3495(%rip), %rax      # 0x4e10
movq -0x40(%rbp), %rcx
movl %esi, (%rax,%rcx,4)
movq %rbx, %rdi
callq 0x12ab
incq %r12
cmpl $0x11, %eax
jl 0x1902
andl $-0x2, %r14d
leaq 0x346f(%rip), %r13      # 0x4e10
leaq -0xc0(%rbp), %r12
movq -0x40(%rbp), %r15
jmp 0x19c2
leaq 0x82e(%rip), %rdi       # 0x21e3
callq 0x1060
jmp 0x1aa6
xorl %r14d, %r14d
movb $0x0, -0x70(%rbp,%r14)
movl $0xa, %edi
callq 0x1040
leaq 0x6a7(%rip), %rdi       # 0x2080
xorl %eax, %eax
callq 0x1070
leaq 0x33f9(%rip), %rbx      # 0x4de0
movl (%rbx,%r15,4), %edx
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x1300
leaq 0x6f4(%rip), %rdi       # 0x20f6
xorl %eax, %eax
callq 0x1070
movl (%r13,%r15,4), %edx
movq -0x30(%rbp), %r14
movq %r14, %rdi
movq -0x80(%rbp), %rsi
callq 0x1300
leaq -0x70(%rbp), %rsi
movl $0x20, %edx
movq %r12, %rdi
callq 0x1050
movl 0x271b(%rip), %edi      # 0x4150
leaq -0xc8(%rbp), %rsi
movl $0x28, %edx
xorl %ecx, %ecx
callq 0x1090
leaq 0x3391(%rip), %rax      # 0x4de0
movl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x12ab
movl %eax, %ebx
movl (%r13,%r15,4), %esi
movq %r14, %rdi
callq 0x12ab
cmpl $0x16, %eax
jl 0x1a7c
leaq 0x690(%rip), %rdi       # 0x2106
movq -0x78(%rbp), %rsi
jmp 0x1a9f
cmpl %eax, %ebx
movq -0x78(%rbp), %rsi
jne 0x1a8d
leaq 0x69c(%rip), %rdi       # 0x2127
jmp 0x1a9f
jge 0x1a98
leaq 0x6ca(%rip), %rdi       # 0x2160
jmp 0x1a9f
leaq 0x6fa(%rip), %rdi       # 0x2199
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq