// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl %eax, -0x8c(%rbp)
movslq -0x8c(%rbp), %rcx
leaq 0x3b39(%rip), %rax      # 0x5160
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movslq -0x8c(%rbp), %rcx
leaq 0x3e3d(%rip), %rax      # 0x5480
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movslq -0x8c(%rbp), %rcx
leaq 0x4141(%rip), %rax      # 0x57a0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movslq -0x8c(%rbp), %rcx
leaq 0x4445(%rip), %rax      # 0x5ac0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movl 0x3ac5(%rip), %edi      # 0x5154
movl $0x2, %esi
leaq -0x88(%rbp), %rdx
callq 0x10e0
movslq -0x28(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rsi
leaq 0x19cc(%rip), %rdi      # 0x3085
movb $0x0, %al
callq 0x1060
movslq -0x8c(%rbp), %rcx
leaq 0x4712(%rip), %rax      # 0x5de0
movl $0x0, (%rax,%rcx,4)
movslq -0x8c(%rbp), %rcx
leaq 0x472d(%rip), %rax      # 0x5e10
movl $0x0, (%rax,%rcx,4)
leaq 0x4747(%rip), %rdi      # 0x5e38
callq 0x1120
movl $0x0, -0xe4(%rbp)
cmpl $0x2, -0xe4(%rbp)
jge 0x17a8
movslq 0x481c(%rip), %rcx      # 0x5f30
leaq 0x4745(%rip), %rax      # 0x5e60
movl (%rax,%rcx,4), %edx
movq -0xc8(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x46ad(%rip), %rcx      # 0x5de0
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movslq 0x47ef(%rip), %rcx      # 0x5f30
leaq 0x47f8(%rip), %rax      # 0x5f40
movl (%rax,%rcx,4), %edx
movq -0xd8(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x4680(%rip), %rcx      # 0x5de0
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x47c3(%rip), %eax      # 0x5f30
addl $0x1, %eax
movl %eax, 0x47ba(%rip)      # 0x5f30
movslq -0x8c(%rbp), %rcx
leaq 0x465c(%rip), %rax      # 0x5de0
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x464f(%rip), %rax      # 0x5de0
movl %edx, (%rax,%rcx,4)
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x1700
movslq 0x4781(%rip), %rcx      # 0x5f30
leaq 0x46aa(%rip), %rax      # 0x5e60
movl (%rax,%rcx,4), %edx
movq -0xd0(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x4642(%rip), %rcx      # 0x5e10
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movslq 0x4754(%rip), %rcx      # 0x5f30
leaq 0x475d(%rip), %rax      # 0x5f40
movl (%rax,%rcx,4), %edx
movq -0xe0(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x4615(%rip), %rcx      # 0x5e10
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x4728(%rip), %eax      # 0x5f30
addl $0x1, %eax
movl %eax, 0x471f(%rip)      # 0x5f30
movslq -0x8c(%rbp), %rcx
leaq 0x45f1(%rip), %rax      # 0x5e10
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x45e4(%rip), %rax      # 0x5e10
movl %edx, (%rax,%rcx,4)
leaq 0x4602(%rip), %rdi      # 0x5e38
callq 0x10d0
movq -0xc8(%rbp), %rax
movslq (%rax), %rcx
leaq 0x1a16(%rip), %rax      # 0x3262
movb (%rax,%rcx), %al
movb %al, -0xb0(%rbp)
movq -0xd8(%rbp), %rax
movslq (%rax), %rcx
leaq 0x19f8(%rip), %rax      # 0x325e
movb (%rax,%rcx), %al
movb %al, -0xaf(%rbp)
movq -0xc8(%rbp), %rax
movslq 0x4(%rax), %rcx
leaq 0x19e1(%rip), %rax      # 0x3262
movb (%rax,%rcx), %al
movb %al, -0xae(%rbp)
movq -0xd8(%rbp), %rax
movslq 0x4(%rax), %rcx
leaq 0x19c2(%rip), %rax      # 0x325e
movb (%rax,%rcx), %al
movb %al, -0xad(%rbp)
movq -0xd0(%rbp), %rax
movslq (%rax), %rcx
leaq 0x19ac(%rip), %rax      # 0x3262
movb (%rax,%rcx), %al
movb %al, -0xac(%rbp)
movq -0xe0(%rbp), %rax
movslq (%rax), %rcx
leaq 0x198e(%rip), %rax      # 0x325e
movb (%rax,%rcx), %al
movb %al, -0xab(%rbp)
movb $0x0, -0xaa(%rbp)
leaq -0x110(%rbp), %rdi
addq $0x8, %rdi
leaq -0xb0(%rbp), %rsi
movl $0x20, %edx
callq 0x1040
movl 0x384e(%rip), %edi      # 0x5150
leaq -0x110(%rbp), %rsi
movl $0x28, %edx
xorl %ecx, %ecx
callq 0x1090
leaq 0x176e(%rip), %rdi      # 0x308a
movb $0x0, %al
callq 0x1060
leaq 0x1876(%rip), %rdi      # 0x31a0
movb $0x0, %al
callq 0x1060
leaq 0x1767(%rip), %rdi      # 0x309f
movb $0x0, %al
callq 0x1060
movq -0xc8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movslq -0x8c(%rbp), %rcx
leaq 0x4485(%rip), %rax      # 0x5de0
movl (%rax,%rcx,4), %edx
callq 0x13e0
leaq 0x1745(%rip), %rdi      # 0x30af
movb $0x0, %al
callq 0x1060
movq -0xd0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movslq -0x8c(%rbp), %rcx
leaq 0x4483(%rip), %rax      # 0x5e10
movl (%rax,%rcx,4), %edx
callq 0x13e0
movl 0x37b9(%rip), %edi      # 0x5154
movq -0x110(%rbp), %rcx
leaq -0x110(%rbp), %rsi
movl $0x28, %edx
xorl %r8d, %r8d
callq 0x1110
leaq -0x110(%rbp), %rsi
addq $0x8, %rsi
leaq 0x1703(%rip), %rdi      # 0x30cb
movb $0x0, %al
callq 0x1060
leaq -0xb0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
addq $0x8, %rsi
movl $0x20, %edx
callq 0x1040
leaq -0xb0(%rbp), %rsi
leaq 0x16e0(%rip), %rdi      # 0x30d9
movb $0x0, %al
callq 0x1060
leaq -0x110(%rbp), %rdi
addq $0x8, %rdi
leaq 0x185e(%rip), %rsi      # 0x3270
movl $0x20, %edx
callq 0x1040
leaq -0xb0(%rbp), %rdi
leaq 0x16cd(%rip), %rsi      # 0x30f7
callq 0x1080
cmpl $0x0, %eax
jne 0x1c07
leaq 0x43f9(%rip), %rdi      # 0x5e38
callq 0x1120
movslq 0x44e5(%rip), %rcx      # 0x5f30
leaq 0x440e(%rip), %rax      # 0x5e60
movl (%rax,%rcx,4), %edx
movq -0xc8(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x4376(%rip), %rcx      # 0x5de0
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movslq 0x44b8(%rip), %rcx      # 0x5f30
leaq 0x44c1(%rip), %rax      # 0x5f40
movl (%rax,%rcx,4), %edx
movq -0xd8(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x4349(%rip), %rcx      # 0x5de0
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movq -0xc8(%rbp), %rax
movslq -0x8c(%rbp), %rdx
leaq 0x432d(%rip), %rcx      # 0x5de0
movslq (%rcx,%rdx,4), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x17a0(%rip), %rax      # 0x3262
movb (%rax,%rcx), %al
movb %al, -0xb0(%rbp)
movq -0xd8(%rbp), %rax
movslq -0x8c(%rbp), %rdx
leaq 0x4300(%rip), %rcx      # 0x5de0
movslq (%rcx,%rdx,4), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x176f(%rip), %rax      # 0x325e
movb (%rax,%rcx), %al
movb %al, -0xaf(%rbp)
movb $0x0, -0xae(%rbp)
movl 0x442b(%rip), %eax      # 0x5f30
addl $0x1, %eax
movl %eax, 0x4422(%rip)      # 0x5f30
movslq -0x8c(%rbp), %rcx
leaq 0x42c4(%rip), %rax      # 0x5de0
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x42b7(%rip), %rax      # 0x5de0
movl %edx, (%rax,%rcx,4)
leaq 0x4305(%rip), %rdi      # 0x5e38
callq 0x10d0
leaq -0x110(%rbp), %rdi
addq $0x8, %rdi
leaq -0xb0(%rbp), %rsi
movl $0x20, %edx
callq 0x1040
movl 0x35f6(%rip), %edi      # 0x5150
leaq -0x110(%rbp), %rsi
movl $0x28, %edx
xorl %ecx, %ecx
callq 0x1090
leaq -0x110(%rbp), %rsi
addq $0x8, %rsi
leaq 0x154c(%rip), %rdi      # 0x30cb
movb $0x0, %al
callq 0x1060
leaq -0xb0(%rbp), %rsi
leaq 0x1567(%rip), %rdi      # 0x30fb
movb $0x0, %al
callq 0x1060
movb $0x0, -0xb0(%rbp)
movq -0xc8(%rbp), %rdi
movslq -0x8c(%rbp), %rcx
leaq 0x4229(%rip), %rax      # 0x5de0
movl (%rax,%rcx,4), %esi
callq 0x1340
cmpl $0x15, %eax
jle 0x1bdf
leaq 0x1548(%rip), %rdi      # 0x3113
movb $0x0, %al
callq 0x1060
movq $0x0, -0x8(%rbp)
jmp 0x1ee9
movq -0xc8(%rbp), %rdi
movslq -0x8c(%rbp), %rcx
leaq 0x41ec(%rip), %rax      # 0x5de0
movl (%rax,%rcx,4), %esi
callq 0x1340
cmpl $0x15, %eax
jne 0x1c03
jmp 0x1c28
jmp 0x1c05
jmp 0x1c23
leaq -0xb0(%rbp), %rdi
leaq 0x151d(%rip), %rsi      # 0x3132
callq 0x1080
cmpl $0x0, %eax
jne 0x1c21
jmp 0x1c28
jmp 0x1c23
jmp 0x1995
movl $0x0, -0xe4(%rbp)
movq -0xd0(%rbp), %rdi
movslq -0x8c(%rbp), %rcx
leaq 0x41c9(%rip), %rax      # 0x5e10
movl (%rax,%rcx,4), %esi
callq 0x1340
cmpl $0x11, %eax
jge 0x1d66
movslq 0x42d1(%rip), %rcx      # 0x5f30
leaq 0x41fa(%rip), %rax      # 0x5e60
movl (%rax,%rcx,4), %edx
movq -0xd0(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x4192(%rip), %rcx      # 0x5e10
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movslq 0x42a4(%rip), %rcx      # 0x5f30
leaq 0x42ad(%rip), %rax      # 0x5f40
movl (%rax,%rcx,4), %edx
movq -0xe0(%rbp), %rax
movslq -0x8c(%rbp), %rsi
leaq 0x4165(%rip), %rcx      # 0x5e10
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movq -0xd0(%rbp), %rax
movslq -0x8c(%rbp), %rdx
leaq 0x4149(%rip), %rcx      # 0x5e10
movslq (%rcx,%rdx,4), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x158c(%rip), %rax      # 0x3262
movb (%rax,%rcx), %cl
movl -0xe4(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xe4(%rbp)
cltq
movb %cl, -0xb0(%rbp,%rax)
movq -0xe0(%rbp), %rax
movslq -0x8c(%rbp), %rdx
leaq 0x4108(%rip), %rcx      # 0x5e10
movslq (%rcx,%rdx,4), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x1547(%rip), %rax      # 0x325e
movb (%rax,%rcx), %cl
movl -0xe4(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xe4(%rbp)
cltq
movb %cl, -0xb0(%rbp,%rax)
movl 0x41f6(%rip), %eax      # 0x5f30
addl $0x1, %eax
movl %eax, 0x41ed(%rip)      # 0x5f30
movslq -0x8c(%rbp), %rcx
leaq 0x40bf(%rip), %rax      # 0x5e10
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x40b2(%rip), %rax      # 0x5e10
movl %edx, (%rax,%rcx,4)
jmp 0x1c32
movslq -0xe4(%rbp), %rax
movb $0x0, -0xb0(%rbp,%rax)
leaq 0x1424(%rip), %rdi      # 0x31a0
movb $0x0, %al
callq 0x1060
leaq 0x1315(%rip), %rdi      # 0x309f
movb $0x0, %al
callq 0x1060
movq -0xc8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movslq -0x8c(%rbp), %rcx
leaq 0x4033(%rip), %rax      # 0x5de0
movl (%rax,%rcx,4), %edx
callq 0x13e0
leaq 0x137c(%rip), %rdi      # 0x3138
movb $0x0, %al
callq 0x1060
movq -0xd0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movslq -0x8c(%rbp), %rcx
leaq 0x4031(%rip), %rax      # 0x5e10
movl (%rax,%rcx,4), %edx
callq 0x13e0
leaq -0x110(%rbp), %rdi
addq $0x8, %rdi
leaq -0xb0(%rbp), %rsi
movl $0x20, %edx
callq 0x1040
movl 0x3347(%rip), %edi      # 0x5150
leaq -0x110(%rbp), %rsi
movl $0x28, %edx
xorl %ecx, %ecx
callq 0x1090
movq -0xc8(%rbp), %rdi
movslq -0x8c(%rbp), %rcx
leaq 0x3faf(%rip), %rax      # 0x5de0
movl (%rax,%rcx,4), %esi
callq 0x1340
movl %eax, -0xb8(%rbp)
movq -0xd0(%rbp), %rdi
movslq -0x8c(%rbp), %rcx
leaq 0x3fbc(%rip), %rax      # 0x5e10
movl (%rax,%rcx,4), %esi
callq 0x1340
movl %eax, -0xbc(%rbp)
cmpl $0x15, -0xbc(%rbp)
jle 0x1e81
movl -0x8c(%rbp), %esi
leaq 0x12d0(%rip), %rdi      # 0x3148
movb $0x0, %al
callq 0x1060
jmp 0x1ee1
movl -0xb8(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jne 0x1ea5
movl -0x8c(%rbp), %esi
leaq 0x12cd(%rip), %rdi      # 0x3169
movb $0x0, %al
callq 0x1060
jmp 0x1edf
movl -0xb8(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jge 0x1ec9
movl -0x8c(%rbp), %esi
leaq 0x12e2(%rip), %rdi      # 0x31a2
movb $0x0, %al
callq 0x1060
jmp 0x1edd
movl -0x8c(%rbp), %esi
leaq 0x1305(%rip), %rdi      # 0x31db
movb $0x0, %al
callq 0x1060
jmp 0x1edf
jmp 0x1ee1
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)