// File: /workspace/github_c_makefile_star2_O1/PARKINHYO[P]BlackjackGame/fifo/fifo_client.c
// Function: recv_msg
void* recv_msg()
{
	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;
	/* server side - send first cardset */
	char filename[BUFF_SIZE];
	int fdd;

	//서버에서 생성된 pid 이름 경로 파이프 open
	sprintf(filename, "%s%d", FROM_SERVER_FILE, getpid());
	if ((fdd = open(filename, O_RDONLY)) == -1) {
		printf("[SYSTEM] in recv_msg() open error!!\n");
	}
	while (1) {
		//check, check2, finalcheck 값에 따라 if문 들어감
		if (check == 1) {
			//카드 받아옴
			read(fdd, buffer, BUFF_SIZE);

			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;

			int choice;
			
			//합계 계산
			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);
			/* 내 덱에 합계가 21이 넘어갈 경우 lose */
			if (my_sum > 21)
			{
				printf("\nI'm busted! I lose!\n");
				break;
			}

			check = 0;

			//send_msg, (HIT / STAND)
			check2 = 1;
		}
		int n = 4;
		for (n; n > 0; n--) {
			printf("\n");
			sleep(1);
		}

		//HIT 일때
		if (check2 == 2) {
			//새로운 카드 받아옴
			read(fdd, buffer, BUFF_SIZE);
			printf("I received: %c%c\n", buffer[0], buffer[1]);

			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");
				break;
			}

			//한번더
			check2 = 1;
		}
		n = 4;
		for (n; n > 0; n--) {
			printf("\n");
			sleep(1);
		}

		if (finalcheck == 1) {
			finalcheck = 0;
			unsigned i;

			//딜러 카드 받아옴
			read(fdd, buffer, BUFF_SIZE);

			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);

			//결과
			printf("my sum = %d\n", my_sum);
			printf("dealer sum = %d\n", dealer_sum);

			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");
			break;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movl $0x1, 0x2ab6(%rip)      # 0x4150
