// File: /workspace/github_c_makefile_star2_O3/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
addq %r15, %rax
movq %rax, -0x48(%rbp)
leaq 0x32b1(%rip), %rax      # 0x47a0
addq %r15, %rax
movq %rax, -0x40(%rbp)
movl 0x2c58(%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 0xb54(%rip), %rdi       # 0x207b
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x1070
leaq 0x38a8(%rip), %r13      # 0x4de0
movl %r14d, (%r13,%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
movslq (%r13,%rbx,4), %rcx
leaq (%r15,%rcx,4), %rdx
leaq 0x38f2(%rip), %rdi      # 0x4e60
movq (%rdi,%rax,4), %rsi
leaq 0x2be7(%rip), %r8       # 0x4160
movq %rsi, (%r8,%rdx)
leaq 0x39bc(%rip), %r8       # 0x4f40
movq (%r8,%rax,4), %rsi
leaq 0x3211(%rip), %r9       # 0x47a0
movq %rsi, (%r9,%rdx)
leal 0x2(%rax), %edx
leaq 0x2ee3(%rip), %rsi      # 0x4480
leaq (%rsi,%r15), %r9
movq %r9, -0x30(%rbp)
leaq 0x3514(%rip), %r10      # 0x4ac0
leal 0x2(%rcx), %ecx
movl %ecx, (%r13,%rbx,4)
movslq %edx, %rcx
movl (%rdi,%rcx,4), %edx
leaq 0x384f(%rip), %rdi      # 0x4e10
movslq (%rdi,%rbx,4), %rsi
movl %edx, (%r9,%rsi,4)
leaq (%r10,%r15), %rdx
movq %rdx, -0x80(%rbp)
movl (%r8,%rcx,4), %ecx
movl %ecx, (%rdx,%rsi,4)
addl $0x3, %eax
movl %eax, 0x394f(%rip)      # 0x4f30
leal 0x1(%rsi), %eax
movl %eax, (%rdi,%rbx,4)
movq %r14, %rdi
callq 0x10d0
leaq 0x2b6a(%rip), %rdi      # 0x4160
movslq (%rdi,%r15), %rax
leaq 0xc58(%rip), %rcx       # 0x2259
movb (%rax,%rcx), %al
leaq -0x70(%rbp), %r13
movb %al, (%r13)
leaq 0x318d(%rip), %rsi      # 0x47a0
movslq (%rsi,%r15), %rax
leaq 0xc37(%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 0x2e3c(%rip), %rax      # 0x4480
movslq (%rax,%r15), %rax
leaq 0x3471(%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 0x2ad4(%rip), %edi      # 0x4150
leaq -0xc8(%rbp), %r15
movl $0x28, %edx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x1090
leaq 0xb36(%rip), %rdi       # 0x21cf
callq 0x1060
movl $0xa, %edi
callq 0x1040
leaq 0x9d1(%rip), %rdi       # 0x2080
xorl %eax, %eax
callq 0x1070
leaq 0x3723(%rip), %rax      # 0x4de0
movl (%rax,%rbx,4), %edx
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x1300
leaq 0x9bc(%rip), %rdi       # 0x2090
xorl %eax, %eax
callq 0x1070
movq %rbx, -0x38(%rbp)
leaq 0x372a(%rip), %rax      # 0x4e10
movl (%rax,%rbx,4), %edx
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq -0x80(%rbp), %rsi
callq 0x1300
movl 0x2a55(%rip), %edi      # 0x4154
movq -0xc8(%rbp), %rcx
movl $0x28, %edx
movq %r15, %rsi
xorl %r8d, %r8d
callq 0x1110
leaq 0x98f(%rip), %rdi       # 0x20ac
movq %r12, %rsi
xorl %eax, %eax
callq 0x1070
movl $0x20, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x1050
leaq 0x97c(%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 0x1877
leaq 0x36ce(%rip), %rdi      # 0x4e38
callq 0x1120
movslq 0x37ba(%rip), %rax      # 0x4f30
leaq 0x36e3(%rip), %rcx      # 0x4e60
movslq (%rcx,%rax,4), %rcx
movq -0x38(%rbp), %r14
movq %r12, %rbx
movq %r15, %r12
leaq 0x364e(%rip), %r15      # 0x4de0
movslq (%r15,%r14,4), %rdx
movq -0x48(%rbp), %r13
movl %ecx, (%r13,%rdx,4)
leaq 0x379a(%rip), %rsi      # 0x4f40
movslq (%rsi,%rax,4), %rsi
movq -0x40(%rbp), %rdi
movl %esi, (%rdi,%rdx,4)
leaq 0xaa1(%rip), %rdi       # 0x2259
movb (%rcx,%rdi), %cl
movb %cl, -0x70(%rbp)
leaq 0xa90(%rip), %rcx       # 0x2255
movb (%rsi,%rcx), %cl
movb %cl, -0x6f(%rbp)
xorl %ecx, %ecx
movb %cl, -0x6e(%rbp)
incl %eax
movl %eax, 0x3758(%rip)      # 0x4f30
leal 0x1(%rdx), %eax
movl %eax, (%r15,%r14,4)
leaq 0x3652(%rip), %rdi      # 0x4e38
callq 0x10d0
movl $0x20, %edx
movq %rbx, %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movl 0x294e(%rip), %edi      # 0x4150
movl $0x28, %edx
movq %r12, %r15
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x1090
leaq 0x891(%rip), %rdi       # 0x20ac
movq %rbx, %r12
movq %rbx, %rsi
movq %r13, %rbx
leaq -0x70(%rbp), %r13
xorl %eax, %eax
callq 0x1070
leaq 0x8a2(%rip), %rdi       # 0x20d8
movq %r13, %rsi
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
movb %al, -0x70(%rbp)
leaq 0x3594(%rip), %rax      # 0x4de0
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
callq 0x12ab
cmpl $0x16, %eax
jge 0x19b0
cmpl $0x15, %eax
movq -0x30(%rbp), %rbx
leaq 0x3251(%rip), %r14      # 0x4ac0
jne 0x16f9
jmp 0x1890
movl -0x70(%rbp), %eax
movl $0x4e415453, %ecx       # imm = 0x4E415453
xorl %ecx, %eax
movzwl -0x6c(%rbp), %ecx
xorl $0x44, %ecx
orl %eax, %ecx
jne 0x16f9
movq -0x38(%rbp), %r15
leaq 0x3575(%rip), %r13      # 0x4e10
movl (%r13,%r15,4), %esi
movq %rbx, %rdi
movq %rsi, %rbx
callq 0x12ab
cmpl $0x10, %eax
jg 0x19c1
movslq %ebx, %rax
movslq 0x3672(%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 0x951(%rip), %r15       # 0x2255
movq -0x90(%rbp), %rax
leaq (%rax,%r12,4), %rax
leaq 0x354a(%rip), %rcx      # 0x4e60
movslq (%rcx,%rax), %rcx
movq -0x88(%rbp), %rdx
leaq (%rdx,%r12,4), %rdx
leaq 0x2b54(%rip), %rsi      # 0x4480
movl %ecx, (%rsi,%rdx)
leaq 0x360a(%rip), %rsi      # 0x4f40
movslq (%rsi,%rax), %rax
movl %eax, (%r13,%rdx)
leaq 0x913(%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, 0x35c5(%rip)      # 0x4f30
movq -0xa0(%rbp), %rax
leal (%rax,%r12), %esi
leaq 0x3493(%rip), %rax      # 0x4e10
movq -0x38(%rbp), %rcx
movl %esi, (%rax,%rcx,4)
movq %rbx, %rdi
callq 0x12ab
incq %r12
cmpl $0x11, %eax
jl 0x1904
andl $-0x2, %r14d
leaq 0x346d(%rip), %r13      # 0x4e10
leaq -0xc0(%rbp), %r12
movq -0x38(%rbp), %r15
jmp 0x19c4
leaq 0x82c(%rip), %rdi       # 0x21e3
callq 0x1060
jmp 0x1aa8
xorl %r14d, %r14d
movb $0x0, -0x70(%rbp,%r14)
movl $0xa, %edi
callq 0x1040
leaq 0x6a5(%rip), %rdi       # 0x2080
xorl %eax, %eax
callq 0x1070
leaq 0x33f7(%rip), %rbx      # 0x4de0
movl (%rbx,%r15,4), %edx
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
callq 0x1300
leaq 0x6f2(%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 0x2719(%rip), %edi      # 0x4150
leaq -0xc8(%rbp), %rsi
movl $0x28, %edx
xorl %ecx, %ecx
callq 0x1090
leaq 0x338f(%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 0x1a7e
leaq 0x68e(%rip), %rdi       # 0x2106
movq -0x78(%rbp), %rsi
jmp 0x1aa1
cmpl %eax, %ebx
movq -0x78(%rbp), %rsi
jne 0x1a8f
leaq 0x69a(%rip), %rdi       # 0x2127
jmp 0x1aa1
jge 0x1a9a
leaq 0x6c8(%rip), %rdi       # 0x2160
jmp 0x1aa1
leaq 0x6f8(%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