// File: /workspace/github_c_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %rdi, -0x78(%rbp)
movslq %edi, %r12
imulq $0x50, %r12, %r14
leaq 0x2c89(%rip), %r15      # 0x4170
leaq (%r15,%r14), %rax
movq %rax, -0x40(%rbp)
leaq 0x32ba(%rip), %r13      # 0x47b0
leaq (%r13,%r14), %rax
movq %rax, -0x48(%rbp)
movl 0x2c5f(%rip), %edi      # 0x4164
pushq $0x2
popq %rsi
leaq -0x128(%rbp), %rbx
movq %rbx, %rdx
callq 0x10f0
movslq 0x60(%rbx), %rsi
movq %rsi, -0xb0(%rbp)
leaq 0xb52(%rip), %rdi       # 0x207b
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1070
leaq 0x38b7(%rip), %rax      # 0x4df0
andl $0x0, (%rax,%r12,4)
leaq 0x38db(%rip), %rax      # 0x4e20
andl $0x0, (%rax,%r12,4)
leaq 0x38f7(%rip), %rdi      # 0x4e48
callq 0x1130
movslq 0x39e3(%rip), %rax      # 0x4f40
movq %r12, -0x30(%rbp)
leaq 0x3888(%rip), %rcx      # 0x4df0
movslq (%rcx,%r12,4), %rdi
movl %edi, %edx
movq %rax, %rcx
shlq $0x20, %rcx
movq %rax, %rsi
shlq $0x2, %rsi
movq %r14, -0x38(%rbp)
leaq (%r14,%rdi,4), %rdi
movabsq $0x100000000, %r8       # imm = 0x100000000
cmpl $0x2, %ebx
je 0x15c2
leaq (%rsi,%rbx,4), %r9
leaq 0x38d2(%rip), %r10      # 0x4e70
movl (%r10,%r9), %r10d
leaq (%rdi,%rbx,4), %r11
movl %r10d, (%r15,%r11)
leaq 0x399f(%rip), %r10      # 0x4f50
movl (%r10,%r9), %r9d
movl %r9d, (%r13,%r11)
addq %r8, %rcx
incq %rbx
jmp 0x158e
leaq 0x2ec7(%rip), %rsi      # 0x4490
movq -0x38(%rbp), %r12
leaq (%rsi,%r12), %rdi
movq %rdi, -0x50(%rbp)
leaq 0x34f4(%rip), %r10      # 0x4ad0
addl %ebx, %edx
movq -0x30(%rbp), %r8
leaq 0x3807(%rip), %r14      # 0x4df0
movl %edx, (%r14,%r8,4)
sarq $0x1e, %rcx
leaq 0x3878(%rip), %rdx      # 0x4e70
movl (%rcx,%rdx), %edx
leaq 0x381e(%rip), %r13      # 0x4e20
movslq (%r13,%r8,4), %rsi
movq %r13, %r9
movq %r8, %r13
movl %edx, (%rdi,%rsi,4)
leaq (%r10,%r12), %rdi
movq %rdi, -0x80(%rbp)
leaq 0x3931(%rip), %rdx      # 0x4f50
movl (%rcx,%rdx), %ecx
movl %ecx, (%rdi,%rsi,4)
leal 0x1(%rax,%rbx), %eax
movl %eax, 0x3911(%rip)      # 0x4f40
leal 0x1(%rsi), %eax
movl %eax, (%r9,%r8,4)
leaq 0x380b(%rip), %rdi      # 0x4e48
callq 0x10d0
movq -0x40(%rbp), %rbx
movslq (%rbx), %rax
leaq 0xc09(%rip), %rcx       # 0x2259
movb (%rax,%rcx), %al
leaq -0x70(%rbp), %r15
movb %al, (%r15)
movq -0x48(%rbp), %r14
movslq (%r14), %rax
leaq 0xbed(%rip), %rdx       # 0x2255
movb (%rax,%rdx), %al
movb %al, 0x1(%r15)
movslq 0x4(%rbx), %rax
movb (%rax,%rcx), %al
movb %al, 0x2(%r15)
movslq 0x4(%r14), %rax
movb (%rax,%rdx), %al
movb %al, 0x3(%r15)
leaq 0x2e04(%rip), %rax      # 0x4490
movslq (%r12,%rax), %rax
movb (%rax,%rcx), %al
movb %al, 0x4(%r15)
leaq 0x3432(%rip), %rax      # 0x4ad0
movslq (%r12,%rax), %rax
movb (%rax,%rdx), %al
movb %al, 0x5(%r15)
movb $0x0, 0x6(%r15)
leaq -0xa8(%rbp), %r12
pushq $0x20
popq %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x1050
movl 0x2a97(%rip), %edi      # 0x4160
leaq -0xb0(%rbp), %rsi
pushq $0x28
popq %rdx
xorl %ecx, %ecx
callq 0x1090
leaq 0xaee(%rip), %rdi       # 0x21cf
callq 0x1060
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x98b(%rip), %rdi       # 0x2080
xorl %eax, %eax
callq 0x1070
leaq 0x36ed(%rip), %rax      # 0x4df0
movl (%rax,%r13,4), %edx
movq %rbx, %rdi
pushq $0x20
popq %rbx
movq %r14, %rsi
pushq $0x28
popq %r14
callq 0x130c
leaq 0x970(%rip), %rdi       # 0x2090
xorl %eax, %eax
callq 0x1070
leaq 0x36f2(%rip), %rax      # 0x4e20
movl (%rax,%r13,4), %edx
movq -0x50(%rbp), %rdi
movq -0x80(%rbp), %rsi
leaq -0xb0(%rbp), %r13
callq 0x130c
movl 0x2a18(%rip), %edi      # 0x4164
movq -0xb0(%rbp), %rcx
movq %r13, %rsi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x1120
leaq 0x944(%rip), %rdi       # 0x20ac
movq %r12, %rsi
xorl %eax, %eax
callq 0x1070
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1050
leaq 0x933(%rip), %rdi       # 0x20ba
movq %r15, %rsi
xorl %eax, %eax
callq 0x1070
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movups %xmm0, 0x10(%r12)
cmpl $0x544948, -0x70(%rbp)  # imm = 0x544948
jne 0x18b1
leaq 0x3695(%rip), %rdi      # 0x4e48
callq 0x1130
movslq 0x3781(%rip), %rax      # 0x4f40
leaq 0x36aa(%rip), %rcx      # 0x4e70
movslq (%rcx,%rax,4), %rcx
movq -0x30(%rbp), %rsi
leaq 0x361b(%rip), %r14      # 0x4df0
movslq (%r14,%rsi,4), %rdx
movq %rsi, %r8
movq -0x40(%rbp), %rsi
movl %ecx, (%rsi,%rdx,4)
leaq 0x3766(%rip), %rsi      # 0x4f50
movslq (%rsi,%rax,4), %rsi
movq -0x48(%rbp), %rdi
movl %esi, (%rdi,%rdx,4)
leaq 0xa5d(%rip), %rdi       # 0x2259
movb (%rcx,%rdi), %cl
movb %cl, -0x70(%rbp)
leaq 0xa4c(%rip), %rcx       # 0x2255
movb (%rsi,%rcx), %cl
movb %cl, -0x6f(%rbp)
xorl %ecx, %ecx
movb %cl, -0x6e(%rbp)
incl %eax
movl %eax, 0x3724(%rip)      # 0x4f40
leal 0x1(%rdx), %eax
movl %eax, (%r14,%r8,4)
leaq 0x361e(%rip), %rdi      # 0x4e48
callq 0x10d0
movq %r12, %rdi
leaq -0x70(%rbp), %rsi
movq %rbx, %rdx
leaq -0xb0(%rbp), %r13
callq 0x1050
movl 0x2915(%rip), %edi      # 0x4160
movq %r13, %rsi
pushq $0x28
popq %rdx
movq %rdx, %r14
xorl %ecx, %ecx
callq 0x1090
leaq 0x84a(%rip), %rdi       # 0x20ac
movq %r12, %rsi
leaq -0x70(%rbp), %r15
xorl %eax, %eax
callq 0x1070
leaq 0x861(%rip), %rdi       # 0x20d8
movq %r15, %rsi
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
movb %al, -0x70(%rbp)
movq -0x30(%rbp), %rax
leaq 0x355f(%rip), %rcx      # 0x4df0
movl (%rcx,%rax,4), %esi
movq -0x40(%rbp), %rdi
callq 0x12c1
cmpl $0x16, %eax
jge 0x1a58
cmpl $0x15, %eax
jne 0x1746
jmp 0x18cb
movq %r15, %rdi
leaq 0x835(%rip), %rsi       # 0x20f0
pushq $0x6
popq %rdx
callq 0x10e0
testl %eax, %eax
jne 0x1746
movq -0x30(%rbp), %rax
leaq 0x354a(%rip), %rcx      # 0x4e20
movl (%rcx,%rax,4), %eax
movslq 0x3660(%rip), %rbx      # 0x4f40
movq %rax, -0x88(%rbp)
movslq %eax, %r13
movq -0x38(%rbp), %rax
leaq (%rax,%r13,4), %rax
movq %rax, -0x38(%rbp)
leal 0x1(%rbx), %r14d
shlq $0x2, %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
movq -0x88(%rbp), %rax
leal (%rax,%r15), %esi
movq -0x50(%rbp), %rdi
callq 0x12c1
cmpl $0x10, %eax
jg 0x199a
leaq (%rbx,%r15,4), %rax
leaq 0x3548(%rip), %rcx      # 0x4e70
movslq (%rcx,%rax), %rcx
movq -0x38(%rbp), %rdx
leaq (%rdx,%r15,4), %rdx
leaq 0x2b55(%rip), %rsi      # 0x4490
movl %ecx, (%rsi,%rdx)
leaq 0x360b(%rip), %rsi      # 0x4f50
movslq (%rsi,%rax), %rax
leaq 0x3180(%rip), %rsi      # 0x4ad0
movl %eax, (%rsi,%rdx)
leal (%r14,%r15), %edx
movl %edx, 0x35e3(%rip)      # 0x4f40
leal 0x1(%r13,%r15), %edx
movq -0x30(%rbp), %rsi
leaq 0x34b3(%rip), %rdi      # 0x4e20
movl %edx, (%rdi,%rsi,4)
leaq 0x8e2(%rip), %rdx       # 0x2259
movb (%rcx,%rdx), %cl
movb %cl, -0x70(%rbp,%r15,2)
leaq 0x8cf(%rip), %rcx       # 0x2255
movb (%rax,%rcx), %al
movb %al, -0x6f(%rbp,%r15,2)
addq $0x2, %r12
incq %r15
jmp 0x1904
andl $-0x2, %r12d
movb $0x0, -0x70(%rbp,%r12)
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x6cd(%rip), %rdi       # 0x2080
xorl %eax, %eax
callq 0x1070
movq -0x30(%rbp), %r14
leaq 0x342b(%rip), %r12      # 0x4df0
movl (%r12,%r14,4), %edx
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x130c
leaq 0x716(%rip), %rdi       # 0x20f6
xorl %eax, %eax
callq 0x1070
leaq 0x3432(%rip), %r15      # 0x4e20
movl (%r15,%r14,4), %edx
movq -0x50(%rbp), %r13
movq %r13, %rdi
movq -0x80(%rbp), %rsi
callq 0x130c
leaq -0x70(%rbp), %rsi
pushq $0x20
popq %rdx
leaq -0xa8(%rbp), %rdi
callq 0x1050
movl 0x2745(%rip), %edi      # 0x4160
leaq -0xb0(%rbp), %rsi
pushq $0x28
popq %rdx
xorl %ecx, %ecx
callq 0x1090
movl (%r12,%r14,4), %esi
movq %rbx, %rdi
callq 0x12c1
movl %eax, %ebx
movl (%r15,%r14,4), %esi
movq %r13, %rdi
callq 0x12c1
cmpl $0x16, %eax
jl 0x1a66
leaq 0x6b4(%rip), %rdi       # 0x2106
movq -0x78(%rbp), %rsi
jmp 0x1a89
leaq 0x784(%rip), %rdi       # 0x21e3
callq 0x1060
jmp 0x1a90
cmpl %eax, %ebx
movq -0x78(%rbp), %rsi
jne 0x1a77
leaq 0x6b2(%rip), %rdi       # 0x2127
jmp 0x1a89
jge 0x1a82
leaq 0x6e0(%rip), %rdi       # 0x2160
jmp 0x1a89
leaq 0x710(%rip), %rdi       # 0x2199
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq