// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1a0, %rsp            # imm = 0x1A0
movl $0x1, 0x293b(%rip)      # 0x4150
movl $0x0, -0x154(%rbp)
movl $0x0, -0x158(%rbp)
leaq -0x180(%rbp), %rax
movq %rax, -0x198(%rbp)
callq 0x1050
movq -0x198(%rbp), %rdi
movl %eax, %ecx
leaq 0x891(%rip), %rsi       # 0x20dd
leaq 0x918(%rip), %rdx       # 0x216b
movb $0x0, %al
callq 0x10f0
leaq -0x180(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x10d0
movl %eax, -0x184(%rbp)
cmpl $-0x1, %eax
jne 0x1883
leaq 0x8fc(%rip), %rdi       # 0x2178
movb $0x0, %al
callq 0x1070
jmp 0x1885
cmpl $0x1, 0x28c4(%rip)      # 0x4150
jne 0x19c1
movl -0x184(%rbp), %edi
leaq 0x2901(%rip), %rsi      # 0x41a0
movl $0x20, %edx
callq 0x1080
movsbl 0x28f0(%rip), %edi      # 0x41a0
callq 0x12d0
movl %eax, -0x60(%rbp)
movsbl 0x28e2(%rip), %edi      # 0x41a1
callq 0x1270
movl %eax, -0x100(%rbp)
movsbl 0x28d1(%rip), %edi      # 0x41a2
callq 0x12d0
movl %eax, -0x5c(%rbp)
movsbl 0x28c3(%rip), %edi      # 0x41a3
callq 0x1270
movl %eax, -0xfc(%rbp)
movsbl 0x28b2(%rip), %edi      # 0x41a4
callq 0x12d0
movl %eax, -0xb0(%rbp)
movsbl 0x28a1(%rip), %edi      # 0x41a5
callq 0x1270
movl %eax, -0x150(%rbp)
movl $0x2, -0x154(%rbp)
movl $0x1, -0x158(%rbp)
leaq -0x60(%rbp), %rdi
movl -0x154(%rbp), %esi
callq 0x1330
movl %eax, 0x2888(%rip)      # 0x41c0
leaq 0x79c(%rip), %rdi       # 0x20db
movb $0x0, %al
callq 0x1070
leaq 0x850(%rip), %rdi       # 0x219d
movb $0x0, %al
callq 0x1070
leaq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl -0x154(%rbp), %edx
callq 0x13d0
leaq 0x836(%rip), %rdi       # 0x21a7
movb $0x0, %al
callq 0x1070
leaq -0xb0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl -0x158(%rbp), %edx
callq 0x13d0
cmpl $0x15, 0x2828(%rip)     # 0x41c0
jle 0x19ad
leaq 0x814(%rip), %rdi       # 0x21b5
movb $0x0, %al
callq 0x1070
jmp 0x1d45
movl $0x0, 0x2799(%rip)      # 0x4150
movl $0x1, 0x2793(%rip)      # 0x4154
movl $0x4, -0x18c(%rbp)
cmpl $0x0, -0x18c(%rbp)
jle 0x19fd
leaq 0x700(%rip), %rdi       # 0x20db
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1120
movl -0x18c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18c(%rbp)
jmp 0x19cb
cmpl $0x2, 0x2750(%rip)      # 0x4154
jne 0x1b15
movl -0x184(%rbp), %edi
leaq 0x2789(%rip), %rsi      # 0x41a0
movl $0x20, %edx
callq 0x1080
movsbl 0x2778(%rip), %esi      # 0x41a0
movsbl 0x2772(%rip), %edx      # 0x41a1
leaq 0x795(%rip), %rdi       # 0x21cb
movb $0x0, %al
callq 0x1070
movsbl 0x275c(%rip), %edi      # 0x41a0
callq 0x12d0
movl %eax, %ecx
movslq -0x154(%rbp), %rax
movl %ecx, -0x60(%rbp,%rax,4)
movsbl 0x2744(%rip), %edi      # 0x41a1
callq 0x1270
movl %eax, %ecx
movslq -0x154(%rbp), %rax
movl %ecx, -0x100(%rbp,%rax,4)
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
leaq 0x653(%rip), %rdi       # 0x20db
movb $0x0, %al
callq 0x1070
leaq 0x707(%rip), %rdi       # 0x219d
movb $0x0, %al
callq 0x1070
leaq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl -0x154(%rbp), %edx
callq 0x13d0
leaq 0x6ed(%rip), %rdi       # 0x21a7
movb $0x0, %al
callq 0x1070
leaq -0xb0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl -0x158(%rbp), %edx
callq 0x13d0
leaq -0x60(%rbp), %rdi
movl -0x154(%rbp), %esi
callq 0x1330
movl %eax, 0x26d1(%rip)      # 0x41c0
cmpl $0x15, 0x26ca(%rip)     # 0x41c0
jle 0x1b0b
leaq 0x6b6(%rip), %rdi       # 0x21b5
movb $0x0, %al
callq 0x1070
jmp 0x1d45
movl $0x1, 0x263f(%rip)      # 0x4154
movl $0x4, -0x18c(%rbp)
cmpl $0x0, -0x18c(%rbp)
jle 0x1b51
leaq 0x5ac(%rip), %rdi       # 0x20db
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1120
movl -0x18c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18c(%rbp)
jmp 0x1b1f
cmpl $0x1, 0x2600(%rip)      # 0x4158
jne 0x1d40
movl $0x0, 0x25f0(%rip)      # 0x4158
movl -0x184(%rbp), %edi
leaq 0x262b(%rip), %rsi      # 0x41a0
movl $0x20, %edx
callq 0x1080
movl $0x0, -0x190(%rbp)
movl -0x190(%rbp), %eax
movq %rax, -0x1a0(%rbp)
leaq 0x2603(%rip), %rdi      # 0x41a0
callq 0x1060
movq %rax, %rcx
movq -0x1a0(%rbp), %rax
cmpq %rcx, %rax
jae 0x1c29
movl -0x190(%rbp), %eax
movl %eax, %ecx
leaq 0x25e0(%rip), %rax      # 0x41a0
movsbl (%rax,%rcx), %edi
callq 0x12d0
movl %eax, %ecx
movslq -0x158(%rbp), %rax
movl %ecx, -0xb0(%rbp,%rax,4)
movl -0x190(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x25b3(%rip), %rax      # 0x41a0
movsbl (%rax,%rcx), %edi
callq 0x1270
movl %eax, %ecx
movslq -0x158(%rbp), %rax
movl %ecx, -0x150(%rbp,%rax,4)
movl -0x158(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x158(%rbp)
movl -0x190(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x190(%rbp)
jmp 0x1b89
leaq 0x4ab(%rip), %rdi       # 0x20db
movb $0x0, %al
callq 0x1070
leaq 0x55f(%rip), %rdi       # 0x219d
movb $0x0, %al
callq 0x1070
leaq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl -0x154(%rbp), %edx
callq 0x13d0
leaq 0x545(%rip), %rdi       # 0x21a7
movb $0x0, %al
callq 0x1070
leaq -0xb0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl -0x158(%rbp), %edx
callq 0x13d0
leaq -0x60(%rbp), %rdi
movl -0x154(%rbp), %esi
callq 0x1330
movl %eax, 0x2529(%rip)      # 0x41c0
leaq -0xb0(%rbp), %rdi
movl -0x158(%rbp), %esi
callq 0x1330
movl %eax, 0x2515(%rip)      # 0x41c4
movl 0x250b(%rip), %esi      # 0x41c0
leaq 0x521(%rip), %rdi       # 0x21dd
movb $0x0, %al
callq 0x1070
movl 0x24fb(%rip), %esi      # 0x41c4
leaq 0x51a(%rip), %rdi       # 0x21ea
movb $0x0, %al
callq 0x1070
cmpl $0x15, 0x24e6(%rip)     # 0x41c4
jle 0x1cf0
leaq 0x514(%rip), %rdi       # 0x21fb
movb $0x0, %al
callq 0x1070
jmp 0x1d3e
movl 0x24ca(%rip), %eax      # 0x41c0
cmpl 0x24c8(%rip), %eax      # 0x41c4
jne 0x1d0e
leaq 0x50e(%rip), %rdi       # 0x2213
movb $0x0, %al
callq 0x1070
jmp 0x1d3c
movl 0x24ac(%rip), %eax      # 0x41c0
cmpl 0x24aa(%rip), %eax      # 0x41c4
jge 0x1d2c
leaq 0x526(%rip), %rdi       # 0x2249
movb $0x0, %al
callq 0x1070
jmp 0x1d3a
leaq 0x53b(%rip), %rdi       # 0x226e
movb $0x0, %al
callq 0x1070
jmp 0x1d3c
jmp 0x1d3e
jmp 0x1d45
jmp 0x1885
movq -0x8(%rbp), %rax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq