// File: /workspace/github_c_makefile_star2_O2/PARKINHYO[P]BlackjackGame/shm/shm_client.c
// Function: recv_msg
void* recv_msg(void* x)
{
  /*게임 플레이에 쓸 카드 변수들 */
  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 */
  printf("게임 시작 대기중...\n");


  while(1) {
    /*첫 카드덱을 받을 경우*/
     if(pstShm2->check==1){
       /*임계영역*/
       if( semop(gnSemID2, &mysem_open, 1) == -1 )
       {
               perror("semop");
               exit(1);
       }
       strncpy(buffer, pstShm2->data, BUFFER_SIZE);
       printf("received : %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;

       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);
       /*체크 시그널 수정*/
       pstShm2->check = 0;
       pstShm2->check2 = 1;

       semop(gnSemID2, &mysem_close, 1);
       /*임계영역*/
     }

     /* 내 덱에 합계가 21이 넘어갈 경우 lose */
     if (my_sum > 21)
     {
       printf("\nI'm busted! I lose!\n");
       /*shared memory detached*/
       if(shmdt(pstShm2) == -1) {
          perror("shmdt failed");
          exit(1);
       }
       break;
     }

     /*HIT하여 새로운 카드 받는 부분*/
     if(pstShm2->check==2){
       /******* 임계영역 *******/
       if( semop(gnSemID2, &mysem_open, 1) == -1 )
       {
               perror("semop");
               exit(1);
       }
       /* recv new card */
       strncpy(buffer, pstShm2->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: ");
       /*클라이언트의 카드 합산이 21이 넘어갈 경우 게임 끝 시그널 설정*/
       pstShm2->finalcheck = display_state(my_hand_values, my_hand_suits, nmy);
       printf("Dealer Hand: ");
       /*카드덱과 합산값 화면출력*/
       display_state(dealer_hand_values, dealer_hand_suits, ndealer);
       /*체크 시그널 수정*/
       pstShm2->check = 0;
       pstShm2->check2 = 1;
       semop(gnSemID2, &mysem_close, 1);
       /******* 임계영역 *******/
     }
     /*게임 끝 시그널일 경우*/
     if(pstShm2->finalcheck){
       pstShm2->finalcheck=0;
       /******* 임계영역 *******/
       if( semop(gnSemID2, &mysem_open, 1) == -1 )
       {
               perror("semop");
               exit(1);
       }
       unsigned i;
       /* 딜러의 패를 받아온다. */
       strncpy(buffer, pstShm2->data, BUFFER_SIZE);
       pstShm2->check = 0;
       pstShm2->check2 = 0;
       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;
   		}
       semop(gnSemID2, &mysem_close, 1);
       /******* 임계영역 *******/

       printf("\n");
       /*마지막 패 보여주기*/
   		printf("My Hand: ");
   		pstShm2->finalcheck = 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");
      break;
     }

  }
  /*shared memory detached*/
  if(shmdt(pstShm2) == -1) {
     perror("shmdt failed");
     exit(1);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
leaq 0xb40(%rip), %rdi       # 0x21ba
callq 0x1060
movq 0x2b12(%rip), %rax      # 0x4198
pushq $0x1
popq %r15
leaq 0x2b0f(%rip), %rbx      # 0x41a0
pushq $0x2
popq %r12
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpl $0x1, (%rax)
jne 0x17e0
movl 0x2aea(%rip), %edi      # 0x4194
leaq 0x2abf(%rip), %rsi      # 0x4170
movq %r15, %rdx
callq 0x1150
cmpl $-0x1, %eax
je 0x1aa6
movq 0x2acf(%rip), %rsi      # 0x4198
addq $0xc, %rsi
movq %rbx, %rdi
pushq $0x20
popq %rdx
callq 0x1050
leaq 0x9ba(%rip), %rdi       # 0x2099
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movsbl 0x2ab0(%rip), %edi      # 0x41a0
callq 0x12da
movl %eax, -0xd0(%rbp)
movsbl 0x2a9f(%rip), %edi      # 0x41a1
callq 0x12b3
movl %eax, -0x80(%rbp)
movsbl 0x2a91(%rip), %edi      # 0x41a2
callq 0x12da
movl %eax, -0xcc(%rbp)
movsbl 0x2a80(%rip), %edi      # 0x41a3
callq 0x12b3
movl %eax, -0x7c(%rbp)
movsbl 0x2a72(%rip), %edi      # 0x41a4
callq 0x12da
movl %eax, -0x170(%rbp)
movsbl 0x2a61(%rip), %edi      # 0x41a5
callq 0x12b3
movl %eax, -0x120(%rbp)
leaq -0xd0(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x1301
movl %eax, 0x2a59(%rip)      # 0x41c0
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x932(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
leaq -0x80(%rbp), %rsi
movl %r12d, %edx
callq 0x134c
leaq 0x91f(%rip), %rdi       # 0x20b2
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
pushq $0x1
popq %r14
movl %r14d, %edx
callq 0x134c
movq 0x29dd(%rip), %rax      # 0x4198
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, (%rax)
movl 0x29c6(%rip), %edi      # 0x4194
leaq 0x29a1(%rip), %rsi      # 0x4176
movq %r15, %rdx
callq 0x1150
movl %r12d, %r13d
cmpl $0x16, 0x29d9(%rip)     # 0x41c0
jge 0x1995
movq 0x29a4(%rip), %rax      # 0x4198
cmpl $0x2, (%rax)
jne 0x18f4
movl 0x2991(%rip), %edi      # 0x4194
leaq 0x2966(%rip), %rsi      # 0x4170
movq %r15, %rdx
callq 0x1150
cmpl $-0x1, %eax
je 0x1aa6
movq 0x2976(%rip), %rsi      # 0x4198
addq $0xc, %rsi
movq %rbx, %rdi
pushq $0x20
popq %rdx
callq 0x1050
leaq 0x895(%rip), %rdi       # 0x20cd
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movsbl 0x2957(%rip), %edi      # 0x41a0
callq 0x12da
movslq %r13d, %r13
movl %eax, -0xd0(%rbp,%r13,4)
movsbl 0x2941(%rip), %edi      # 0x41a1
callq 0x12b3
movl %eax, -0x80(%rbp,%r13,4)
incl %r13d
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x82c(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1090
leaq -0xd0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movl %r13d, %edx
callq 0x134c
movq 0x28fb(%rip), %rcx      # 0x4198
movl %eax, 0x8(%rcx)
leaq 0x80b(%rip), %rdi       # 0x20b2
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl %r14d, %edx
callq 0x134c
movq 0x28cd(%rip), %rax      # 0x4198
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, (%rax)
movl 0x28b6(%rip), %edi      # 0x4194
leaq 0x2891(%rip), %rsi      # 0x4176
movq %r15, %rdx
callq 0x1150
movq 0x28a4(%rip), %rax      # 0x4198
cmpl $0x0, 0x8(%rax)
je 0x169b
andl $0x0, 0x8(%rax)
movl 0x288c(%rip), %edi      # 0x4194
leaq 0x2861(%rip), %rsi      # 0x4170
pushq $0x1
popq %rdx
callq 0x1150
cmpl $-0x1, %eax
je 0x1aa6
movq 0x2871(%rip), %r15      # 0x4198
leaq 0xc(%r15), %rsi
pushq $0x20
popq %rdx
movq %rbx, %rdi
callq 0x1050
andq $0x0, (%r15)
leaq 0x78c(%rip), %rdi       # 0x20cd
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movq %rbx, %rdi
callq 0x1080
movq %rax, %r15
movl %r14d, %r14d
pushq $0x1
popq %r12
leal -0x1(%r12), %eax
cmpq %rax, %r15
jbe 0x19bb
movsbl (%rax,%rbx), %edi
callq 0x12da
movl %eax, -0x170(%rbp,%r14,4)
movl %r12d, %eax
movsbl (%rax,%rbx), %edi
callq 0x12b3
movl %eax, -0x120(%rbp,%r14,4)
incq %r14
addl $0x2, %r12d
jmp 0x195d
leaq 0x8c8(%rip), %rdi       # 0x2264
callq 0x1060
movq 0x27f0(%rip), %rdi      # 0x4198
callq 0x1070
cmpl $-0x1, %eax
jne 0x1a83
jmp 0x1aba
movl 0x27d3(%rip), %edi      # 0x4194
leaq 0x27ae(%rip), %rsi      # 0x4176
pushq $0x1
popq %rdx
callq 0x1150
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x6c9(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1090
leaq -0xd0(%rbp), %rbx
leaq -0x80(%rbp), %rsi
movq %rbx, %rdi
movl %r13d, %edx
callq 0x134c
movq 0x2795(%rip), %rcx      # 0x4198
movl %eax, 0x8(%rcx)
leaq 0x6a5(%rip), %rdi       # 0x20b2
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %r15
leaq -0x120(%rbp), %rsi
movq %r15, %rdi
movl %r14d, %edx
callq 0x134c
movq %rbx, %rdi
movl %r13d, %esi
callq 0x1301
movl %eax, %ebx
movl %eax, 0x2780(%rip)      # 0x41c0
movq %r15, %rdi
movl %r14d, %esi
callq 0x1301
movl %eax, 0x2773(%rip)      # 0x41c4
cmpl $0x15, %eax
jle 0x1a5f
leaq 0x7f0(%rip), %rdi       # 0x224d
jmp 0x1a7e
cmpl %eax, %ebx
jne 0x1a6c
leaq 0x7ae(%rip), %rdi       # 0x2218
jmp 0x1a7e
leaq 0x781(%rip), %rax       # 0x21f4
leaq 0x75b(%rip), %rdi       # 0x21d5
cmovlq %rax, %rdi
callq 0x1060
movq 0x270e(%rip), %rdi      # 0x4198
callq 0x1070
cmpl $-0x1, %eax
je 0x1aba
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5d3(%rip), %rdi       # 0x2080
callq 0x1100
pushq $0x1
popq %rdi
callq 0x1160
leaq 0x5ff(%rip), %rdi       # 0x20c0
jmp 0x1aad