// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl $0x1, -0x38(%rbp)
movl $0xeac5, -0x34(%rbp)    # imm = 0xEAC5
movl 0x1e2c(%rip), %eax      # 0x3372
movl %eax, -0x6e(%rbp)
movw 0x1e26(%rip), %ax       # 0x3376
movw %ax, -0x6a(%rbp)
movl 0x1e1e(%rip), %eax      # 0x3378
movl %eax, -0x74(%rbp)
movw 0x1e18(%rip), %ax       # 0x337c
movw %ax, -0x70(%rbp)
movl -0x34(%rbp), %edi
movl $0x1, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1030
movl %eax, 0x3c14(%rip)      # 0x5194
cmpl $-0x1, %eax
jne 0x15b4
movl -0x34(%rbp), %edi
xorl %edx, %edx
movl %edx, %esi
callq 0x1030
movl %eax, 0x3bfd(%rip)      # 0x5194
cmpl $-0x1, %eax
jne 0x15b2
leaq 0x1ac6(%rip), %rdi      # 0x3069
callq 0x1120
movl $0x1, %edi
callq 0x1160
jmp 0x15b6
jmp 0x15b6
movl $0x2, %edi
leaq 0xb3e(%rip), %rsi       # 0x2100
callq 0x10a0
movl $0x1, -0x68(%rbp)
movl 0x3bc0(%rip), %edi      # 0x5194
movq -0x68(%rbp), %rcx
xorl %esi, %esi
movl $0x10, %edx
movb $0x0, %al
callq 0x1130
callq 0x2140
leaq 0x1a7e(%rip), %rdi      # 0x3070
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
leaq 0x1a6d(%rip), %rdi      # 0x3070
movl $0x2, %esi
movb $0x0, %al
callq 0x1110
movl %eax, 0x3b83(%rip)      # 0x5198
cmpl $-0x1, %eax
jne 0x1632
leaq 0x1a5d(%rip), %rdi      # 0x307e
movb $0x0, %al
callq 0x1050
movl $0x1, %edi
callq 0x1160
leaq 0x1a57(%rip), %rdi      # 0x3090
movb $0x0, %al
callq 0x1050
leaq 0x3b59(%rip), %rdi      # 0x51a0
xorl %esi, %esi
movl $0x20, %edx
callq 0x1060
movl 0x3b3f(%rip), %edi      # 0x5198
leaq 0x3b40(%rip), %rsi      # 0x51a0
movl $0x20, %edx
callq 0x1070
cmpq $-0x1, %rax
jne 0x1675
jmp 0x182d
leaq 0x3b24(%rip), %r8       # 0x51a0
leaq 0x1a1d(%rip), %rsi      # 0x30a0
leaq 0x3b36(%rip), %rdx      # 0x51c0
leaq 0x3b33(%rip), %rcx      # 0x51c4
movq %r8, %rdi
movb $0x0, %al
callq 0x10f0
cmpl $0x3, 0x3b1e(%rip)      # 0x51c0
jne 0x1828
movl 0x3b16(%rip), %esi      # 0x51c4
leaq 0x19f4(%rip), %rdi      # 0x30a9
movb $0x0, %al
callq 0x1050
movl 0x3ace(%rip), %eax      # 0x5190
addl $0x1, %eax
movl %eax, 0x3ac5(%rip)      # 0x5190
cmpl $0x3, %eax
jle 0x16e3
leaq 0x19f1(%rip), %rdi      # 0x30c8
movb $0x0, %al
callq 0x1050
jmp 0x182d
movl 0x3adb(%rip), %edx      # 0x51c4
movl 0x3aa1(%rip), %eax      # 0x5190
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x3acc(%rip), %rax      # 0x51c8
movl %edx, (%rax,%rcx,4)
movl 0x3abf(%rip), %ecx      # 0x51c4
leaq 0x3ad4(%rip), %rdi      # 0x51e0
leaq 0x19d2(%rip), %rsi      # 0x30e5
leaq 0x19d0(%rip), %rdx      # 0x30ea
movb $0x0, %al
callq 0x1150
leaq 0x3ab8(%rip), %rdi      # 0x51e0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
movl 0x3a8c(%rip), %ecx      # 0x51c4
leaq 0x3ac1(%rip), %rdi      # 0x5200
leaq 0x199f(%rip), %rsi      # 0x30e5
leaq 0x1923(%rip), %rdx      # 0x3070
movb $0x0, %al
callq 0x1150
leaq 0x3aa5(%rip), %rdi      # 0x5200
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
leaq 0x3a74(%rip), %rdi      # 0x51e0
movl $0x2, %esi
movb $0x0, %al
callq 0x1110
movl %eax, %edx
movl 0x3a10(%rip), %eax      # 0x5190
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x3a93(%rip), %rax      # 0x5220
movl %edx, (%rax,%rcx,4)
movl 0x39fe(%rip), %edi      # 0x5194
leaq -0x6e(%rbp), %rsi
movl $0x1, %edx
callq 0x1140
cmpl $-0x1, %eax
jne 0x17bf
leaq 0x1947(%rip), %rdi      # 0x30f7
callq 0x1120
movl $0x1, %edi
callq 0x1160
movslq -0x38(%rbp), %rax
leaq -0x30(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movslq 0x39bb(%rip), %rcx      # 0x5190
xorl %eax, %eax
movl %eax, %esi
leaq 0x90(%rip), %rdx        # 0x1870
callq 0x1100
movl -0x38(%rbp), %esi
leaq 0x190e(%rip), %rdi      # 0x30fd
movb $0x0, %al
callq 0x1050
leaq -0x60(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq 0x1859(%rip), %rsi      # 0x305d
movb $0x0, %al
callq 0x1150
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl 0x397a(%rip), %edi      # 0x5194
leaq -0x74(%rbp), %rsi
movl $0x1, %edx
callq 0x1140
jmp 0x1640
movl $0x1, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x1859
movslq -0x78(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1170
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x1834
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)