// File: /workspace/github_c_makefile_star2_O3/PARKINHYO[P]BlackjackGame/shm/shm_server.c
// Function: play_game_one
void* play_game_one(void *data)
{
  int gnShmID2;      /* Shared Memory Indicator */
  int gnSemID2;      /* Semapore Indicator */
  int id = (intptr_t)data;
  int shmId = (intptr_t)data + 60100;
  int semId = (intptr_t)data+1 +60100;
  char buffer[BUFFER_SIZE];
  int nwritten;
  int player_sum, dealer_sum;
  int *player_hand_values = players_hand_values[id], *dealer_hand_values = dealers_hand_values[id];
  int *player_hand_suits = players_hand_suits[id], *dealer_hand_suits = dealers_hand_suits[id];
  int i;

  /* Shared Memory */
  key_t keyShm;       /* Shared Memory Key */
  _ST_SHM *shared_data;      /* 공용 메모리 구조체 */

  /* Semapore */
  key_t keySem;       /* Semapore Key */
  union semun sem_union;
  struct sembuf mysem_open  = {0, -1, SEM_UNDO}; // 세마포어 얻기
  struct sembuf mysem_close = {0, 1, SEM_UNDO};  // 세마포어 돌려주기

  /* Shared Memory의 키값을 생성한다. */
  keyShm = (key_t)shmId;
  /* Semapore 키값을 생성한다. */
  keySem = (key_t)semId;
  /* 공유 메모리 세그먼트를 연다 - 필요하면 만든다. */
  if((gnShmID2 = shmget( keyShm, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666 )) == -1)
  {
    printf("Shared memory segment exist - opening as client\n");
    /* 세그먼트가 이미 존재한다면 - try as a client */
    if( (gnShmID2 = shmget( keyShm, SEGSIZE, 0 )) == -1 )
    {
      perror("shmget");
      exit(1);
          }
  }
  else
  {
    printf("Creating new shared memory segment\n");
  }
  /* 현재 프로세스에 공유 메모리 세그먼트를 연결한다. */
  if((shared_data = (_ST_SHM *)shmat(gnShmID2, 0, 0)) == NULL)
  {
    perror("shmat");
    exit(1);
  }
  /* 공유 Semapore 세그먼트를 연다 - 필요하면 만든다. */
  if((gnSemID2 = semget( keySem, 1, IPC_CREAT | IPC_EXCL | 0666 )) == -1)
  {
    printf("Semapore segment exist - opening as client\n");
    /* 세그먼트가 이미 존재한다면 - try as a client */
    if( (gnSemID2 = semget( keySem, 0, 0 )) == -1 )
    {
      perror("semget");
      exit(1);
    }
  }
  else
  {
    printf("Creating new semapore segment\n");
  }

  /* 공유 메모리 초기화 */
  shared_data->check = 0;
  shared_data->check2 = 0;
  shared_data->finalcheck=0;
  /* Semapore 초기화 */
  sem_union.val = 1;
  semctl( gnSemID2, 0, SETVAL, sem_union );

  /*첫 카드 뽑기*/
  nplayers[id] = 0;
	ndealers[id] = 0;
  /*딜러들의 카드덱은 공유 - mutex로 raceCondition 제어*/
  pthread_mutex_lock(&card_mutex);
	{
		for (i = 0; i < 2; ++i)
		{
			player_hand_values[nplayers[id]] = card_values[ncard];
			player_hand_suits[nplayers[id]] = card_suits[ncard];
			++ncard;
			++nplayers[id];
		}

		dealer_hand_values[ndealers[id]] = card_values[ncard];
		dealer_hand_suits[ndealers[id]] = card_suits[ncard];
		++ncard;
		++ndealers[id];
	}
	pthread_mutex_unlock(&card_mutex);

  buffer[0] = value_codes[player_hand_values[0]];
	buffer[1] = suit_codes[player_hand_suits[0]];
	buffer[2] = value_codes[player_hand_values[1]];
	buffer[3] = suit_codes[player_hand_suits[1]];
	buffer[4] = value_codes[dealer_hand_values[0]];
	buffer[5] = suit_codes[dealer_hand_suits[0]];
	buffer[6] = 0;

  /******* 임계영역 *******/
  if( semop(gnSemID2, &mysem_open, 1) == -1 )
	{
		perror("semop");
		exit(1);
	}
  /* shared memory 전송 */
  strncpy(shared_data->data, buffer, BUFFER_SIZE);
	printf("send: first cardset\n");
  shared_data->check = 1;
  semop(gnSemID2, &mysem_close, 1);
  /******* 임계영역 *******/

  while(shared_data->check) {
     sleep(1);
     printf("waiting...\n");
   }
  /* 첫 카드를 주고서*/
  printf("\n");
  printf("Player %d Hand: ", id );
  display_state(player_hand_values, player_hand_suits, nplayers[id]);
  printf("Dealer Hand with player %d: ", id);
  display_state(dealer_hand_values, dealer_hand_suits, ndealers[id]);

  while(TRUE){
    if(!shared_data->check2){
      if( semop(gnSemID2, &mysem_open, 1) == -1 ) // 대기상태
      {
        perror("semop");
        exit(1);
      }
    }
    /* HIT, STAND signal 빋기*/
    if(!shared_data->check2)
    {
      strncpy(buffer, shared_data->data, BUFFER_SIZE);
      shared_data->check2=1;
      printf("I received from player %d: %s\n", id, buffer);
      strncpy(shared_data->data, "\0", BUFFER_SIZE);

      /* HIT 결과 전송 */

      if (strcmp(buffer, HIT) == 0)
      {
        pthread_mutex_lock(&card_mutex);
        {
          player_hand_values[nplayers[id]] = card_values[ncard];
          player_hand_suits[nplayers[id]] = card_suits[ncard];

          buffer[0] = value_codes[player_hand_values[nplayers[id]]];
          buffer[1] = suit_codes[player_hand_suits[nplayers[id]]];
          buffer[2] = 0;

          ++ncard;
          ++nplayers[id];
        }
        pthread_mutex_unlock(&card_mutex);

        strncpy(shared_data->data, buffer, BUFFER_SIZE);
        printf("I send to player %d: %s\n", id, buffer);
        shared_data->check=2;
        buffer[0] = '\0';
        semop(gnSemID2, &mysem_close, 1);

        /* 플레이어 결과가 21이 넘을 경우*/
        if (calc_sum(player_hand_values, nplayers[id]) > 21)
        {
          printf("Player %d busted. Dealer wins.\n", id);
          break;
        }
        /* 플레이어 결과가 21일 경우 이기기 때문에 break */
        else if (calc_sum(player_hand_values, nplayers[id]) == 21)
          break;
      }
      /* 플레이어의 signal이 STAND인 경우 stop*/
      else if (strcmp(buffer, STAND) == 0){
        //shared_data->finalcheck=1;
        shared_data->check=0;
        semop(gnSemID2, &mysem_close, 1);
        break;
      }
    }
  }
  i=0;
  while (calc_sum(dealer_hand_values, ndealers[id]) < 17)
  {
    dealer_hand_values[ndealers[id]] = card_values[ncard];
    dealer_hand_suits[ndealers[id]] = card_suits[ncard];
    buffer[i++] = value_codes[dealer_hand_values[ndealers[id]]];
    buffer[i++] = suit_codes[dealer_hand_suits[ndealers[id]]];
    ++ncard;
    ++ndealers[id];
  }
  buffer[i] = 0;

  printf("\n");
  printf("Player %d Hand: ", id);
  display_state(player_hand_values, player_hand_suits, nplayers[id]);
  printf("Dealer %d Hand: ", id);
  display_state(dealer_hand_values, dealer_hand_suits, ndealers[id]);

  if( semop(gnSemID2, &mysem_open, 1) == -1 )
  {
          perror("semop");
          exit(1);
  }
  strncpy(shared_data->data, buffer, BUFFER_SIZE);
  shared_data->finalcheck=1;
  semop(gnSemID2, &mysem_close, 1);

  player_sum = calc_sum(player_hand_values, nplayers[id]);
  dealer_sum = calc_sum(dealer_hand_values, ndealers[id]);

  if (dealer_sum > 21)
    printf("\nDealer busted! Player %d wins!\n", id);
  else if (player_sum == dealer_sum)
    printf("\nPlayer %d and dealer have the same score. It's a push!\n", id);
  else if (player_sum < dealer_sum)
    printf("\nDealer has a higher score than player %d. Dealer wins!\n", id);
  else
    printf("\nPlayer %d has a higher score. Player wins!\n", id);


    /* 세마포어, shared memory 제거*/
  if(shmdt(shared_data) == -1) {
     perror("shmdt failed");
     exit(1);
  }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r15
leal 0xeac4(%rdi), %ebx
movw $0x1000, %ax            # imm = 0x1000
movw %ax, -0x44(%rbp)
movl $0xffff0000, -0x48(%rbp) # imm = 0xFFFF0000
movw %ax, -0x30(%rbp)
movl $0x10000, -0x34(%rbp)   # imm = 0x10000
movl $0x2c, %esi
movl %ebx, %edi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1150
cmpl $-0x1, %eax
je 0x191b
movl %eax, %r14d
leaq 0x196f(%rip), %rdi      # 0x3283
callq 0x1080
jmp 0x1941
leaq 0x1984(%rip), %rdi      # 0x32a6
callq 0x1080
movl $0x2c, %esi
movl %ebx, %edi
xorl %edx, %edx
callq 0x1150
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x2058
movl %r14d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1140
testq %rax, %rax
je 0x2044
movq %rax, %r14
leal 0xeac5(%r15), %ebx
movl %ebx, %edi
movl $0x1, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1030
cmpl $-0x1, %eax
je 0x1987
movl %eax, %r13d
leaq 0x1956(%rip), %rdi      # 0x32d6
callq 0x1080
jmp 0x19aa
leaq 0x1966(%rip), %rdi      # 0x32f4
callq 0x1080
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1030
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x205d
movq %r15, -0x40(%rbp)
movslq %r15d, %r15
movq %r15, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %r12
leaq 0x399d(%rip), %rax      # 0x5360
addq %r12, %rax
movq %rax, -0x58(%rbp)
leaq 0x3acf(%rip), %rax      # 0x54a0
addq %r12, %rax
movq %rax, -0x50(%rbp)
leaq 0x3c01(%rip), %rax      # 0x55e0
addq %r12, %rax
movq %rax, -0x88(%rbp)
leaq 0x3d30(%rip), %rax      # 0x5720
addq %rax, %r12
movq $0x0, (%r14)
xorl %ebx, %ebx
movl %ebx, 0x8(%r14)
movl $0x1, %ecx
movl %r13d, %edi
movl %r13d, -0x2c(%rbp)
xorl %esi, %esi
movl $0x10, %edx
xorl %eax, %eax
callq 0x1130
leaq 0x3e3f(%rip), %r13      # 0x5860
movl %ebx, (%r13,%r15,4)
leaq 0x3e43(%rip), %rax      # 0x5870
movl %ebx, (%rax,%r15,4)
movq %rax, %rbx
leaq 0x3e45(%rip), %rdi      # 0x5880
callq 0x11b0
movslq 0x3909(%rip), %rax      # 0x5350
movslq (%r13,%r15,4), %rcx
leaq (%r15,%r15,4), %rdx
shlq $0x4, %rdx
movq %rdx, -0x98(%rbp)
leaq (%rdx,%rcx,4), %rdx
leaq 0x374a(%rip), %rdi      # 0x51b0
movq (%rdi,%rax,4), %rsi
leaq 0x38ef(%rip), %r8       # 0x5360
movq %rsi, (%r8,%rdx)
leaq 0x3804(%rip), %r8       # 0x5280
movq (%r8,%rax,4), %rsi
leaq 0x3b59(%rip), %r9       # 0x55e0
movq %rsi, (%r9,%rdx)
leal 0x2(%rax), %edx
leal 0x2(%rcx), %ecx
movl %ecx, (%r13,%r15,4)
movslq %edx, %rcx
movl (%rdi,%rcx,4), %edx
movslq (%rbx,%r15,4), %rsi
movq -0x50(%rbp), %r13
movl %edx, (%r13,%rsi,4)
movl (%r8,%rcx,4), %ecx
movl %ecx, (%r12,%rsi,4)
addl $0x3, %eax
movl %eax, 0x3896(%rip)      # 0x5350
leal 0x1(%rsi), %eax
movq %r15, -0x60(%rbp)
movl %eax, (%rbx,%r15,4)
leaq 0x3db4(%rip), %rdi      # 0x5880
callq 0x10f0
movq -0x58(%rbp), %rsi
movslq (%rsi), %rax
leaq 0x1863(%rip), %rcx      # 0x3342
movb (%rax,%rcx), %al
movb %al, -0x80(%rbp)
movq -0x88(%rbp), %rdi
movslq (%rdi), %rax
leaq 0x1848(%rip), %rdx      # 0x333e
movb (%rax,%rdx), %al
movb %al, -0x7f(%rbp)
movslq 0x4(%rsi), %rax
movb (%rax,%rcx), %al
movb %al, -0x7e(%rbp)
movslq 0x4(%rdi), %rax
movb (%rax,%rdx), %al
movb %al, -0x7d(%rbp)
movslq (%r13), %rax
movb (%rax,%rcx), %al
movb %al, -0x7c(%rbp)
movq %r12, -0xa0(%rbp)
movslq (%r12), %rax
movb (%rax,%rdx), %al
movb %al, -0x7b(%rbp)
movb $0x0, -0x7a(%rbp)
leaq -0x48(%rbp), %rsi
movl $0x1, %edx
movl -0x2c(%rbp), %ebx
movl %ebx, %edi
callq 0x1160
cmpl $-0x1, %eax
je 0x2049
movq %r14, %r13
addq $0xc, %r13
leaq -0x80(%rbp), %rsi
movl $0x20, %edx
movq %r13, %rdi
callq 0x1050
leaq 0x17b5(%rip), %rdi      # 0x331f
callq 0x1080
movl $0x1, (%r14)
leaq -0x34(%rbp), %rsi
movl $0x1, %edx
movl %ebx, %edi
callq 0x1160
cmpl $0x0, (%r14)
je 0x1bab
leaq 0x17a0(%rip), %rbx      # 0x3333
movl $0x1, %edi
callq 0x11a0
movq %rbx, %rdi
callq 0x1080
cmpl $0x0, (%r14)
jne 0x1b93
movl $0xa, %edi
callq 0x1040
leaq 0x14da(%rip), %rdi      # 0x3096
movq %r14, -0x90(%rbp)
movq -0x40(%rbp), %r14
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
movq -0x60(%rbp), %rbx
leaq 0x3c84(%rip), %r15      # 0x5860
movl (%r15,%rbx,4), %edx
movq -0x58(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x14a0
leaq 0x14b0(%rip), %rdi      # 0x30a7
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x3c68(%rip), %rax      # 0x5870
movl (%rax,%rbx,4), %edx
movq %r14, %rbx
movq -0x90(%rbp), %r14
movq -0x50(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x14a0
cmpl $0x0, 0x4(%r14)
jne 0x1c25
movl $0x1, %edx
movl -0x2c(%rbp), %edi
leaq -0x48(%rbp), %rsi
callq 0x1160
cmpl $-0x1, %eax
je 0x203f
cmpl $0x0, 0x4(%r14)
jne 0x1c25
movq %r15, %r12
movl $0x20, %edx
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x1050
movl $0x1, 0x4(%r14)
leaq 0x1451(%rip), %rdi      # 0x30c4
movl %ebx, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10a0
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movups %xmm0, 0x10(%r13)
cmpl $0x544948, -0x80(%rbp)  # imm = 0x544948
jne 0x1da2
leaq 0x3be0(%rip), %r12      # 0x5880
movq %r12, %rdi
callq 0x11b0
movslq 0x36a1(%rip), %rax      # 0x5350
leaq 0x34fa(%rip), %rcx      # 0x51b0
movslq (%rcx,%rax,4), %rcx
movq -0x60(%rbp), %r15
leaq 0x3b9b(%rip), %rdx      # 0x5860
movslq (%rdx,%r15,4), %rdx
movq %rbx, %r14
movq -0x58(%rbp), %rsi
movl %ecx, (%rsi,%rdx,4)
leaq 0x35a6(%rip), %rsi      # 0x5280
movslq (%rsi,%rax,4), %rsi
movq -0x88(%rbp), %rdi
movl %esi, (%rdi,%rdx,4)
leaq 0x1653(%rip), %rdi      # 0x3342
movb (%rcx,%rdi), %cl
movb %cl, -0x80(%rbp)
leaq 0x1642(%rip), %rcx      # 0x333e
movb (%rsi,%rcx), %cl
movb %cl, -0x7f(%rbp)
xorl %ecx, %ecx
movb %cl, -0x7e(%rbp)
incl %eax
movl %eax, 0x3641(%rip)      # 0x5350
leal 0x1(%rdx), %eax
leaq 0x3b47(%rip), %rcx      # 0x5860
movl %eax, (%rcx,%r15,4)
movq %r12, %rdi
callq 0x10f0
movl $0x20, %edx
movq %r13, %r12
movq %r13, %rdi
leaq -0x80(%rbp), %r13
movq %r13, %rsi
callq 0x1050
leaq 0x13a0(%rip), %rdi      # 0x30e3
movl %r14d, %esi
movq -0x90(%rbp), %r14
movq %r13, %rdx
xorl %eax, %eax
callq 0x10a0
movl $0x2, (%r14)
xorl %eax, %eax
movb %al, -0x80(%rbp)
movl $0x1, %edx
movl -0x2c(%rbp), %edi
leaq -0x34(%rbp), %rsi
callq 0x1160
leaq 0x3ae5(%rip), %rax      # 0x5860
movl (%rax,%r15,4), %esi
leaq 0x3ada(%rip), %r15      # 0x5860
movq -0x58(%rbp), %rdi
callq 0x144b
cmpl $0x16, %eax
jge 0x1dd8
cmpl $0x15, %eax
movq %r12, %r13
jne 0x1c25
jmp 0x1deb
movl -0x80(%rbp), %eax
movl $0x4e415453, %ecx       # imm = 0x4E415453
xorl %ecx, %eax
movzwl -0x7c(%rbp), %ecx
xorl $0x44, %ecx
orl %eax, %ecx
movq %r12, %r15
jne 0x1c25
movl $0x0, (%r14)
leaq -0x34(%rbp), %rsi
movl $0x1, %edx
movl -0x2c(%rbp), %edi
callq 0x1160
jmp 0x1deb
leaq 0x131d(%rip), %rdi      # 0x30fc
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
movq %r12, %r13
movq %r13, -0xa8(%rbp)
movq -0x60(%rbp), %r13
leaq 0x3a73(%rip), %rbx      # 0x5870
movl (%rbx,%r13,4), %esi
movq -0x50(%rbp), %rdi
movq %rsi, %r14
callq 0x144b
cmpl $0x10, %eax
jg 0x1f09
movslq %r14d, %rax
movslq 0x3530(%rip), %r15      # 0x5350
leal 0x1(%r15), %ecx
movq %rcx, -0xb8(%rbp)
shlq $0x2, %r15
movq %r15, -0xb0(%rbp)
movq -0x98(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x98(%rbp)
incl %r14d
xorl %r13d, %r13d
leaq 0x335b(%rip), %r15      # 0x51b0
xorl %r12d, %r12d
movq -0x50(%rbp), %rbx
movq -0xb0(%rbp), %rax
leaq (%rax,%r13,4), %rax
movslq (%r15,%rax), %rcx
movq -0x98(%rbp), %rdx
leaq (%rdx,%r13,4), %rdx
leaq 0x3623(%rip), %rsi      # 0x54a0
movl %ecx, (%rsi,%rdx)
leaq 0x33f9(%rip), %rsi      # 0x5280
movslq (%rsi,%rax), %rax
leaq 0x388e(%rip), %rsi      # 0x5720
movl %eax, (%rsi,%rdx)
leaq 0x14a6(%rip), %rdx      # 0x3342
movb (%rcx,%rdx), %cl
movb %cl, -0x80(%rbp,%r13,2)
leaq 0x1493(%rip), %rcx      # 0x333e
movb (%rax,%rcx), %al
addq $0x2, %r12
movb %al, -0x7f(%rbp,%r13,2)
movq -0xb8(%rbp), %rax
addl %r13d, %eax
movl %eax, 0x3489(%rip)      # 0x5350
leal (%r14,%r13), %esi
movq -0x60(%rbp), %rax
leaq 0x399a(%rip), %rcx      # 0x5870
movl %esi, (%rcx,%rax,4)
movq %rbx, %rdi
callq 0x144b
incq %r13
cmpl $0x11, %eax
jl 0x1e5c
andl $-0x2, %r12d
movq -0x40(%rbp), %r14
movq -0x60(%rbp), %r13
leaq 0x3970(%rip), %rbx      # 0x5870
leaq 0x3959(%rip), %r15      # 0x5860
jmp 0x1f10
xorl %r12d, %r12d
movq -0x40(%rbp), %r14
movb $0x0, -0x80(%rbp,%r12)
movl $0xa, %edi
callq 0x1040
leaq 0x116f(%rip), %rdi      # 0x3096
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
movl (%r15,%r13,4), %edx
movq -0x58(%rbp), %r12
movq %r12, %rdi
movq -0x88(%rbp), %rsi
callq 0x14a0
leaq 0x11d3(%rip), %rdi      # 0x3122
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
movl (%rbx,%r13,4), %edx
movq -0x50(%rbp), %r15
movq %r15, %rdi
movq -0xa0(%rbp), %rsi
callq 0x14a0
leaq -0x48(%rbp), %rsi
movl $0x1, %edx
movl -0x2c(%rbp), %ebx
movl %ebx, %edi
callq 0x1160
cmpl $-0x1, %eax
je 0x204e
leaq -0x80(%rbp), %rsi
movl $0x20, %edx
movq -0xa8(%rbp), %rdi
callq 0x1050
movq -0x90(%rbp), %r14
movl $0x1, 0x8(%r14)
leaq -0x34(%rbp), %rsi
movl $0x1, %edx
movl %ebx, %edi
callq 0x1160
leaq 0x3899(%rip), %rax      # 0x5860
movl (%rax,%r13,4), %esi
movq %r12, %rdi
callq 0x144b
movl %eax, %ebx
leaq 0x3894(%rip), %rax      # 0x5870
movl (%rax,%r13,4), %esi
movq %r15, %rdi
callq 0x144b
cmpl $0x15, %eax
jle 0x1ff6
leaq 0x113f(%rip), %rdi      # 0x3133
jmp 0x2015
cmpl %eax, %ebx
jne 0x2003
leaq 0x1153(%rip), %rdi      # 0x3154
jmp 0x2015
leaq 0x1183(%rip), %rax      # 0x318d
leaq 0x11b5(%rip), %rdi      # 0x31c6
cmovlq %rax, %rdi
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x10a0
movq %r14, %rdi
callq 0x1090
cmpl $-0x1, %eax
je 0x2053
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1270
callq 0x12b8
callq 0x1288
callq 0x1258
callq 0x1240
callq 0x12d0
callq 0x12a0