// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movw $0x1000, %ax            # imm = 0x1000
movw %ax, -0x34(%rbp)
movl $0xffff0000, -0x38(%rbp) # imm = 0xFFFF0000
movw %ax, -0x2c(%rbp)
movl $0x10000, -0x30(%rbp)   # imm = 0x10000
movl $0x2c, %esi
movl $0xeac4, %edi           # imm = 0xEAC4
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1150
movl %eax, 0x3ad5(%rip)      # 0x51a0
cmpl $-0x1, %eax
je 0x16e4
leaq 0x1bac(%rip), %rdi      # 0x3283
callq 0x1080
movl 0x3abe(%rip), %edi      # 0x51a0
jmp 0x1712
leaq 0x1bbb(%rip), %rdi      # 0x32a6
callq 0x1080
movl $0x2c, %esi
movl $0xeac4, %edi           # imm = 0xEAC4
xorl %edx, %edx
callq 0x1150
movl %eax, %edi
movl %eax, 0x3a97(%rip)      # 0x51a0
cmpl $-0x1, %eax
je 0x18b3
xorl %esi, %esi
xorl %edx, %edx
callq 0x1140
testq %rax, %rax
je 0x18ae
movq %rax, %rbx
movl $0xeac5, %edi           # imm = 0xEAC5
movl $0x1, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1030
movl %eax, 0x3a63(%rip)      # 0x51a4
cmpl $-0x1, %eax
je 0x1754
leaq 0x1b89(%rip), %rdi      # 0x32d6
callq 0x1080
jmp 0x177d
leaq 0x1b99(%rip), %rdi      # 0x32f4
callq 0x1080
movl $0xeac5, %edi           # imm = 0xEAC5
xorl %esi, %esi
xorl %edx, %edx
callq 0x1030
movl %eax, 0x3a30(%rip)      # 0x51a4
cmpl $-0x1, %eax
je 0x18b8
leaq -0x210(%rip), %rsi      # 0x1574
movl $0x2, %edi
callq 0x10c0
movl $0x0, (%rbx)
movl 0x3a0a(%rip), %edi      # 0x51a4
movl $0x1, %ecx
xorl %esi, %esi
movl $0x10, %edx
xorl %eax, %eax
callq 0x1130
callq 0x15c6
movq %rbx, %r14
addq $0xc, %r14
movl $0x1, %r15d
leaq 0x18b7(%rip), %r12      # 0x307d
leaq -0x80(%rbp), %r13
cmpl $0x0, (%rbx)
je 0x1874
movl $0x5, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x1060
testl %eax, %eax
jne 0x1874
movl 0x39b3(%rip), %edi      # 0x51a4
movl $0x1, %edx
leaq -0x38(%rbp), %rsi
callq 0x1160
cmpl $-0x1, %eax
je 0x18a9
leaq 0x187a(%rip), %rdi      # 0x3089
movq %r14, %rsi
xorl %eax, %eax
callq 0x10a0
movslq %r15d, %r15
leaq -0x60(,%r15,8), %rdi
addq %rbp, %rdi
xorl %esi, %esi
leaq 0x8d(%rip), %rdx        # 0x18bd
movq %r15, %rcx
callq 0x1100
movq %r13, %rdi
leaq 0x181b(%rip), %rsi      # 0x305d
movl %r15d, %edx
xorl %eax, %eax
callq 0x1170
movq %r14, %rdi
movq %r13, %rsi
callq 0x1070
movl $0x0, (%rbx)
incl %r15d
movl 0x393e(%rip), %edi      # 0x51a4
movl $0x1, %edx
leaq -0x30(%rbp), %rsi
callq 0x1160
cmpl $0x4, %r15d
jne 0x17ca
movl $0x1, %ebx
movq -0x60(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x1190
incq %rbx
cmpq $0x4, %rbx
jne 0x1883
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11e0
callq 0x1210
callq 0x1228
callq 0x11f8