// File: /workspace/github_c_makefile_star2_O3/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
movl $0xeac5, %edi           # imm = 0xEAC5
movl $0x1, %esi
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x1030
movl %eax, 0x2cab(%rip)      # 0x4194
cmpl $-0x1, %eax
jne 0x150b
movl $0xeac5, %edi           # imm = 0xEAC5
xorl %esi, %esi
xorl %edx, %edx
callq 0x1030
movl %eax, 0x2c92(%rip)      # 0x4194
cmpl $-0x1, %eax
je 0x17a4
leaq 0x956(%rip), %rsi       # 0x1e68
movl $0x2, %edi
callq 0x10a0
movl 0x2c72(%rip), %edi      # 0x4194
movl $0x1, %ecx
xorl %esi, %esi
movl $0x10, %edx
xorl %eax, %eax
callq 0x1130
callq 0x1e9f
leaq 0xb2f(%rip), %rbx       # 0x2070
movq %rbx, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x1110
movl %eax, 0x2c35(%rip)      # 0x4198
cmpl $-0x1, %eax
je 0x178e
leaq 0xd58(%rip), %rdi       # 0x22cb
callq 0x1050
xorps %xmm0, %xmm0
movaps %xmm0, 0x2c1e(%rip)     # 0x41a0
movaps %xmm0, 0x2c27(%rip)     # 0x41b0
leaq 0x2c10(%rip), %rsi      # 0x41a0
movl 0x2c02(%rip), %edi      # 0x4198
movl $0x20, %edx
callq 0x1080
cmpq $-0x1, %rax
je 0x1778
movl $0x1, %ebx
leaq 0x2bea(%rip), %r13      # 0x41a0
leaq 0x2c23(%rip), %r14      # 0x41e0
leaq 0xae2(%rip), %r15       # 0x20a6
leaq 0x2c35(%rip), %r12      # 0x4200
movq %r13, %rdi
leaq 0xaa9(%rip), %rsi       # 0x207e
leaq 0x2be4(%rip), %rdx      # 0x41c0
leaq 0x2be1(%rip), %rcx      # 0x41c4
movq %r13, %r8
xorl %eax, %eax
callq 0x10f0
cmpl $0x3, 0x2bcc(%rip)      # 0x41c0
jne 0x171b
movl 0x2bc4(%rip), %esi      # 0x41c4
leaq 0xa80(%rip), %rdi       # 0x2087
xorl %eax, %eax
callq 0x1070
movslq 0x2b7b(%rip), %rax      # 0x4190
leal 0x1(%rax), %ecx
movl %ecx, 0x2b72(%rip)      # 0x4190
cmpq $0x3, %rax
jge 0x174b
movl 0x2b96(%rip), %ecx      # 0x41c4
leaq 0x2b93(%rip), %rdx      # 0x41c8
movl %ecx, (%rdx,%rax,4)
movq %r14, %rdi
movq %r15, %rsi
leaq 0xa66(%rip), %rdx       # 0x20ab
xorl %eax, %eax
callq 0x1150
movq %r14, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
movl 0x2b65(%rip), %ecx      # 0x41c4
movq %r12, %rdi
movq %r15, %rsi
leaq 0xa04(%rip), %rdx       # 0x2070
xorl %eax, %eax
callq 0x1150
movq %r12, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x10c0
movq %r14, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x1110
movslq 0x2afa(%rip), %rcx      # 0x4190
leaq 0x2b83(%rip), %rdx      # 0x4220
movl %eax, -0x4(%rdx,%rcx,4)
movl 0x2aed(%rip), %edi      # 0x4194
movl $0x1, %edx
leaq -0x38(%rbp), %rsi
callq 0x1140
cmpl $-0x1, %eax
je 0x1789
movslq %ebx, %rbx
leaq -0x60(,%rbx,8), %rdi
addq %rbp, %rdi
movslq 0x2abd(%rip), %rcx      # 0x4190
xorl %esi, %esi
leaq 0xcd(%rip), %rdx        # 0x17a9
callq 0x1100
leaq 0x9d6(%rip), %rdi       # 0x20be
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
leaq -0x80(%rbp), %rdi
leaq 0x961(%rip), %rsi       # 0x205d
movl %ebx, %edx
xorl %eax, %eax
callq 0x1150
incl %ebx
movl 0x2a87(%rip), %edi      # 0x4194
movl $0x1, %edx
leaq -0x30(%rbp), %rsi
callq 0x1140
xorps %xmm0, %xmm0
movaps %xmm0, 0x2a8b(%rip)     # 0x41b0
movaps %xmm0, 0x2a74(%rip)     # 0x41a0
movl 0x2a66(%rip), %edi      # 0x4198
movl $0x20, %edx
movq %r13, %rsi
callq 0x1080
cmpq $-0x1, %rax
jne 0x15cb
jmp 0x1757
leaq 0xb88(%rip), %rdi       # 0x22da
callq 0x1050
cmpl $0x2, %ebx
jl 0x1778
movl %ebx, %ebx
movl $0x1, %r14d
movq -0x60(%rbp,%r14,8), %rdi
xorl %esi, %esi
callq 0x1170
incq %r14
cmpq %r14, %rbx
jne 0x1764
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11c0
leaq 0xb61(%rip), %rdi       # 0x22f6
callq 0x1050
movl $0x1, %edi
callq 0x1160
callq 0x11d8