// File: /workspace/github_c_makefile_star2_O1/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 0xa2d(%rip), %rdi       # 0x21ba
callq 0x1060
leaq 0xb80(%rip), %r13       # 0x2319
leaq 0xb75(%rip), %r15       # 0x2315
movl $0xa, %r14d
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x29e6(%rip), %rax      # 0x4198
cmpl $0x1, (%rax)
jne 0x1998
movl 0x29d3(%rip), %edi      # 0x4194
movl $0x1, %edx
leaq 0x29a3(%rip), %rsi      # 0x4170
callq 0x1150
cmpl $-0x1, %eax
je 0x1cd0
movq 0x29b6(%rip), %rsi      # 0x4198
addq $0xc, %rsi
movl $0x20, %edx
leaq 0x29ae(%rip), %rbx      # 0x41a0
movq %rbx, %rdi
callq 0x1050
leaq 0x898(%rip), %rdi       # 0x2099
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movb 0x298f(%rip), %cl       # 0x41a0
xorl %eax, %eax
cmpb %cl, (%rax,%r13)
je 0x1827
incq %rax
cmpq $0xe, %rax
jne 0x1813
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x80(%rbp)
movb 0x2971(%rip), %cl       # 0x41a1
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x1846
incq %rax
cmpq $0x4, %rax
jne 0x1832
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xd0(%rbp)
movb 0x2950(%rip), %cl       # 0x41a2
xorl %eax, %eax
cmpb %cl, (%rax,%r13)
je 0x1868
incq %rax
cmpq $0xe, %rax
jne 0x1854
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x7c(%rbp)
movb 0x2932(%rip), %cl       # 0x41a3
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x1887
incq %rax
cmpq $0x4, %rax
jne 0x1873
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xcc(%rbp)
movb 0x2911(%rip), %cl       # 0x41a4
xorl %eax, %eax
cmpb %cl, (%rax,%r13)
je 0x18a9
incq %rax
cmpq $0xe, %rax
jne 0x1895
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x170(%rbp)
movb 0x28f0(%rip), %cl       # 0x41a5
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x18cb
incq %rax
cmpq $0x4, %rax
jne 0x18b7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x120(%rbp)
xorl %ecx, %ecx
xorl %eax, %eax
movl -0x80(%rbp,%rcx,4), %edx
cmpl $0xa, %edx
cmovgel %r14d, %edx
addl %edx, %eax
incq %rcx
cmpq $0x2, %rcx
jne 0x18d5
xorl %ecx, %ecx
movl %eax, %edx
leal 0xa(%rdx), %eax
cmpl $0xc, %edx
cmovgel %edx, %eax
cmpl $0x1, -0x80(%rbp,%rcx,4)
cmovnel %edx, %eax
incq %rcx
cmpq $0x2, %rcx
jne 0x18ed
movl %eax, 0x28b1(%rip)      # 0x41c0
movl $0xa, %edi
callq 0x1040
leaq 0x788(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1090
movl $0x2, %ebx
leaq -0x80(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movl $0x2, %edx
callq 0x1440
leaq 0x76a(%rip), %rdi       # 0x20b2
xorl %eax, %eax
callq 0x1090
movl $0x1, %r12d
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl $0x1, %edx
callq 0x1440
movq 0x2824(%rip), %rax      # 0x4198
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, (%rax)
movl 0x280d(%rip), %edi      # 0x4194
movl $0x1, %edx
leaq 0x27e3(%rip), %rsi      # 0x4176
callq 0x1150
cmpl $0x16, 0x2821(%rip)     # 0x41c0
jge 0x1c67
movq 0x27ec(%rip), %rax      # 0x4198
cmpl $0x2, (%rax)
jne 0x1ae2
movq %r12, %r14
movq %rbx, %r12
movl 0x27d3(%rip), %edi      # 0x4194
movl $0x1, %edx
leaq 0x27a3(%rip), %rsi      # 0x4170
callq 0x1150
cmpl $-0x1, %eax
je 0x1cd5
movq 0x27b6(%rip), %rsi      # 0x4198
addq $0xc, %rsi
movl $0x20, %edx
leaq 0x27ae(%rip), %rbx      # 0x41a0
movq %rbx, %rdi
callq 0x1050
leaq 0x6cc(%rip), %rdi       # 0x20cd
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movb 0x278f(%rip), %cl       # 0x41a0
xorl %eax, %eax
movq %r12, %rbx
movq %r14, %r12
movl $0xa, %r14d
cmpb %cl, (%rax,%r13)
je 0x1a33
incq %rax
cmpq $0xe, %rax
jne 0x1a1f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movslq %ebx, %rbx
movl %eax, -0x80(%rbp,%rbx,4)
movb 0x2761(%rip), %cl       # 0x41a1
xorl %eax, %eax
cmpb %cl, (%rax,%r15)
je 0x1a56
incq %rax
cmpq $0x4, %rax
jne 0x1a42
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xd0(%rbp,%rbx,4)
incl %ebx
movl $0xa, %edi
callq 0x1040
leaq 0x638(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1090
leaq -0x80(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movl %ebx, %edx
callq 0x1440
movq 0x2708(%rip), %rcx      # 0x4198
movl %eax, 0x8(%rcx)
leaq 0x618(%rip), %rdi       # 0x20b2
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl %r12d, %edx
callq 0x1440
movq 0x26da(%rip), %rax      # 0x4198
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, (%rax)
movl 0x26c3(%rip), %edi      # 0x4194
movl $0x1, %edx
leaq 0x2699(%rip), %rsi      # 0x4176
callq 0x1150
movq 0x26af(%rip), %rax      # 0x4198
cmpl $0x0, 0x8(%rax)
je 0x17ab
movl $0x0, 0x8(%rax)
movl 0x2694(%rip), %edi      # 0x4194
leaq 0x2669(%rip), %rsi      # 0x4170
movl $0x1, %edx
callq 0x1150
cmpl $-0x1, %eax
je 0x1cdf
movq 0x2677(%rip), %rax      # 0x4198
movq %rax, -0x30(%rbp)
leaq 0xc(%rax), %rsi
leaq 0x2670(%rip), %r14      # 0x41a0
movl $0x20, %edx
movq %r14, %rdi
callq 0x1050
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
leaq 0x57e(%rip), %rdi       # 0x20cd
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
callq 0x1080
testq %rax, %rax
je 0x1bc1
movl %r12d, %r12d
xorl %ecx, %ecx
movb (%rcx,%r14), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%r13)
je 0x1b85
incq %rdx
cmpq $0xe, %rdx
jne 0x1b71
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, -0x170(%rbp,%r12,4)
movb 0x1(%rcx,%r14), %sil
xorl %edx, %edx
cmpb %sil, (%rdx,%r15)
je 0x1ba8
incq %rdx
cmpq $0x4, %rdx
jne 0x1b94
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, -0x120(%rbp,%r12,4)
incq %r12
addq $0x2, %rcx
movl %ecx, %edx
andl $-0x2, %edx
cmpq %rdx, %rax
ja 0x1b6b
movl 0x25cd(%rip), %edi      # 0x4194
leaq 0x25a8(%rip), %rsi      # 0x4176
movl $0x1, %edx
callq 0x1150
movl $0xa, %edi
callq 0x1040
leaq 0x4bf(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1090
leaq -0x80(%rbp), %r14
leaq -0xd0(%rbp), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x1440
movq 0x258c(%rip), %rcx      # 0x4198
movl %eax, 0x8(%rcx)
leaq 0x49c(%rip), %rdi       # 0x20b2
xorl %eax, %eax
callq 0x1090
leaq -0x170(%rbp), %r15
leaq -0x120(%rbp), %rsi
movq %r15, %rdi
movl %r12d, %edx
callq 0x1440
movq %r14, %rdi
movl %ebx, %esi
callq 0x13eb
movl %eax, %ebx
movl %eax, 0x2578(%rip)      # 0x41c0
movq %r15, %rdi
movl %r12d, %esi
callq 0x13eb
movl %eax, 0x256b(%rip)      # 0x41c4
cmpl $0x15, %eax
jle 0x1c89
leaq 0x5e8(%rip), %rdi       # 0x224d
jmp 0x1ca8
leaq 0x5f6(%rip), %rdi       # 0x2264
callq 0x1060
movq 0x251e(%rip), %rdi      # 0x4198
callq 0x1070
cmpl $-0x1, %eax
jne 0x1cad
callq 0x1218
cmpl %eax, %ebx
jne 0x1c96
leaq 0x584(%rip), %rdi       # 0x2218
jmp 0x1ca8
leaq 0x557(%rip), %rax       # 0x21f4
leaq 0x531(%rip), %rdi       # 0x21d5
cmovlq %rax, %rdi
callq 0x1060
movq 0x24e4(%rip), %rdi      # 0x4198
callq 0x1070
cmpl $-0x1, %eax
je 0x1cda
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1230
callq 0x11e8
callq 0x1200
callq 0x11d0