// File: /workspace/github_c_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movl $0x1, 0x2adb(%rip)      # 0x4150
callq 0x1060
leaq 0xa16(%rip), %rsi       # 0x2097
leaq 0xa3e(%rip), %rdx       # 0x20c6
leaq -0x190(%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 0x16bb
leaq 0xb00(%rip), %rdi       # 0x21b6
callq 0x1040
pushq $0xa
popq %r12
pushq $0x1
popq %r15
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpl $0x1, 0x2a80(%rip)      # 0x4150
jne 0x17d4
movl -0x2c(%rbp), %edi
leaq 0x2ac0(%rip), %rsi      # 0x41a0
pushq $0x20
popq %rdx
callq 0x1090
movsbl 0x2ab1(%rip), %edi      # 0x41a0
callq 0x128a
movl %eax, -0xd0(%rbp)
movsbl 0x2aa0(%rip), %edi      # 0x41a1
callq 0x1263
movl %eax, -0x80(%rbp)
movsbl 0x2a92(%rip), %edi      # 0x41a2
callq 0x128a
movl %eax, -0xcc(%rbp)
movsbl 0x2a81(%rip), %edi      # 0x41a3
callq 0x1263
movl %eax, -0x7c(%rbp)
movsbl 0x2a73(%rip), %edi      # 0x41a4
callq 0x128a
movl %eax, -0x170(%rbp)
movsbl 0x2a62(%rip), %edi      # 0x41a5
callq 0x1263
movl %eax, -0x120(%rbp)
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
pushq $0x2
popq %r13
movl %r13d, %esi
callq 0x12b1
movl %eax, 0x2a56(%rip)      # 0x41c0
movl %r12d, %edi
callq 0x1030
leaq 0x95a(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
movl %r13d, %edx
callq 0x12fc
leaq 0x947(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl %r15d, %edx
callq 0x12fc
cmpl $0x15, 0x2a06(%rip)     # 0x41c0
jg 0x195e
andl $0x0, 0x2989(%rip)      # 0x4150
movl $0x1, 0x2983(%rip)      # 0x4154
movl %r15d, %r14d
pushq $-0x4
popq %rbx
testl %ebx, %ebx
je 0x17ef
movl %r12d, %edi
callq 0x1030
movl %r15d, %edi
callq 0x1130
incl %ebx
jmp 0x17d7
cmpl $0x2, 0x295e(%rip)      # 0x4154
jne 0x18c9
movl -0x2c(%rbp), %edi
leaq 0x299a(%rip), %rsi      # 0x41a0
pushq $0x20
popq %rdx
callq 0x1090
movsbl 0x298b(%rip), %esi      # 0x41a0
movsbl 0x2985(%rip), %edx      # 0x41a1
leaq 0x8c8(%rip), %rdi       # 0x20eb
xorl %eax, %eax
callq 0x1080
movsbl 0x296f(%rip), %edi      # 0x41a0
callq 0x128a
movslq %r13d, %r13
movl %eax, -0xd0(%rbp,%r13,4)
movsbl 0x2959(%rip), %edi      # 0x41a1
callq 0x1263
movl %eax, -0x80(%rbp,%r13,4)
incl %r13d
movl %r12d, %edi
callq 0x1030
leaq 0x86f(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
movl %r13d, %edx
callq 0x12fc
leaq 0x855(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl %r14d, %edx
callq 0x12fc
movq %rbx, %rdi
movl %r13d, %esi
callq 0x12b1
movl %eax, 0x290a(%rip)      # 0x41c0
cmpl $0x15, %eax
jg 0x195e
movl $0x1, 0x288b(%rip)      # 0x4154
pushq $-0x4
popq %rbx
testl %ebx, %ebx
je 0x18e4
movl %r12d, %edi
callq 0x1030
movl %r15d, %edi
callq 0x1130
incl %ebx
jmp 0x18cc
cmpl $0x1, 0x286d(%rip)      # 0x4158
jne 0x16c9
andl $0x0, 0x2860(%rip)      # 0x4158
pushq $0x20
popq %rdx
movl -0x2c(%rbp), %edi
leaq 0x289b(%rip), %rbx      # 0x41a0
movq %rbx, %rsi
callq 0x1090
movq %rbx, %rdi
callq 0x1070
movq %rax, %r15
movl %r14d, %r14d
pushq $0x1
popq %r12
leal -0x1(%r12), %eax
cmpq %rax, %r15
jbe 0x196a
leaq 0x2870(%rip), %rbx      # 0x41a0
movsbl (%rax,%rbx), %edi
callq 0x128a
movl %eax, -0x170(%rbp,%r14,4)
movl %r12d, %eax
movsbl (%rax,%rbx), %edi
callq 0x1263
movl %eax, -0x120(%rbp,%r14,4)
incq %r14
addl $0x2, %r12d
jmp 0x191f
leaq 0x904(%rip), %rdi       # 0x2269
jmp 0x1a3c
pushq $0xa
popq %rdi
callq 0x1030
leaq 0x75a(%rip), %rdi       # 0x20d3
xorl %eax, %eax
callq 0x1080
leaq -0xd0(%rbp), %rbx
leaq -0x80(%rbp), %rsi
movq %rbx, %rdi
movl %r13d, %edx
callq 0x12fc
leaq 0x740(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1080
leaq -0x170(%rbp), %r15
leaq -0x120(%rbp), %rsi
movq %r15, %rdi
movl %r14d, %edx
callq 0x12fc
movq %rbx, %rdi
movl %r13d, %esi
callq 0x12b1
movl %eax, %ebx
movl %eax, 0x27f0(%rip)      # 0x41c0
movq %r15, %rdi
movl %r14d, %esi
callq 0x12b1
movl %eax, 0x27e3(%rip)      # 0x41c4
leaq 0x715(%rip), %rdi       # 0x20fd
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movl 0x27cd(%rip), %esi      # 0x41c4
leaq 0x70c(%rip), %rdi       # 0x210a
xorl %eax, %eax
callq 0x1080
movl 0x27b9(%rip), %eax      # 0x41c4
cmpl $0x15, %eax
jle 0x1a19
leaq 0x83b(%rip), %rdi       # 0x2252
jmp 0x1a3c
cmpl %eax, 0x27a1(%rip)      # 0x41c0
jne 0x1a2a
leaq 0x7f5(%rip), %rdi       # 0x221d
jmp 0x1a3c
leaq 0x7c8(%rip), %rax       # 0x21f9
leaq 0x7a2(%rip), %rdi       # 0x21da
cmovlq %rax, %rdi
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1040