// File: /workspace/github_c_makefile_star2_O2/PARKINHYO[P]BlackjackGame/mqueue/mqueue_client.c
// Function: recv_msg
void* recv_msg() {
	msg buf;
	buf.type = pid;

	char buffer[BUFFER_SIZE];

	check =1;
	int my_hand_values[20], dealer_hand_values[20];
	int my_hand_suits[20], dealer_hand_suits[20];
	int nmy = 0, ndealer = 0;
	buf.type = pid;

	while (1) {
		if(check == 1){

			printf("pid: %d\n\n", pid);
			msgrcv(msgid_Recv_Main, (void*)&buf, sizeof(msg), pid, 0);
			strncpy(buffer, buf.data, BUFFER_SIZE);
			printf("%s\n", buffer);

			my_hand_values[0] = get_value_id(buffer[0]);
			my_hand_suits[0] = get_suit_id(buffer[1]);
			my_hand_values[1] = get_value_id(buffer[2]);
			my_hand_suits[1] = get_suit_id(buffer[3]);
			dealer_hand_values[0] = get_value_id(buffer[4]);
			dealer_hand_suits[0] = get_suit_id(buffer[5]);
			nmy = 2;
			ndealer = 1;

			my_sum = calc_sum(my_hand_values, nmy);

			printf("\n");
			// 카드덱과 합산값 화면출력
			printf("My Hand: ");
			display_state(my_hand_values, my_hand_suits, nmy);
			printf("Dealer Hand: ");
			display_state(dealer_hand_values, dealer_hand_suits, ndealer);

			if (my_sum > 21)
			{
				printf("\nI'm busted! I lose!\n");
				return 0;
			}

			check = 0;
			check2 = 1;
			buffer[0]='\0';
		}
		if(check2 == 2){
			printf("buffer: %s\n", buffer);
			msgrcv(msgid_Recv_Main, (void*)&buf, sizeof(msg), pid, 0);
			printf("buf.data: %s\n", buf.data);
			strncpy(buffer, buf.data, BUFFER_SIZE);
			printf("I received: %s\n", buffer);
			my_hand_values[nmy] = get_value_id(buffer[0]);
			my_hand_suits[nmy] = get_suit_id(buffer[1]);
			++nmy;

			printf("\n");
			printf("My Hand: ");
			display_state(my_hand_values, my_hand_suits, nmy);
			printf("Dealer Hand: ");
			display_state(dealer_hand_values, dealer_hand_suits, ndealer);
			my_sum = calc_sum(my_hand_values, nmy);
			if(my_sum>21){
				printf("\nI'm busted! I lose!\n");
				return 0;
			}

			check2 = 1;
		}

		if(finalcheck == 1){
			finalcheck = 0;
			unsigned i;
			msgrcv(msgid_Recv_Main, (void*)&buf, sizeof(msg), pid, 0);
			strncpy(buffer, buf.data, BUFFER_SIZE);

			printf("I received: %s\n", buffer);

			for (i = 0; i < strlen(buffer); i += 2)
			{
				dealer_hand_values[ndealer] = get_value_id(buffer[i]);
				dealer_hand_suits[ndealer] = get_suit_id(buffer[i + 1]);
				++ndealer;
			}

			printf("\n");
			printf("My Hand: ");
			display_state(my_hand_values, my_hand_suits, nmy);
			printf("Dealer Hand: ");
			display_state(dealer_hand_values, dealer_hand_suits, ndealer);

			my_sum = calc_sum(my_hand_values, nmy);
			dealer_sum = calc_sum(dealer_hand_values, ndealer);

			if (dealer_sum > 21)
				printf("\nDealer busted! I win!\n");
			else if (my_sum == dealer_sum)
				printf("\nMe and the dealer have the same score. It's a push!\n");
			else if (my_sum < dealer_sum)
				printf("\nDealer has a higher score. I lose!\n");
			else
				printf("\nI have a higher score. I win!\n");

			return 0;

		}
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movslq 0x2be3(%rip), %rax      # 0x4174
movl $0x1, 0x2bc5(%rip)      # 0x4160
leaq -0x70(%rbp), %rcx
movq %rax, -0x8(%rcx)
pushq $0x1
popq %r13
leaq 0xae5(%rip), %r14       # 0x2093
leaq -0x1c0(%rbp), %r15
pushq $0x2
popq %r12
movl %r13d, %eax
xorl %ebx, %ebx
movl $0x0, -0x2c(%rbp)
cmpl $0x1, %eax
jne 0x16fc
movl 0x2ba0(%rip), %esi      # 0x4174
movq %r14, %rdi
xorl %eax, %eax
callq 0x1090
movl 0x2b8c(%rip), %edi      # 0x4170
movslq 0x2b89(%rip), %rcx      # 0x4174
leaq -0x78(%rbp), %rsi
pushq $0x28
popq %rdx
xorl %r8d, %r8d
callq 0x1140
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
leaq -0x70(%rbp), %rsi
pushq $0x20
popq %rdx
callq 0x1050
movq %rbx, %rdi
callq 0x1060
movsbl -0x50(%rbp), %edi
callq 0x129a
movl %eax, -0x1c0(%rbp)
movsbl -0x4f(%rbp), %edi
callq 0x1273
movl %eax, -0xd0(%rbp)
movsbl -0x4e(%rbp), %edi
callq 0x129a
movl %eax, -0x1bc(%rbp)
movsbl -0x4d(%rbp), %edi
callq 0x1273
movl %eax, -0xcc(%rbp)
movsbl -0x4c(%rbp), %edi
callq 0x129a
movl %eax, -0x170(%rbp)
movsbl -0x4b(%rbp), %edi
callq 0x1273
movl %eax, -0x120(%rbp)
movq %r15, %rdi
movl %r12d, %esi
callq 0x12c1
movl %eax, 0x2af8(%rip)      # 0x4178
pushq $0xa
popq %rdi
callq 0x1040
leaq 0xa0e(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
movq %r15, %rdi
leaq -0xd0(%rbp), %rsi
movl %r12d, %edx
callq 0x130c
leaq 0x9f8(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl %r13d, %edx
callq 0x130c
cmpl $0x15, 0x2aa5(%rip)     # 0x4178
jg 0x1944
andl $0x0, 0x2a80(%rip)      # 0x4160
movl $0x1, 0x2a7a(%rip)      # 0x4164
movb $0x0, -0x50(%rbp)
xorl %eax, %eax
movl %r13d, -0x2c(%rbp)
movl %r12d, %ebx
jmp 0x181e
cmpl $0x2, 0x2a61(%rip)      # 0x4164
jne 0x181e
movl %r13d, %r14d
leaq 0x9a2(%rip), %rdi       # 0x20b5
movq %r15, %r12
leaq -0x50(%rbp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movl 0x2a46(%rip), %edi      # 0x4170
movslq 0x2a43(%rip), %rcx      # 0x4174
leaq -0x78(%rbp), %rsi
pushq $0x28
popq %rdx
xorl %r8d, %r8d
callq 0x1140
leaq 0x97a(%rip), %rdi       # 0x20c1
leaq -0x70(%rbp), %r13
movq %r13, %rsi
xorl %eax, %eax
callq 0x1090
movq %r15, %rdi
movq %r13, %rsi
pushq $0x20
popq %rdx
callq 0x1050
leaq 0x965(%rip), %rdi       # 0x20cf
movq %r15, %rsi
movq %r12, %r15
xorl %eax, %eax
callq 0x1090
movsbl -0x50(%rbp), %edi
callq 0x129a
movslq %ebx, %rbx
movl %eax, -0x1c0(%rbp,%rbx,4)
movsbl -0x4f(%rbp), %edi
callq 0x1273
movl %eax, -0xd0(%rbp,%rbx,4)
incl %ebx
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x8f2(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
movq %r12, %rdi
leaq -0xd0(%rbp), %rsi
movl %ebx, %edx
callq 0x130c
leaq 0x8dd(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x130c
movq %r12, %rdi
movl %ebx, %esi
callq 0x12c1
movl %eax, 0x2981(%rip)      # 0x4178
cmpl $0x15, %eax
jg 0x1944
movl 0x295a(%rip), %eax      # 0x4160
movl $0x1, 0x2954(%rip)      # 0x4164
movl %r14d, %r13d
leaq 0x879(%rip), %r14       # 0x2093
pushq $0x2
popq %r12
cmpl $0x1, 0x2943(%rip)      # 0x4168
jne 0x15c5
andl $0x0, 0x2936(%rip)      # 0x4168
movl 0x2938(%rip), %edi      # 0x4170
movslq 0x2935(%rip), %rcx      # 0x4174
leaq -0x78(%rbp), %rsi
pushq $0x28
popq %rdx
xorl %r8d, %r8d
callq 0x1140
leaq -0x50(%rbp), %r14
pushq $0x20
popq %rdx
movq %r14, %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
leaq 0x867(%rip), %rdi       # 0x20cf
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
callq 0x1080
movq %rax, %r12
movl -0x2c(%rbp), %r14d
pushq $0x1
popq %r15
leal -0x1(%r15), %eax
cmpq %rax, %r12
jbe 0x18be
movsbl -0x50(%rbp,%rax), %edi
callq 0x129a
movl %eax, -0x170(%rbp,%r14,4)
movl %r15d, %eax
movsbl -0x50(%rbp,%rax), %edi
callq 0x1273
movl %eax, -0x120(%rbp,%r14,4)
incq %r14
addl $0x2, %r15d
jmp 0x1885
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x7d0(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
leaq -0x1c0(%rbp), %r15
leaq -0xd0(%rbp), %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0x130c
leaq 0x7b4(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %r12
leaq -0x120(%rbp), %rsi
movq %r12, %rdi
movl %r14d, %edx
callq 0x130c
movq %r15, %rdi
movl %ebx, %esi
callq 0x12c1
movl %eax, %ebx
movl %eax, 0x2853(%rip)      # 0x4178
movq %r12, %rdi
movl %r14d, %esi
callq 0x12c1
movl %eax, 0x2846(%rip)      # 0x417c
cmpl $0x15, %eax
jle 0x194d
leaq 0x8ac(%rip), %rdi       # 0x21ee
jmp 0x196c
leaq 0x8ba(%rip), %rdi       # 0x2205
jmp 0x196c
cmpl %eax, %ebx
jne 0x195a
leaq 0x861(%rip), %rdi       # 0x21b9
jmp 0x196c
leaq 0x834(%rip), %rax       # 0x2195
leaq 0x80e(%rip), %rdi       # 0x2176
cmovlq %rax, %rdi
callq 0x1060
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq