// File: /workspace/github_c_makefile_star2_O0/PARKINHYO[P]BlackjackGame/shm/shm_server.c
// Function: main
int main(void)
{
  /*딜러스레드를 만들 스레드변수*/
  pthread_t threads[MAX_PLAYERS];
  /* Shared Memory */
  key_t keyShm;       /* Shared Memory Key */
  key_t keySem;       /* Semapore Key */
  _ST_SHM *pstShm1;      /* 공용 메모리 구조체 */
  int i;
  int count = 1;
  /*키캆들 정의*/
  keyShm = (key_t)60100;
  keySem = (key_t)60101;
  char buffer[BUFFER_SIZE];
  union semun sem_union;
  struct sembuf mysem_open  = {0, -1, SEM_UNDO}; // 세마포어 얻기
  struct sembuf mysem_close = {0, 1, SEM_UNDO};  // 세마포어 돌려주기
  /******************************************
   * 공유 메모리 사용
   ******************************************/
  /* 공유 메모리 세그먼트를 연다 - 필요하면 만든다. */
  if( (gnShmID1 = shmget( keyShm, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666 )) == -1 )
  {
          printf("Shared memory segment exist - opening as client\n");
          /* Segment probably already exists - try as a client */
          if( (gnShmID1 = shmget( keyShm, SEGSIZE, 0 )) == -1 )
          {
                  perror("shmget");
                  exit(1);
          }
  }
  else
  {
          printf("Creating new shared memory segment\n");
  }

  /* 현재 프로세스에 공유 메모리 세그먼트를 연결한다. */
  if( (pstShm1 = (_ST_SHM *)shmat(gnShmID1, 0, 0)) == NULL )
  {
          perror("shmat");
          exit(1);
  }

  /* 공유 Semapore 세그먼트를 연다 - 필요하면 만든다. */
  if( (gnSemID1 = semget( keySem, 1, IPC_CREAT | IPC_EXCL | 0666 )) == -1 )
  {
    printf("Semapore segment exist - opening as client\n");
    /* Segment probably already exists - try as a client */
    if( (gnSemID1 = semget( keySem, 0, 0 )) == -1 )
    {
      perror("semget");
      exit(1);
    }
  }
  else
  {
    printf("Creating new semapore segment\n");
  }

  /* Signal 등록 */
  (void) signal (SIGINT, (void (*)()) set_shutdown);

  /* 공유 메모리 초기화 */
  pstShm1->check = 0;
  /* Semapore 초기화 */
  sem_union.val = 1;
  semctl( gnSemID1, 0, SETVAL, sem_union );
  /*카드 초기화*/
  init_cards();

  while(1) {
     /*클라이언트가 접속해서 정보 입력*/
     if(pstShm1->check) {
        /*"start"를 전송받으면*/
        if(strncmp(pstShm1->data, "start", 5) == 0){
          /*임계영역*/
          if( semop(gnSemID1, &mysem_open, 1) == -1 )
          {
                 perror("semop");
                 exit(1);
          }
          printf("start! : %s\n", pstShm1->data);
          /*딜러를 의미하는 play_game_one 스레드 생성*/
          pthread_create(&threads[count], NULL, play_game_one, (void *)(intptr_t) count);

          sprintf(buffer, "%d", count);

          strcpy(pstShm1->data, buffer);

          pstShm1->check = 0;
          count+=1;
          semop(gnSemID1, &mysem_close, 1);
          /*임계영역*/
      }
    }
  if(count==4){break;}
  }
  for (i = 1;i<count; ++i)
    pthread_join(threads[i], NULL);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl $0x1, -0x48(%rbp)
movl $0xeac4, -0x34(%rbp)    # imm = 0xEAC4
movl $0xeac5, -0x38(%rbp)    # imm = 0xEAC5
movl 0x1c31(%rip), %eax      # 0x335e
movl %eax, -0x7e(%rbp)
movw 0x1c2b(%rip), %ax       # 0x3362
movw %ax, -0x7a(%rbp)
movl 0x1c23(%rip), %eax      # 0x3364
movl %eax, -0x84(%rbp)
movw 0x1c1a(%rip), %ax       # 0x3368
movw %ax, -0x80(%rbp)
movl -0x34(%rbp), %edi
movl $0x2c, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1140
movl %eax, 0x3a26(%rip)      # 0x5190
cmpl $-0x1, %eax
jne 0x17af
leaq 0x1979(%rip), %rdi      # 0x30ef
movb $0x0, %al
callq 0x1080
movl -0x34(%rbp), %edi
movl $0x2c, %esi
xorl %edx, %edx
callq 0x1140
movl %eax, 0x39fe(%rip)      # 0x5190
cmpl $-0x1, %eax
jne 0x17ad
leaq 0x1982(%rip), %rdi      # 0x3120
callq 0x1110
movl $0x1, %edi
callq 0x1170
jmp 0x17bd
leaq 0x1971(%rip), %rdi      # 0x3127
movb $0x0, %al
callq 0x1080
movl 0x39cd(%rip), %edi      # 0x5190
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1130
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x17ee
leaq 0x196c(%rip), %rdi      # 0x314b
callq 0x1110
movl $0x1, %edi
callq 0x1170
movl -0x38(%rbp), %edi
movl $0x1, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1030
movl %eax, 0x398e(%rip)      # 0x5194
cmpl $-0x1, %eax
jne 0x1848
leaq 0x193f(%rip), %rdi      # 0x3151
movb $0x0, %al
callq 0x1080
movl -0x38(%rbp), %edi
xorl %edx, %edx
movl %edx, %esi
callq 0x1030
movl %eax, 0x3969(%rip)      # 0x5194
cmpl $-0x1, %eax
jne 0x1846
leaq 0x1946(%rip), %rdi      # 0x317d
callq 0x1110
movl $0x1, %edi
callq 0x1170
jmp 0x1856
leaq 0x1935(%rip), %rdi      # 0x3184
movb $0x0, %al
callq 0x1080
movl $0x2, %edi
leaq -0x2f2(%rip), %rsi      # 0x1570
callq 0x10b0
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
movl $0x1, -0x78(%rbp)
movl 0x3916(%rip), %edi      # 0x5194
movq -0x78(%rbp), %rcx
xorl %esi, %esi
movl $0x10, %edx
movb $0x0, %al
callq 0x1120
callq 0x15d0
movq -0x40(%rbp), %rax
cmpl $0x0, (%rax)
je 0x197e
movq -0x40(%rbp), %rdi
addq $0xc, %rdi
leaq 0x18f2(%rip), %rsi      # 0x31a3
movl $0x5, %edx
callq 0x1050
cmpl $0x0, %eax
jne 0x197c
movl 0x38ca(%rip), %edi      # 0x5194
leaq -0x7e(%rbp), %rsi
movl $0x1, %edx
callq 0x1150
cmpl $-0x1, %eax
jne 0x18f3
leaq 0x18c5(%rip), %rdi      # 0x31a9
callq 0x1110
movl $0x1, %edi
callq 0x1170
movq -0x40(%rbp), %rsi
addq $0xc, %rsi
leaq 0x18ad(%rip), %rdi      # 0x31af
movb $0x0, %al
callq 0x1080
movslq -0x48(%rbp), %rax
leaq -0x30(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movslq -0x48(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq 0xa9(%rip), %rdx        # 0x19d0
callq 0x10f0
leaq -0x70(%rbp), %rdi
movl -0x48(%rbp), %edx
leaq 0x1723(%rip), %rsi      # 0x305d
movb $0x0, %al
callq 0x1160
movq -0x40(%rbp), %rdi
addq $0xc, %rdi
leaq -0x70(%rbp), %rsi
callq 0x1060
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl 0x3829(%rip), %edi      # 0x5194
leaq -0x84(%rbp), %rsi
movl $0x1, %edx
callq 0x1150
jmp 0x197e
cmpl $0x4, -0x48(%rbp)
jne 0x1986
jmp 0x198b
jmp 0x1895
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x19b7
movslq -0x44(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1180
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1992
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)