callq 0x1060
leaq 0x9f1(%rip), %rsi       # 0x2097
leaq 0xa19(%rip), %rdx       # 0x20c6
leaq -0x1a0(%rbp), %rbx
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1100
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10e0
movl %eax, -0x2c(%rbp)
cmpl $-0x1, %eax
jne 0x16e0
leaq 0xadb(%rip), %rdi       # 0x21b6
callq 0x1040
leaq 0x2ab9(%rip), %r12      # 0x41a0
leaq 0xb94(%rip), %rbx       # 0x2282
leaq 0xb89(%rip), %r14       # 0x227e
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpl $0x1, 0x2a4e(%rip)      # 0x4150
jne 0x18c4
movl $0x20, %edx
movl -0x2c(%rbp), %edi
movq %r12, %rsi
callq 0x1090
movb 0x2a82(%rip), %cl       # 0x41a0
xorl %eax, %eax
movl $0xa, %esi
cmpb %cl, (%rax,%rbx)
je 0x1738
incq %rax
cmpq $0xe, %rax
jne 0x1725
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x90(%rbp)
movb 0x2a5d(%rip), %cl       # 0x41a1
xorl %eax, %eax
cmpb %cl, (%rax,%r14)
je 0x175a
incq %rax
cmpq $0x4, %rax
jne 0x1746
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xe0(%rbp)
movb 0x2a3c(%rip), %cl       # 0x41a2
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x177b
incq %rax
cmpq $0xe, %rax
jne 0x1768
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x8c(%rbp)
movb 0x2a1c(%rip), %cl       # 0x41a3
xorl %eax, %eax
cmpb %cl, (%rax,%r14)
je 0x179d
incq %rax
cmpq $0x4, %rax
jne 0x1789
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xdc(%rbp)
movb 0x29fb(%rip), %cl       # 0x41a4
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x17be
incq %rax
cmpq $0xe, %rax
jne 0x17ab
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x180(%rbp)
movb 0x29db(%rip), %cl       # 0x41a5
xorl %eax, %eax
cmpb %cl, (%rax,%r14)
je 0x17e0
incq %rax
cmpq $0x4, %rax
jne 0x17cc
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x130(%rbp)
xorl %ecx, %ecx
xorl %eax, %eax
movl -0x90(%rbp,%rcx,4), %edx
cmpl $0xa, %edx
cmovgel %esi, %edx
addl %edx, %eax
incq %rcx
cmpq $0x2, %rcx
jne 0x17ea
xorl %ecx, %ecx
movl %eax, %edx
leal 0xa(%rdx), %eax
cmpl $0xc, %edx
cmovgel %edx, %eax
cmpl $0x1, -0x90(%rbp,%rcx,4)
cmovnel %edx, %eax
incq %rcx
cmpq $0x2, %rcx
jne 0x1804
movl %eax, 0x2997(%rip)      # 0x41c0
movl $0xa, %edi
callq 0x1030
leaq 0x899(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0x90(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movl $0x2, %edx
callq 0x1300
leaq 0x87d(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x180(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x1, %edx
callq 0x1300
movl 0x293a(%rip), %r15d     # 0x41c0
cmpl $0x16, %r15d
jge 0x18a2
movl $0x0, 0x28ba(%rip)      # 0x4150
movl $0x1, 0x28b4(%rip)      # 0x4154
jmp 0x18ae
leaq 0x9c0(%rip), %rdi       # 0x2269
callq 0x1040
cmpl $0x15, %r15d
jg 0x1bb8
movl $0x2, %r15d
movl $0x1, %r13d
movl $0x5, %r12d
movl $0xa, %edi
callq 0x1030
movl $0x1, %edi
callq 0x1130
decl %r12d
cmpl $0x1, %r12d
ja 0x18ca
cmpl $0x2, 0x2866(%rip)      # 0x4154
jne 0x19f9
movl $0x20, %edx
movl -0x2c(%rbp), %edi
leaq 0x289d(%rip), %rsi      # 0x41a0
callq 0x1090
movsbl 0x2891(%rip), %esi      # 0x41a0
movsbl 0x288b(%rip), %edx      # 0x41a1
leaq 0x7ce(%rip), %rdi       # 0x20eb
xorl %eax, %eax
callq 0x1080
movb 0x2876(%rip), %cl       # 0x41a0
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x193f
incq %rax
cmpq $0xe, %rax
jne 0x192c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movslq %r15d, %r15
movl %eax, -0x90(%rbp,%r15,4)
movb 0x2851(%rip), %cl       # 0x41a1
xorl %eax, %eax
cmpb %cl, (%rax,%r14)
je 0x1966
incq %rax
cmpq $0x4, %rax
jne 0x1952
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xe0(%rbp,%r15,4)
incl %r15d
movl $0xa, %edi
callq 0x1030
leaq 0x751(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0x90(%rbp), %r12
movq %r12, %rdi
leaq -0xe0(%rbp), %rsi
movl %r15d, %edx
callq 0x1300
leaq 0x734(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x180(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl %r13d, %edx
callq 0x1300
movq %r12, %rdi
movl %r15d, %esi
callq 0x12ab
movl %eax, 0x27e9(%rip)      # 0x41c0
cmpl $0x15, %eax
jle 0x19ef
leaq 0x886(%rip), %rdi       # 0x2269
leaq 0x27b6(%rip), %r12      # 0x41a0
jmp 0x1ba9
movl $0x1, 0x275b(%rip)      # 0x4154
movl $0x5, %r12d
movl $0xa, %edi
callq 0x1030
movl $0x1, %edi
callq 0x1130
decl %r12d
cmpl $0x1, %r12d
ja 0x19ff
movb $0x1, %al
cmpl $0x1, 0x2733(%rip)      # 0x4158
jne 0x1b72
movl $0x0, 0x2723(%rip)      # 0x4158
movl $0x20, %edx
movl -0x2c(%rbp), %edi
leaq 0x275c(%rip), %r12      # 0x41a0
movq %r12, %rsi
callq 0x1090
movq %r12, %rdi
callq 0x1070
testq %rax, %rax
je 0x1ab4
movslq %r13d, %r13
xorl %ecx, %ecx
movb (%rcx,%r12), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%rbx)
je 0x1a78
incq %rdx
cmpq $0xe, %rdx
jne 0x1a64
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, -0x180(%rbp,%r13,4)
movb 0x1(%rcx,%r12), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%r14)
je 0x1a9b
incq %rdx
cmpq $0x4, %rdx
jne 0x1a87
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, -0x130(%rbp,%r13,4)
incq %r13
addq $0x2, %rcx
movl %ecx, %edx
andl $-0x2, %edx
cmpq %rdx, %rax
ja 0x1a5e
movl $0xa, %edi
callq 0x1030
leaq 0x60e(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0x90(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movl %r15d, %edx
callq 0x1300
leaq 0x5f4(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
movq %r15, %r12
leaq -0x180(%rbp), %r15
movq %r15, %rdi
leaq -0x130(%rbp), %rsi
movl %r13d, %edx
callq 0x1300
leaq -0x90(%rbp), %rdi
movq %r12, -0x38(%rbp)
movl %r12d, %esi
callq 0x12ab
movl %eax, %r12d
movl %eax, 0x2698(%rip)      # 0x41c0
movq %r15, %rdi
movl %r13d, %esi
callq 0x12ab
movl %eax, 0x268b(%rip)      # 0x41c4
leaq 0x5bd(%rip), %rdi       # 0x20fd
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
movl 0x2674(%rip), %esi      # 0x41c4
leaq 0x5b3(%rip), %rdi       # 0x210a
xorl %eax, %eax
callq 0x1080
movl 0x2660(%rip), %eax      # 0x41c4
cmpl $0x15, %eax
jle 0x1b7b
leaq 0x6e2(%rip), %rdi       # 0x2252
jmp 0x1b9e
leaq 0x2627(%rip), %r12      # 0x41a0
jmp 0x1bb0
cmpl %eax, 0x263f(%rip)      # 0x41c0
jne 0x1b8c
leaq 0x693(%rip), %rdi       # 0x221d
jmp 0x1b9e
leaq 0x647(%rip), %rdi       # 0x21da
leaq 0x65f(%rip), %rax       # 0x21f9
cmovlq %rax, %rdi
leaq 0x25fb(%rip), %r12      # 0x41a0
movq -0x38(%rbp), %r15
callq 0x1040
xorl %eax, %eax
testb %al, %al
jne 0x16fb
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq