// File: /workspace/github_c_makefile_star2_O1/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 0x2bc4(%rip), %rax      # 0x4174
leaq -0x70(%rbp), %rcx
movq %rax, -0x8(%rcx)
movl $0x1, 0x2b9e(%rip)      # 0x4160
leaq -0x78(%rbp), %r12
leaq 0xc5b(%rip), %r15       # 0x2228
leaq 0xc50(%rip), %rbx       # 0x2224
movl $0xa, %r13d
xorl %r14d, %r14d
movq $0x0, -0x30(%rbp)
cmpl $0x1, 0x2b74(%rip)      # 0x4160
jne 0x17c5
movl 0x2b7c(%rip), %esi      # 0x4174
leaq 0xa94(%rip), %rdi       # 0x2093
xorl %eax, %eax
callq 0x1090
movl 0x2b64(%rip), %edi      # 0x4170
movslq 0x2b61(%rip), %rcx      # 0x4174
movl $0x28, %edx
movq %r12, %rsi
xorl %r8d, %r8d
callq 0x1140
movl $0x20, %edx
leaq -0x50(%rbp), %r14
movq %r14, %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq %r14, %rdi
callq 0x1060
movb -0x50(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x1659
incq %rax
cmpq $0xe, %rax
jne 0x1645
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xd0(%rbp)
movb -0x4f(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x1677
incq %rax
cmpq $0x4, %rax
jne 0x1664
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x120(%rbp)
movb -0x4e(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x1696
incq %rax
cmpq $0xe, %rax
jne 0x1682
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xcc(%rbp)
movb -0x4d(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x16b4
incq %rax
cmpq $0x4, %rax
jne 0x16a1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x11c(%rbp)
movb -0x4c(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x16d3
incq %rax
cmpq $0xe, %rax
jne 0x16bf
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x1c0(%rbp)
movb -0x4b(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x16f1
incq %rax
cmpq $0x4, %rax
jne 0x16de
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x170(%rbp)
xorl %ecx, %ecx
xorl %eax, %eax
movl -0xd0(%rbp,%rcx,4), %edx
cmpl $0xa, %edx
cmovgel %r13d, %edx
addl %edx, %eax
incq %rcx
cmpq $0x2, %rcx
jne 0x16fb
xorl %ecx, %ecx
movl %eax, %edx
leal 0xa(%rdx), %eax
cmpl $0xc, %edx
cmovgel %edx, %eax
cmpl $0x1, -0xd0(%rbp,%rcx,4)
cmovnel %edx, %eax
incq %rcx
cmpq $0x2, %rcx
jne 0x1716
movl %eax, 0x2a3d(%rip)      # 0x4178
movl $0xa, %edi
callq 0x1040
leaq 0x951(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
leaq -0xd0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl $0x2, %edx
callq 0x1310
leaq 0x935(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x1c0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
movl $0x1, %edx
callq 0x1310
cmpl $0x15, 0x29e0(%rip)     # 0x4178
jg 0x19cf
movl $0x0, 0x29b8(%rip)      # 0x4160
movl $0x1, 0x29b2(%rip)      # 0x4164
movb $0x0, -0x50(%rbp)
movl $0x1, %eax
movq %rax, -0x30(%rbp)
movl $0x2, %r14d
cmpl $0x2, 0x2998(%rip)      # 0x4164
jne 0x1905
leaq 0x8dc(%rip), %rdi       # 0x20b5
leaq -0x50(%rbp), %r13
movq %r13, %rsi
xorl %eax, %eax
callq 0x1090
movl 0x2983(%rip), %edi      # 0x4170
movslq 0x2980(%rip), %rcx      # 0x4174
movl $0x28, %edx
movq %r12, %rsi
xorl %r8d, %r8d
callq 0x1140
leaq 0x8b6(%rip), %rdi       # 0x20c1
leaq -0x70(%rbp), %r12
movq %r12, %rsi
xorl %eax, %eax
callq 0x1090
movl $0x20, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x1050
leaq 0x89f(%rip), %rdi       # 0x20cf
movq %r13, %rsi
xorl %eax, %eax
callq 0x1090
movb -0x50(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x1853
incq %rax
cmpq $0xe, %rax
jne 0x183f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movslq %r14d, %r14
movl %eax, -0xd0(%rbp,%r14,4)
movb -0x4f(%rbp), %cl
xorl %eax, %eax
movl $0xa, %r13d
cmpb %cl, (%rax,%rbx)
je 0x187c
incq %rax
cmpq $0x4, %rax
jne 0x1869
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x120(%rbp,%r14,4)
incl %r14d
movl $0xa, %edi
callq 0x1040
leaq 0x805(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
leaq -0xd0(%rbp), %r12
movq %r12, %rdi
leaq -0x120(%rbp), %rsi
movl %r14d, %edx
callq 0x1310
leaq 0x7e8(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x1c0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x1310
movq %r12, %rdi
movl %r14d, %esi
callq 0x12bb
movl %eax, 0x288a(%rip)      # 0x4178
cmpl $0x15, %eax
jg 0x19cf
movl $0x1, 0x2863(%rip)      # 0x4164
leaq -0x78(%rbp), %r12
cmpl $0x1, 0x285c(%rip)      # 0x4168
jne 0x15e5
movl $0x0, 0x284c(%rip)      # 0x4168
movl 0x284e(%rip), %edi      # 0x4170
movslq 0x284b(%rip), %rcx      # 0x4174
leaq -0x78(%rbp), %rsi
movl $0x28, %edx
xorl %r8d, %r8d
callq 0x1140
leaq -0x70(%rbp), %rsi
leaq -0x50(%rbp), %r13
movl $0x20, %edx
movq %r13, %rdi
callq 0x1050
leaq 0x779(%rip), %rdi       # 0x20cf
movq %r13, %rsi
xorl %eax, %eax
callq 0x1090
movq %r13, %rdi
callq 0x1080
testq %rax, %rax
je 0x19db
movq -0x30(%rbp), %r12
movl %r12d, %r12d
xorl %ecx, %ecx
movb -0x50(%rbp,%rcx), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%r15)
je 0x1991
incq %rdx
cmpq $0xe, %rdx
jne 0x197d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, -0x1c0(%rbp,%r12,4)
movb -0x4f(%rbp,%rcx), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%rbx)
je 0x19b4
incq %rdx
cmpq $0x4, %rdx
jne 0x19a0
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, -0x170(%rbp,%r12,4)
incq %r12
addq $0x2, %rcx
movl %ecx, %edx
andl $-0x2, %edx
cmpq %rdx, %rax
ja 0x1976
jmp 0x19df
leaq 0x82f(%rip), %rdi       # 0x2205
jmp 0x1a88
movq -0x30(%rbp), %r12
movl $0xa, %edi
callq 0x1040
leaq 0x6ad(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
leaq -0xd0(%rbp), %rbx
leaq -0x120(%rbp), %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x1310
leaq 0x690(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x1c0(%rbp), %r15
leaq -0x170(%rbp), %rsi
movq %r15, %rdi
movl %r12d, %edx
callq 0x1310
movq %rbx, %rdi
movl %r14d, %esi
callq 0x12bb
movl %eax, %ebx
movl %eax, 0x272e(%rip)      # 0x4178
movq %r15, %rdi
movl %r12d, %esi
callq 0x12bb
movl %eax, 0x2721(%rip)      # 0x417c
cmpl $0x15, %eax
jle 0x1a69
leaq 0x787(%rip), %rdi       # 0x21ee
jmp 0x1a88
cmpl %eax, %ebx
jne 0x1a76
leaq 0x745(%rip), %rdi       # 0x21b9
jmp 0x1a88
leaq 0x718(%rip), %rax       # 0x2195
leaq 0x6f2(%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