// File: /workspace/github_c_makefile_star2_O0/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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movl $0x0, -0x154(%rbp)
movl $0x0, -0x158(%rbp)
leaq 0x194f(%rip), %rdi      # 0x3159
movb $0x0, %al
callq 0x1090
movq 0x3980(%rip), %rax      # 0x5198
cmpl $0x1, (%rax)
jne 0x199e
movl 0x396d(%rip), %edi      # 0x5194
leaq 0x3942(%rip), %rsi      # 0x5170
movl $0x1, %edx
callq 0x1150
cmpl $-0x1, %eax
jne 0x1853
leaq 0x18d4(%rip), %rdi      # 0x3118
callq 0x1100
movl $0x1, %edi
callq 0x1160
movq 0x393e(%rip), %rsi      # 0x5198
addq $0xc, %rsi
leaq 0x393b(%rip), %rdi      # 0x51a0
movl $0x20, %edx
callq 0x1040
leaq 0x18ff(%rip), %rdi      # 0x3175
leaq 0x3923(%rip), %rsi      # 0x51a0
movb $0x0, %al
callq 0x1090
movsbl 0x3915(%rip), %edi      # 0x51a0
callq 0x1330
movl %eax, -0x60(%rbp)
movsbl 0x3907(%rip), %edi      # 0x51a1
callq 0x12d0
movl %eax, -0x100(%rbp)
movsbl 0x38f6(%rip), %edi      # 0x51a2
callq 0x1330
movl %eax, -0x5c(%rbp)
movsbl 0x38e8(%rip), %edi      # 0x51a3
callq 0x12d0
movl %eax, -0xfc(%rbp)
movsbl 0x38d7(%rip), %edi      # 0x51a4
callq 0x1330
movl %eax, -0xb0(%rbp)
movsbl 0x38c6(%rip), %edi      # 0x51a5
callq 0x12d0
movl %eax, -0x150(%rbp)
movl $0x2, -0x154(%rbp)
movl $0x1, -0x158(%rbp)
leaq -0x60(%rbp), %rdi
movl -0x154(%rbp), %esi
callq 0x1390
movl %eax, 0x38ad(%rip)      # 0x51c0
leaq 0x1a27(%rip), %rdi      # 0x3341
movb $0x0, %al
callq 0x1090
leaq 0x185c(%rip), %rdi      # 0x3184
movb $0x0, %al
callq 0x1090
leaq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl -0x154(%rbp), %edx
callq 0x1430
leaq 0x1842(%rip), %rdi      # 0x318e
movb $0x0, %al
callq 0x1090
leaq -0xb0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl -0x158(%rbp), %edx
callq 0x1430
movq 0x3825(%rip), %rax      # 0x5198
movl $0x0, (%rax)
movq 0x3818(%rip), %rax      # 0x5198
movl $0x1, 0x4(%rax)
movl 0x3807(%rip), %edi      # 0x5194
leaq 0x37e2(%rip), %rsi      # 0x5176
movl $0x1, %edx
callq 0x1150
cmpl $0x15, 0x381b(%rip)     # 0x51c0
jle 0x19e1
leaq 0x17ee(%rip), %rdi      # 0x319c
movb $0x0, %al
callq 0x1090
movq 0x37dc(%rip), %rdi      # 0x5198
callq 0x1070
cmpl $-0x1, %eax
jne 0x19dc
leaq 0x17e5(%rip), %rdi      # 0x31b2
callq 0x1100
movl $0x1, %edi
callq 0x1160
jmp 0x1d8d
movq 0x37b0(%rip), %rax      # 0x5198
cmpl $0x2, (%rax)
jne 0x1b2f
movl 0x379d(%rip), %edi      # 0x5194
leaq 0x3772(%rip), %rsi      # 0x5170
movl $0x1, %edx
callq 0x1150
cmpl $-0x1, %eax
jne 0x1a23
leaq 0x1704(%rip), %rdi      # 0x3118
callq 0x1100
movl $0x1, %edi
callq 0x1160
movq 0x376e(%rip), %rsi      # 0x5198
addq $0xc, %rsi
leaq 0x376b(%rip), %rdi      # 0x51a0
movl $0x20, %edx
callq 0x1040
leaq 0x1779(%rip), %rdi      # 0x31bf
leaq 0x3753(%rip), %rsi      # 0x51a0
movb $0x0, %al
callq 0x1090
movsbl 0x3745(%rip), %edi      # 0x51a0
callq 0x1330
movl %eax, %ecx
movslq -0x154(%rbp), %rax
movl %ecx, -0x60(%rbp,%rax,4)
movsbl 0x372d(%rip), %edi      # 0x51a1
callq 0x12d0
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 0x18a2(%rip), %rdi      # 0x3341
movb $0x0, %al
callq 0x1090
leaq 0x16d7(%rip), %rdi      # 0x3184
movb $0x0, %al
callq 0x1090
leaq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl -0x154(%rbp), %edx
callq 0x1430
movl %eax, %ecx
movq 0x36c5(%rip), %rax      # 0x5198
movl %ecx, 0x8(%rax)
leaq 0x16b1(%rip), %rdi      # 0x318e
movb $0x0, %al
callq 0x1090
leaq -0xb0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl -0x158(%rbp), %edx
callq 0x1430
movq 0x3694(%rip), %rax      # 0x5198
movl $0x0, (%rax)
movq 0x3687(%rip), %rax      # 0x5198
movl $0x1, 0x4(%rax)
movl 0x3676(%rip), %edi      # 0x5194
leaq 0x3651(%rip), %rsi      # 0x5176
movl $0x1, %edx
callq 0x1150
movq 0x3662(%rip), %rax      # 0x5198
cmpl $0x0, 0x8(%rax)
je 0x1d88
movq 0x3651(%rip), %rax      # 0x5198
movl $0x0, 0x8(%rax)
movl 0x3640(%rip), %edi      # 0x5194
leaq 0x3615(%rip), %rsi      # 0x5170
movl $0x1, %edx
callq 0x1150
cmpl $-0x1, %eax
jne 0x1b80
leaq 0x15a7(%rip), %rdi      # 0x3118
callq 0x1100
movl $0x1, %edi
callq 0x1160
movq 0x3611(%rip), %rsi      # 0x5198
addq $0xc, %rsi
leaq 0x360e(%rip), %rdi      # 0x51a0
movl $0x20, %edx
callq 0x1040
movq 0x35f5(%rip), %rax      # 0x5198
movl $0x0, (%rax)
movq 0x35e8(%rip), %rax      # 0x5198
movl $0x0, 0x4(%rax)
leaq 0x1601(%rip), %rdi      # 0x31bf
leaq 0x35db(%rip), %rsi      # 0x51a0
movb $0x0, %al
callq 0x1090
movl $0x0, -0x160(%rbp)
movl -0x160(%rbp), %eax
movq %rax, -0x168(%rbp)
leaq 0x35b6(%rip), %rdi      # 0x51a0
callq 0x1080
movq %rax, %rcx
movq -0x168(%rbp), %rax
cmpq %rcx, %rax
jae 0x1c76
movl -0x160(%rbp), %eax
movl %eax, %ecx
leaq 0x3593(%rip), %rax      # 0x51a0
movsbl (%rax,%rcx), %edi
callq 0x1330
movl %eax, %ecx
movslq -0x158(%rbp), %rax
movl %ecx, -0xb0(%rbp,%rax,4)
movl -0x160(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x3566(%rip), %rax      # 0x51a0
movsbl (%rax,%rcx), %edi
callq 0x12d0
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 -0x160(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x160(%rbp)
jmp 0x1bd6
movl 0x3518(%rip), %edi      # 0x5194
leaq 0x34f3(%rip), %rsi      # 0x5176
movl $0x1, %edx
callq 0x1150
leaq 0x16ad(%rip), %rdi      # 0x3341
movb $0x0, %al
callq 0x1090
leaq 0x14e2(%rip), %rdi      # 0x3184
movb $0x0, %al
callq 0x1090
leaq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl -0x154(%rbp), %edx
callq 0x1430
movl %eax, %ecx
movq 0x34d0(%rip), %rax      # 0x5198
movl %ecx, 0x8(%rax)
leaq 0x14bc(%rip), %rdi      # 0x318e
movb $0x0, %al
callq 0x1090
leaq -0xb0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl -0x158(%rbp), %edx
callq 0x1430
leaq -0x60(%rbp), %rdi
movl -0x154(%rbp), %esi
callq 0x1390
movl %eax, 0x34b9(%rip)      # 0x51c0
leaq -0xb0(%rbp), %rdi
movl -0x158(%rbp), %esi
callq 0x1390
movl %eax, 0x34a5(%rip)      # 0x51c4
cmpl $0x15, 0x349e(%rip)     # 0x51c4
jle 0x1d38
leaq 0x14a0(%rip), %rdi      # 0x31cf
movb $0x0, %al
callq 0x1090
jmp 0x1d86
movl 0x3482(%rip), %eax      # 0x51c0
cmpl 0x3480(%rip), %eax      # 0x51c4
jne 0x1d56
leaq 0x149a(%rip), %rdi      # 0x31e7
movb $0x0, %al
callq 0x1090
jmp 0x1d84
movl 0x3464(%rip), %eax      # 0x51c0
cmpl 0x3462(%rip), %eax      # 0x51c4
jge 0x1d74
leaq 0x14b2(%rip), %rdi      # 0x321d
movb $0x0, %al
callq 0x1090
jmp 0x1d82
leaq 0x14c7(%rip), %rdi      # 0x3242
movb $0x0, %al
callq 0x1090
jmp 0x1d84
jmp 0x1d86
jmp 0x1d8d
jmp 0x1811
movq 0x3404(%rip), %rdi      # 0x5198
callq 0x1070
cmpl $-0x1, %eax
jne 0x1db4
leaq 0x140d(%rip), %rdi      # 0x31b2
callq 0x1100
movl $0x1, %edi
callq 0x1160
movq -0x8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw %cs:(%rax,%rax)