// File: /workspace/github_c_makefile_star2_O3/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, -0x34(%rbp)
cmpl $-0x1, %eax
jne 0x16e0
leaq 0xadb(%rip), %rdi       # 0x21b6
callq 0x1040
leaq 0xb9b(%rip), %r13       # 0x2282
leaq 0xb90(%rip), %rbx       # 0x227e
movl $0xa, %r14d
movq $0x0, -0x30(%rbp)
xorl %r12d, %r12d
cmpl $0x1, 0x2a4a(%rip)      # 0x4150
jne 0x18b3
movl $0x20, %edx
movl -0x34(%rbp), %edi
leaq 0x2a85(%rip), %rsi      # 0x41a0
callq 0x1090
movb 0x2a7a(%rip), %cl       # 0x41a0
xorl %eax, %eax
cmpb %cl, (%rax,%r13)
je 0x173c
incq %rax
cmpq $0xe, %rax
jne 0x1728
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x90(%rbp)
movb 0x2a59(%rip), %cl       # 0x41a1
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x175d
incq %rax
cmpq $0x4, %rax
jne 0x174a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xe0(%rbp)
movb 0x2a39(%rip), %cl       # 0x41a2
xorl %eax, %eax
cmpb %cl, (%rax,%r13)
je 0x177f
incq %rax
cmpq $0xe, %rax
jne 0x176b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x8c(%rbp)
movb 0x2a18(%rip), %cl       # 0x41a3
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x17a0
incq %rax
cmpq $0x4, %rax
jne 0x178d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xdc(%rbp)
movb 0x29f8(%rip), %cl       # 0x41a4
xorl %eax, %eax
cmpb %cl, (%rax,%r13)
je 0x17c2
incq %rax
cmpq $0xe, %rax
jne 0x17ae
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x180(%rbp)
movb 0x29d7(%rip), %cl       # 0x41a5
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x17e3
incq %rax
cmpq $0x4, %rax
jne 0x17d0
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 %r14d, %edx
addl %edx, %eax
incq %rcx
cmpq $0x2, %rcx
jne 0x17ed
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 0x1808
movl %eax, 0x2993(%rip)      # 0x41c0
movl $0xa, %edi
callq 0x1030
leaq 0x895(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0x90(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movl $0x2, %edx
callq 0x1300
leaq 0x879(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x180(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x1, %edx
callq 0x1300
cmpl $0x15, 0x2936(%rip)     # 0x41c0
jg 0x1b5c
movl $0x0, 0x28b6(%rip)      # 0x4150
movl $0x1, 0x28b0(%rip)      # 0x4154
movl $0x2, %r12d
movl $0x1, %eax
movq %rax, -0x30(%rbp)
movl $0x5, %r15d
movl $0xa, %edi
callq 0x1030
movl $0x1, %edi
callq 0x1130
decl %r15d
cmpl $0x1, %r15d
ja 0x18b9
cmpl $0x2, 0x2877(%rip)      # 0x4154
jne 0x19da
movl $0x20, %edx
movl -0x34(%rbp), %edi
leaq 0x28ae(%rip), %rsi      # 0x41a0
callq 0x1090
movsbl 0x28a2(%rip), %esi      # 0x41a0
movsbl 0x289c(%rip), %edx      # 0x41a1
leaq 0x7df(%rip), %rdi       # 0x20eb
xorl %eax, %eax
callq 0x1080
movb 0x2887(%rip), %cl       # 0x41a0
xorl %eax, %eax
cmpb %cl, (%rax,%r13)
je 0x192f
incq %rax
cmpq $0xe, %rax
jne 0x191b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movslq %r12d, %r12
movl %eax, -0x90(%rbp,%r12,4)
movb 0x2861(%rip), %cl       # 0x41a1
xorl %eax, %eax
cmpb %cl, (%rax,%rbx)
je 0x1955
incq %rax
cmpq $0x4, %rax
jne 0x1942
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xe0(%rbp,%r12,4)
incl %r12d
movl $0xa, %edi
callq 0x1030
leaq 0x762(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0x90(%rbp), %r15
movq %r15, %rdi
leaq -0xe0(%rbp), %rsi
movl %r12d, %edx
callq 0x1300
leaq 0x745(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x180(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x1300
movq %r15, %rdi
movl %r12d, %esi
callq 0x12ab
movl %eax, 0x27f9(%rip)      # 0x41c0
cmpl $0x15, %eax
jg 0x1b5c
movl $0x1, 0x277a(%rip)      # 0x4154
movl $0x5, %r15d
movl $0xa, %edi
callq 0x1030
movl $0x1, %edi
callq 0x1130
decl %r15d
cmpl $0x1, %r15d
ja 0x19e0
cmpl $0x1, 0x2754(%rip)      # 0x4158
jne 0x16ff
movl $0x0, 0x2744(%rip)      # 0x4158
leaq 0x2785(%rip), %r14      # 0x41a0
movl $0x20, %edx
movl -0x34(%rbp), %edi
movq %r14, %rsi
callq 0x1090
movq %r14, %rdi
callq 0x1070
testq %rax, %rax
je 0x1aa4
movq -0x30(%rbp), %rcx
movl %ecx, %ecx
movq %rcx, -0x30(%rbp)
xorl %ecx, %ecx
movb (%rcx,%r14), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%r13)
je 0x1a5e
incq %rdx
cmpq $0xe, %rdx
jne 0x1a4a
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rsi
movl %edx, -0x180(%rbp,%rsi,4)
movb 0x1(%rcx,%r14), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%rbx)
je 0x1a84
incq %rdx
cmpq $0x4, %rdx
jne 0x1a70
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rsi
movl %edx, -0x130(%rbp,%rsi,4)
incq %rsi
movq %rsi, -0x30(%rbp)
addq $0x2, %rcx
movl %ecx, %edx
andl $-0x2, %edx
cmpq %rdx, %rax
ja 0x1a44
movl $0xa, %edi
callq 0x1030
leaq 0x61e(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0x90(%rbp), %rbx
leaq -0xe0(%rbp), %rsi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x1300
leaq 0x601(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x180(%rbp), %r14
leaq -0x130(%rbp), %rsi
movq %r14, %rdi
movq -0x30(%rbp), %r15
movl %r15d, %edx
callq 0x1300
movq %rbx, %rdi
movl %r12d, %esi
callq 0x12ab
movl %eax, %ebx
movl %eax, 0x26ad(%rip)      # 0x41c0
movq %r14, %rdi
movl %r15d, %esi
callq 0x12ab
movl %eax, 0x26a0(%rip)      # 0x41c4
leaq 0x5d2(%rip), %rdi       # 0x20fd
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movl 0x268a(%rip), %esi      # 0x41c4
leaq 0x5c9(%rip), %rdi       # 0x210a
xorl %eax, %eax
callq 0x1080
movl 0x2676(%rip), %eax      # 0x41c4
cmpl $0x15, %eax
jle 0x1b79
leaq 0x6f8(%rip), %rdi       # 0x2252
jmp 0x1b63
leaq 0x706(%rip), %rdi       # 0x2269
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1040
cmpl %eax, 0x2641(%rip)      # 0x41c0
jne 0x1b8a
leaq 0x695(%rip), %rdi       # 0x221d
jmp 0x1b63
leaq 0x668(%rip), %rax       # 0x21f9
leaq 0x642(%rip), %rdi       # 0x21da
cmovlq %rax, %rdi
jmp 0x1b63