// File: /workspace/github_c_makefile_star2_O2/PARKINHYO[P]BlackjackGame/fifo/fifo_server.c
// Function: main
int main(void)
{
	int id;
	/*딜러스레드를 만들 스레드변수*/
	pthread_t threads[MAX_PLAYERS];
	key_t keySem;       /* Semapore Key */
	int count = 1;
	/*세마포어 키*/
	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 };  // 세마포어 돌려주기

	/* 공유 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);

	/* Semapore 초기화 */
	sem_union.val = 1;
	semctl(gnSemID1, 0, SETVAL, sem_union);

	/*카드 초기화*/
	init_cards();

	//파이프 -생성 
	mkfifo(FROM_CLIENT_FILE, 0666);

	//파이프 -읽기전용으로 연다
	if ((fd = open(FROM_CLIENT_FILE, O_RDWR)) == -1) {
		printf("[SERVER] open!!!\n");
		exit(1);
	}

	printf("SERVER start!\n\n");
	while (1) {
		memset(buff, 0, BUFF_SIZE);

		//클라이언트 들어오면 받아옴
		if (read(fd, buff, BUFF_SIZE) == -1) {
			break;
		}

		//cmd==3, 각 클라이언트별 pid sscanf
		sscanf(buff, "%d %d|%s", &cmd, &pid, buff);

		if (cmd == 3) {
			printf("[SERVER] client(%d) enter....\n", pid);

			//clientCounter는 들어올때마다 증가, max넘으면 출력
			if (++clientCounter > MAX_CLIENT) {
				printf("[SERVER] client count over!\n");
				break;
				continue;
			}

			//클라이언트 개수 인덱스 배열에 pid 대입
			clientName[clientCounter - 1] = pid;

			//pid 추가한 파일경로로 파이프 생성
			sprintf(filename, "%s%d", TO_CLIENT_FILE, pid);
			mkfifo(filename, 0666);

			sprintf(filename2, "%s%d", FROM_CLIENT_FILE, pid);
			mkfifo(filename2, 0666);

			//파이프 열기
			client[clientCounter - 1] = open(filename, O_RDWR);

			if (semop(gnSemID1, &mysem_open, 1) == -1)
			{
				perror("semop");
				exit(1);
			}

			//쓰레드 생성, 게임 시작, clientCounter도 전달
			pthread_create(&threads[count], NULL, play_game_one, (void*)(intptr_t)clientCounter);


			printf("[SERVER] count: %d\n", count);
			sprintf(buffer, "%d", count);

			count += 1;
			semop(gnSemID1, &mysem_close, 1);
		}
	}
	for (int ii = 1; ii < count; ++ii)
		pthread_join(threads[ii], 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 $0x1
popq %rbx
movl $0xeac5, %edi           # imm = 0xEAC5
movl %ebx, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1030
movl %eax, 0x2d1f(%rip)      # 0x4194
cmpl $-0x1, %eax
jne 0x1497
movl $0xeac5, %edi           # imm = 0xEAC5
xorl %esi, %esi
xorl %edx, %edx
callq 0x1030
movl %eax, 0x2d06(%rip)      # 0x4194
cmpl $-0x1, %eax
je 0x16ec
leaq 0x912(%rip), %rsi       # 0x1db0
pushq $0x2
popq %r14
movl %r14d, %edi
callq 0x10a0
movl 0x2ce4(%rip), %edi      # 0x4194
pushq $0x10
popq %rdx
pushq $0x1
popq %rcx
xorl %esi, %esi
xorl %eax, %eax
callq 0x1140
callq 0x1de5
leaq 0xba5(%rip), %r15       # 0x2070
movq %r15, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
movq %r15, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1120
movl %eax, 0x2cad(%rip)      # 0x4198
cmpl $-0x1, %eax
je 0x16d5
leaq 0xdd0(%rip), %rdi       # 0x22cb
callq 0x1050
leaq 0x2c99(%rip), %r15      # 0x41a0
pushq $0x20
popq %r12
pushq $0x1
popq %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x2c97(%rip)     # 0x41b0
movaps %xmm0, 0x2c80(%rip)     # 0x41a0
movl 0x2c72(%rip), %edi      # 0x4198
movq %r15, %rsi
movq %r12, %rdx
callq 0x1080
movl %ebx, %r13d
cmpq $-0x1, %rax
je 0x16bf
movq %r15, %rdi
leaq 0xb36(%rip), %rsi       # 0x207e
leaq 0x2c71(%rip), %rdx      # 0x41c0
leaq 0x2c6e(%rip), %rcx      # 0x41c4
movq %r15, %r8
xorl %eax, %eax
callq 0x10f0
cmpl $0x3, 0x2c59(%rip)      # 0x41c0
jne 0x150f
movl 0x2c55(%rip), %esi      # 0x41c4
leaq 0xb11(%rip), %rdi       # 0x2087
xorl %eax, %eax
callq 0x1070
movslq 0x2c0c(%rip), %rax      # 0x4190
leal 0x1(%rax), %ecx
movl %ecx, 0x2c03(%rip)      # 0x4190
cmpq $0x3, %rax
jge 0x16a2
movl 0x2c27(%rip), %ecx      # 0x41c4
leaq 0x2c24(%rip), %rdx      # 0x41c8
movl %ecx, (%rdx,%rax,4)
leaq 0x2c32(%rip), %rdi      # 0x41e0
leaq 0xaf1(%rip), %rsi       # 0x20a6
leaq 0xaef(%rip), %rdx       # 0x20ab
xorl %eax, %eax
callq 0x1160
leaq 0x2c16(%rip), %rdi      # 0x41e0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
movl 0x2bea(%rip), %ecx      # 0x41c4
leaq 0x2c1f(%rip), %rdi      # 0x4200
leaq 0xabe(%rip), %rsi       # 0x20a6
leaq 0xa81(%rip), %rdx       # 0x2070
xorl %eax, %eax
callq 0x1160
leaq 0x2c03(%rip), %rdi      # 0x4200
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
leaq 0x2bd2(%rip), %rdi      # 0x41e0
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x1120
movslq 0x2b71(%rip), %rcx      # 0x4190
leaq 0x2bfa(%rip), %rdx      # 0x4220
movl %eax, -0x4(%rdx,%rcx,4)
movl 0x2b64(%rip), %edi      # 0x4194
leaq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x1150
cmpl $-0x1, %eax
je 0x16e3
leaq -0x60(,%r13,8), %rdi
addq %rbp, %rdi
movslq 0x2b39(%rip), %rcx      # 0x4190
xorl %esi, %esi
leaq 0xa0(%rip), %rdx        # 0x1700
callq 0x1110
leaq 0xa52(%rip), %rdi       # 0x20be
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
leaq -0x80(%rbp), %rdi
leaq 0x9dd(%rip), %rsi       # 0x205d
movl %ebx, %edx
xorl %eax, %eax
callq 0x1160
incl %ebx
movl 0x2b03(%rip), %edi      # 0x4194
leaq -0x30(%rbp), %rsi
movq %r14, %rdx
callq 0x1150
jmp 0x150f
leaq 0xc31(%rip), %rdi       # 0x22da
callq 0x1050
jmp 0x16bf
movq -0x60(%rbp,%r14,8), %rdi
xorl %esi, %esi
callq 0x1180
incq %r14
cmpq %r13, %r14
jb 0x16b0
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc1a(%rip), %rdi       # 0x22f6
callq 0x1050
jmp 0x16f8
leaq 0x9ce(%rip), %rdi       # 0x20b8
jmp 0x16f3
leaq 0x976(%rip), %rdi       # 0x2069
callq 0x1130
pushq $0x1
popq %rdi
callq 0x1170