// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2c
popq %rsi
movl $0xeac4, %edi           # imm = 0xEAC4
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1160
movl %eax, 0x2be8(%rip)      # 0x41a0
cmpl $-0x1, %eax
je 0x15d1
leaq 0xcbf(%rip), %rdi       # 0x2283
callq 0x1080
movl 0x2bd1(%rip), %edi      # 0x41a0
jmp 0x15fd
leaq 0xcce(%rip), %rdi       # 0x22a6
callq 0x1080
pushq $0x2c
popq %rsi
movl $0xeac4, %edi           # imm = 0xEAC4
xorl %edx, %edx
callq 0x1160
movl %eax, %edi
movl %eax, 0x2bac(%rip)      # 0x41a0
cmpl $-0x1, %eax
je 0x17aa
xorl %esi, %esi
xorl %edx, %edx
callq 0x1150
testq %rax, %rax
je 0x17a1
movq %rax, %rbx
pushq $0x1
popq %r14
movl $0xeac5, %edi           # imm = 0xEAC5
movl %r14d, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1030
movl %eax, 0x2b76(%rip)      # 0x41a4
cmpl $-0x1, %eax
je 0x1641
leaq 0xc9c(%rip), %rdi       # 0x22d6
callq 0x1080
jmp 0x166a
leaq 0xcac(%rip), %rdi       # 0x22f4
callq 0x1080
movl $0xeac5, %edi           # imm = 0xEAC5
xorl %esi, %esi
xorl %edx, %edx
callq 0x1030
movl %eax, 0x2b43(%rip)      # 0x41a4
cmpl $-0x1, %eax
je 0x17b3
leaq -0x205(%rip), %rsi      # 0x146c
pushq $0x2
popq %rdi
callq 0x10c0
andl $0x0, (%rbx)
movl 0x2b22(%rip), %edi      # 0x41a4
pushq $0x10
popq %rdx
pushq $0x1
popq %r15
xorl %esi, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x1140
callq 0x14bc
movq %rbx, %rax
addq $0xc, %rax
movq %rax, -0x40(%rbp)
pushq $0x5
popq %r12
cmpl $0x0, (%rbx)
je 0x175b
movq -0x40(%rbp), %rdi
leaq 0x9c0(%rip), %rsi       # 0x207d
movq %r12, %rdx
callq 0x1060
testl %eax, %eax
jne 0x175b
movl 0x2ad1(%rip), %edi      # 0x41a4
leaq -0x38(%rbp), %rsi
movq %r15, %rdx
callq 0x1170
cmpl $-0x1, %eax
je 0x178d
leaq 0x99a(%rip), %rdi       # 0x2089
movq -0x40(%rbp), %r12
movq %r12, %rsi
xorl %eax, %eax
callq 0x10a0
movslq %r14d, %r14
leaq -0x60(,%r14,8), %rdi
addq %rbp, %rdi
xorl %esi, %esi
leaq 0xa8(%rip), %rdx        # 0x17bc
movq %r14, %rcx
callq 0x1110
leaq -0x80(%rbp), %r13
movq %r13, %rdi
leaq 0x933(%rip), %rsi       # 0x205d
movl %r14d, %edx
xorl %eax, %eax
callq 0x1180
movq %r12, %rdi
movq %r13, %rsi
callq 0x1070
andl $0x0, (%rbx)
incl %r14d
movl 0x2a59(%rip), %edi      # 0x41a4
leaq -0x30(%rbp), %rsi
movq %r15, %rdx
callq 0x1170
pushq $0x5
popq %r12
cmpl $0x4, %r14d
jne 0x16a9
jmp 0x1776
movq -0x60(%rbp,%r15,8), %rdi
xorl %esi, %esi
callq 0x11a0
incq %r15
cmpq $0x4, %r15
jne 0x1767
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8ef(%rip), %rdi       # 0x2083
callq 0x1130
pushq $0x1
popq %rdi
callq 0x1190
leaq 0x8c8(%rip), %rdi       # 0x2070
jmp 0x1794
leaq 0x8b8(%rip), %rdi       # 0x2069
jmp 0x1794
leaq 0x8bc(%rip), %rdi       # 0x2076
jmp 0x1794