// File: /workspace/github_c_makefile_star2_O3/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 0x2bba(%rip), %rax      # 0x4174
movl $0x1, 0x2b9c(%rip)      # 0x4160
leaq -0x78(%rbp), %rcx
movq %rax, -0x8(%rcx)
movl $0x1, %eax
leaq 0xabb(%rip), %r14       # 0x2093
leaq 0xc49(%rip), %rbx       # 0x2228
leaq 0xc3e(%rip), %r15       # 0x2224
movl $0xa, %r13d
xorl %ecx, %ecx
xorl %r12d, %r12d
cmpl $0x1, %eax
jne 0x17cd
movl 0x2b74(%rip), %esi      # 0x4174
movq %r14, %rdi
xorl %eax, %eax
callq 0x1090
movl 0x2b60(%rip), %edi      # 0x4170
movslq 0x2b5d(%rip), %rcx      # 0x4174
movl $0x28, %edx
leaq -0x80(%rbp), %rsi
xorl %r8d, %r8d
callq 0x1140
movl $0x20, %edx
leaq -0x50(%rbp), %r12
movq %r12, %rdi
leaq -0x78(%rbp), %rsi
callq 0x1050
movq %r12, %rdi
callq 0x1060
movb -0x50(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x165d
incq %rax
cmpq $0xe, %rax
jne 0x164a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xd0(%rbp)
movb -0x4f(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x167c
incq %rax
cmpq $0x4, %rax
jne 0x1668
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x120(%rbp)
movb -0x4e(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x169a
incq %rax
cmpq $0xe, %rax
jne 0x1687
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xcc(%rbp)
movb -0x4d(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x16b9
incq %rax
cmpq $0x4, %rax
jne 0x16a5
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x11c(%rbp)
movb -0x4c(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x16d7
incq %rax
cmpq $0xe, %rax
jne 0x16c4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x1c0(%rbp)
movb -0x4b(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x16f6
incq %rax
cmpq $0x4, %rax
jne 0x16e2
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 0x1700
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 0x171b
movl %eax, 0x2a38(%rip)      # 0x4178
movl $0xa, %edi
callq 0x1040
leaq 0x94c(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
leaq -0xd0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl $0x2, %edx
callq 0x1310
leaq 0x930(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x1c0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
movl $0x1, %edx
callq 0x1310
cmpl $0x15, 0x29db(%rip)     # 0x4178
jg 0x1a7c
movl $0x0, 0x29b3(%rip)      # 0x4160
movl $0x1, 0x29ad(%rip)      # 0x4164
movb $0x0, -0x50(%rbp)
movl $0x2, %ecx
movl $0x1, %r12d
xorl %eax, %eax
jmp 0x192b
cmpl $0x2, 0x2990(%rip)      # 0x4164
jne 0x192b
movq %r12, -0x58(%rbp)
leaq 0x8d0(%rip), %rdi       # 0x20b5
leaq -0x50(%rbp), %r13
movq %r13, %rsi
xorl %eax, %eax
movq %rcx, %r12
callq 0x1090
movl 0x2974(%rip), %edi      # 0x4170
movslq 0x2971(%rip), %rcx      # 0x4174
movl $0x28, %edx
leaq -0x80(%rbp), %rsi
xorl %r8d, %r8d
callq 0x1140
leaq 0x8a6(%rip), %rdi       # 0x20c1
leaq -0x78(%rbp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
movl $0x20, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x1050
leaq 0x88f(%rip), %rdi       # 0x20cf
movq %r13, %rsi
xorl %eax, %eax
callq 0x1090
movq %r12, %r13
movb -0x50(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x1865
incq %rax
cmpq $0xe, %rax
jne 0x1852
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movslq %r13d, %r13
movl %eax, -0xd0(%rbp,%r13,4)
movb -0x4f(%rbp), %cl
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x1889
incq %rax
cmpq $0x4, %rax
jne 0x1875
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x120(%rbp,%r13,4)
incl %r13d
movl $0xa, %edi
callq 0x1040
leaq 0x7f8(%rip), %rdi       # 0x209d
xorl %eax, %eax
callq 0x1090
leaq -0xd0(%rbp), %r14
movq %r14, %rdi
leaq -0x120(%rbp), %rsi
movl %r13d, %edx
callq 0x1310
leaq 0x7db(%rip), %rdi       # 0x20a7
xorl %eax, %eax
callq 0x1090
leaq -0x1c0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x1310
movq %r14, %rdi
movq %r13, %r12
movl %r13d, %esi
callq 0x12bb
movl %eax, 0x287a(%rip)      # 0x4178
cmpl $0x15, %eax
jg 0x1a7c
movl 0x2853(%rip), %eax      # 0x4160
movl $0x1, 0x284d(%rip)      # 0x4164
leaq 0x775(%rip), %r14       # 0x2093
movq %r12, %rcx
movl $0xa, %r13d
movq -0x58(%rbp), %r12
cmpl $0x1, 0x2836(%rip)      # 0x4168
jne 0x15f1
movq %rcx, %r14
movl $0x0, 0x2823(%rip)      # 0x4168
movl 0x2825(%rip), %edi      # 0x4170
movslq 0x2822(%rip), %rcx      # 0x4174
leaq -0x80(%rbp), %rsi
movl $0x28, %edx
xorl %r8d, %r8d
callq 0x1140
leaq -0x50(%rbp), %r13
movl $0x20, %edx
movq %r13, %rdi
leaq -0x78(%rbp), %rsi
callq 0x1050
leaq 0x750(%rip), %rdi       # 0x20cf
movq %r13, %rsi
xorl %eax, %eax
callq 0x1090
movq %r13, %rdi
callq 0x1080
testq %rax, %rax
je 0x19f2
movl %r12d, %r12d
xorl %ecx, %ecx
movb -0x50(%rbp,%rcx), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%rbx)
je 0x19b6
incq %rdx
cmpq $0xe, %rdx
jne 0x19a2
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, -0x1c0(%rbp,%r12,4)
movb -0x4f(%rbp,%rcx), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%r15)
je 0x19d9
incq %rdx
cmpq $0x4, %rdx
jne 0x19c5
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 0x199b
movl $0xa, %edi
callq 0x1040
leaq 0x69a(%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 0x67d(%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, 0x271b(%rip)      # 0x4178
movq %r15, %rdi
movl %r12d, %esi
callq 0x12bb
movl %eax, 0x270e(%rip)      # 0x417c
cmpl $0x15, %eax
jle 0x1a85
leaq 0x774(%rip), %rdi       # 0x21ee
jmp 0x1aa4
leaq 0x782(%rip), %rdi       # 0x2205
jmp 0x1aa4
cmpl %eax, %ebx
jne 0x1a92
leaq 0x729(%rip), %rdi       # 0x21b9
jmp 0x1aa4
leaq 0x6fc(%rip), %rax       # 0x2195
leaq 0x6d6(%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