// File: /workspace/github_c_makefile_star2_O0/QwazyWabbitWOS[P]lmctf60/g_ctffunc.c
// Function: ctf_SetEntTeamEx
void ctf_SetEntTeamEx(edict_t* ent, int whatteam, int nopenalty)
{
	char buf[MAX_INFO_STRING];
	edict_t *player;
	int red, blue;
	char message[MAX_INFO_STRING];
//	int Old_Team;


	//Old_Team = ent->client->ctf.teamnum;
	//
	//if(Old_Team <= CTF_TEAM_OBSERVER && whatteam >= CTF_TEAM_RED)
	//{
	//	ForceCommand(ent, "spectator 0");
	//	//ent->client->ctf.teamnum = whatteam;
	//	return;
	//}


	red = blue = 0;
	player = ctf_findplayer(NULL, ent, CTF_TEAM_ANYTEAM);
	while (player)
	{
		if (player->client->ctf.teamnum == CTF_TEAM_RED)
			red++;
		else if (player->client->ctf.teamnum == CTF_TEAM_BLUE)
			blue++;
		player = ctf_findplayer(player, ent, CTF_TEAM_ANYTEAM);
	}

	//validate the team
	//if (whatteam != CTF_TEAM_RED && whatteam != CTF_TEAM_BLUE &&
	//	whatteam != CTF_TEAM_OBSERVER)
	//	whatteam = CTF_TEAM_UNDEFINED;
	//bat
	if(whatteam >= CTF_TEAM_LIMIT || whatteam <= CTF_TEAM_MIN_LIMIT)
		whatteam = CTF_TEAM_UNDEFINED;

	ent->client->ctf.teamnum = whatteam;

	if (ent->client->p_stats_player)
		ent->client->p_stats_player->info.teamnum = whatteam;

	if (!ent || !ent->inuse || !ent->client)
		return; //can't use validate player because this gets called
	//before the ent's classname is set to player.
	
	// STDLog Team Change - Surt
	strcpy(buf,"");
	ctf_teamstring(buf, ent->client->ctf.teamnum, CTF_TEAM_MATCHING);
	sl_LogPlayerTeamChange( &gi,
		ent->client->pers.netname,
		buf,
		level.time );

	//bat
	//Let them keep their score all the time.

	//if (((ent->client->ctf.teamnum == CTF_TEAM_BLUE) && (red > blue) && (redscore > bluescore)) ||
	//	((ent->client->ctf.teamnum == CTF_TEAM_RED) && (blue > red) && (bluescore > redscore)) ||
	//	nopenalty)
	//{
	//	// No penalty for changing teams
	//}
	//else
	//{
	//	//set the client score to 0
	//	stats_clear(ent);
	//}
	

	Com_sprintf(message, sizeof message, "%s is now on the %s team.\n", ent->client->pers.netname, buf);
	ctf_BSafePrint(PRINT_HIGH, message);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x420, %rsp            # imm = 0x420
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl $0x0, -0x220(%rbp)
movl $0x0, -0x21c(%rbp)
movq -0x8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
movl $0xfffffffb, %edx       # imm = 0xFFFFFFFB
callq 0x19340
movq %rax, -0x218(%rbp)
cmpq $0x0, -0x218(%rbp)
je 0x251f4
movq -0x218(%rbp), %rax
movq 0x58(%rax), %rax
cmpl $0x1, 0x1100(%rax)
jne 0x251b1
movl -0x21c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x21c(%rbp)
jmp 0x251d6
movq -0x218(%rbp), %rax
movq 0x58(%rax), %rax
cmpl $0x2, 0x1100(%rax)
jne 0x251d4
movl -0x220(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x220(%rbp)
jmp 0x251d6
movq -0x218(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl $0xfffffffb, %edx       # imm = 0xFFFFFFFB
callq 0x19340
movq %rax, -0x218(%rbp)
jmp 0x25182
cmpl $0x3, -0xc(%rbp)
jge 0x25200
cmpl $-0x4, -0xc(%rbp)
jg 0x25207
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %ecx
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movl %ecx, 0x1100(%rax)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
cmpq $0x0, 0x9138(%rax)
je 0x25242
movl -0xc(%rbp), %ecx
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x9138(%rax), %rax
movl %ecx, 0x204(%rax)
cmpq $0x0, -0x8(%rbp)
je 0x2525e
movq -0x8(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x2525e
movq -0x8(%rbp), %rax
cmpq $0x0, 0x58(%rax)
jne 0x25263
jmp 0x25312
leaq -0x210(%rbp), %rdi
leaq 0x4bd0f(%rip), %rsi     # 0x70f80
callq 0x182c0
leaq -0x210(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x1100(%rax), %esi
movl $0xfffffff9, %edx       # imm = 0xFFFFFFF9
callq 0x18210
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rsi
addq $0xc0, %rsi
addq $0x200, %rsi            # imm = 0x200
leaq -0x210(%rbp), %rdx
movq 0x5dae7(%rip), %rax     # 0x82da0
movss 0x4(%rax), %xmm0
movq 0x5d33b(%rip), %rdi     # 0x82600
callq 0x18120
leaq -0x420(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rcx
addq $0xc0, %rcx
addq $0x200, %rcx            # imm = 0x200
leaq -0x210(%rbp), %r8
movl $0x200, %esi            # imm = 0x200
leaq 0x4b59b(%rip), %rdx     # 0x70895
movb $0x0, %al
callq 0x18f90
leaq -0x420(%rbp), %rsi
movl $0x2, %edi
callq 0x19470
addq $0x420, %rsp            # imm = 0x420
popq %rbp
retq
nopl (%rax,%rax)