// File: /workspace/github_c_makefile_star2_O3/QwazyWabbitWOS[P]lmctf60/p_client.c
// Function: PutClientInServer
void PutClientInServer (edict_t *ent)
{
	trace_t		tr; // CTF CODE -- LM_JORM
	
	vec3_t mins = {-16, -16, -24};
	vec3_t maxs = {16, 16, 32};
	int 	index;
	vec3_t spawn_origin;
	vec3_t spawn_angles;
	gclient_t *client;
	int 	i;
 	client_persistent_t	saved;
	client_respawn_t	resp;
	stats_player_s *p_saved_stats = NULL; // STATS - LM_Hati

	static unsigned long unique_id = 6; //guaranteed 0-5 are special
	
	// find a spawn point
	// do it before setting health back up, so farthest
	// ranging doesn't count this client
	SelectSpawnPoint (ent, spawn_origin, spawn_angles);

	index = ent-g_edicts-1;
	client = ent->client;

	// deathmatch wipes most client data every spawn
	if (deathmatch->value)
	{
		char		userinfo[MAX_INFO_STRING];
		char savedsquad [MAX_CATEGORY_LEN]; // ADC
	
		resp = client->resp;
		memcpy (userinfo, client->pers.userinfo, sizeof(userinfo));

		// We don't want to lose the squad so save it
		// off here first, then copy it back. The unitstatus
		// will be set to "respawned" elsewhere.

		strncpy (savedsquad, ent->client->pers.squad, sizeof savedsquad); // ADC
		savedsquad[MAX_CATEGORY_LEN-1] = 0; // ADC

		InitClientPersistent (client);
		ClientUserinfoChanged (ent, userinfo);

		strncpy (ent->client->pers.squad, savedsquad, MAX_CATEGORY_LEN); // ADC
		ent->client->pers.squad[MAX_CATEGORY_LEN-1] = 0; // ADC
	}
	else if (coop->value)
	{
//		int			n;
		char		userinfo[MAX_INFO_STRING];

		resp = client->resp;
		memcpy (userinfo, client->pers.userinfo, sizeof(userinfo));
		// this is kind of ugly, but it's how we want to handle keys in coop
//		for (n = 0; n < game.num_items; n++)
//		{
//			if (itemlist[n].flags & IT_KEY)
//				resp.coop_respawn.inventory[n] = client->pers.inventory[n];
//		}
		resp.coop_respawn.game_helpchanged = client->pers.game_helpchanged;
		resp.coop_respawn.helpchanged = client->pers.helpchanged;
		client->pers = resp.coop_respawn;
		ClientUserinfoChanged (ent, userinfo);
		if (resp.score > client->pers.score)
			client->pers.score = resp.score;
	}
	else
	{
		memset (&resp, 0, sizeof(resp));
	}
	

	p_saved_stats = ent->client->p_stats_player;
	ctftemp = client->ctf; //surt never overwrite

	saved = client->pers;
	memset (client, 0, sizeof(*client));
	client->pers = saved;

	
	if(client->pers.health <= 0)
		InitClientPersistent(client);
	
	client->resp = resp;

	// copy some data from the client to the entity
	FetchClientEntData (ent);

	// clear entity values
	ent->groundentity = NULL;
	ent->client = &game.clients[index];
	ent->takedamage = DAMAGE_AIM;
	ent->movetype = MOVETYPE_WALK;
	ent->viewheight = 22;
	ent->inuse = true;
	ent->classname = "player";
	ent->mass = 200;
	ent->solid = SOLID_BBOX;
	ent->deadflag = DEAD_NO;
	ent->air_finished = level.time + 12;
	ent->clipmask = MASK_PLAYERSOLID;
	ent->model = "players/male/tris.md2";
	ent->pain = player_pain;
	ent->die = player_die;
	ent->waterlevel = 0;
	ent->watertype = 0;
	ent->flags &= ~FL_NO_KNOCKBACK;
	ent->svflags &= ~SVF_DEADMONSTER;

	VectorCopy (mins, ent->mins);
	VectorCopy (maxs, ent->maxs);
	VectorClear (ent->velocity);

	// clear playerstate values
	memset (&ent->client->ps, 0, sizeof(client->ps));

	client->ps.pmove.origin[0] = spawn_origin[0]*8;
	client->ps.pmove.origin[1] = spawn_origin[1]*8;
	client->ps.pmove.origin[2] = spawn_origin[2]*8;

	if (deathmatch->value && ((int)dmflags->value & DF_FIXED_FOV))
	{
		client->ps.fov = 90;
	}
	else
	{
		client->ps.fov = atoi(Info_ValueForKey(client->pers.userinfo, "fov"));
		if (client->ps.fov < 1)
			client->ps.fov = 90;
		else if (client->ps.fov > 160)
			client->ps.fov = 160;
	}

	client->ps.gunindex = gi.modelindex(client->pers.weapon->view_model);

	// clear entity state values
	ent->s.effects = 0;
	ent->s.modelindex = 255;		// will use the skin specified model
	ent->s.modelindex2 = 255;		// custom gun model
	// sknum is player num and weapon number
	// weapon number will be added in changeweapon
	ent->s.skinnum = ent - g_edicts - 1;

	ent->s.frame = 0;
	VectorCopy (spawn_origin, ent->s.origin);
	ent->s.origin[2] += 1;	// make sure off ground
	VectorCopy (ent->s.origin, ent->s.old_origin);

	// set the delta angle
	for (i=0 ; i<3 ; i++)
	{
		client->ps.pmove.delta_angles[i] = ANGLE2SHORT(spawn_angles[i] - client->resp.cmd_angles[i]);
	}

	ent->s.angles[PITCH] = 0;
	ent->s.angles[YAW] = spawn_angles[YAW];
	ent->s.angles[ROLL] = 0;
	VectorCopy (ent->s.angles, client->ps.viewangles);
	VectorCopy (ent->s.angles, client->v_angle);

	
	// spawn a spectator
	if(client->pers.spectator) {
		client->chase_target = NULL;

		client->resp.spectator = true;

		ent->movetype = MOVETYPE_NOCLIP;
		ent->solid = SOLID_NOT;
		ent->svflags |= SVF_NOCLIENT;
		ent->client->ps.gunindex = 0;
		gi.linkentity (ent);
		//Don't return here!  I still want to save everything!
		//-bat
		//return;
	} else
		client->resp.spectator = false;
	

	if (!KillBox (ent))
	{	// couldn't spawn in?
		
		// CTF CODE -- LM_JORM
		// If we failed Killbox, maybe it was because we are on a teammate.  Kill them anyway
		while (1)
		{
			tr = gi.trace (ent->s.origin, ent->mins, ent->maxs, ent->s.origin, NULL, MASK_PLAYERSOLID);
			if (!tr.ent)
				break;
			
			// FIXME -- This will make it look like a suicide
			T_Damage (tr.ent, tr.ent, tr.ent, vec3_origin, ent->s.origin, vec3_origin, 100000, 0, DAMAGE_NO_PROTECTION, MOD_TELEFRAG);
			
			// If we still didn't kill it, stop
			if (tr.ent->solid)
				break;
			
		}
		// END CTF CODE -- LM_JORM
	}


	gi.linkentity (ent);

	client->p_stats_player = p_saved_stats; // STATS - LM_Hati
	client->ctf = ctftemp; //surt restore

	client->ctf.ctfid = unique_id++;
	client->showctfhud = true; // LM_JORM -- Turn on CTF HUD
	client->ctf.extra_flags |= CTF_EXTRAFLAGS_RADIO_SOUND; // LM_JORM -- Turn on our radio
	
	// force the current weapon up
	client->newweapon = client->pers.weapon;
	ChangeWeapon (ent);

	//sprintf(DBuffer, "pcis-z t %d p %d r %d", ent->client->ctf.teamnum,
	//	ent->client->pers.spectator, ent->client->resp.spectator);
	//Debug_Show(DBuffer);


}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xde8, %rsp            # imm = 0xDE8
movq %rdi, %rbx
leaq 0x58(%rsp), %rsi
leaq 0x64(%rsp), %rdx
callq 0x193f0
movq 0x23db5(%rip), %rcx     # 0x68558
movq %rbx, %rbp
movq 0x58(%rbx), %r14
movq 0x2470f(%rip), %rax     # 0x68ec0
movq (%rax), %rax
movss 0x20(%rax), %xmm1
xorps %xmm0, %xmm0
subq (%rcx), %rbp
ucomiss %xmm0, %xmm1
jne 0x447ca
jnp 0x44ce5
leaq 0x758(%r14), %rsi
leaq 0x760(%rsp), %rdi
movl $0x688, %edx            # imm = 0x688
callq 0x18c80
movsd 0xde0(%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0xde8(%r14), %rax
movq %rax, 0x8(%rsp)
movsd 0xdf0(%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
movups 0xdf8(%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0xc0(%r14), %rsi
leaq 0xc0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
callq 0x18c80
movl $0x2d0, %r12d           # imm = 0x2D0
leaq (%r14,%r12), %rsi
leaq 0x70(%rsp), %r13
movl $0x10, %edx
movq %r13, %rdi
callq 0x181c0
xorl %r15d, %r15d
movb %r15b, 0xf(%r13)
movq %r14, %rdi
callq 0x19910
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x19a90
addq 0x58(%rbx), %r12
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x181c0
movq 0x58(%rbx), %rax
movb %r15b, 0x2df(%rax)
sarq $0x3, %rbp
movabsq $-0x20a4f089731d3543, %r13 # imm = 0xDF5B0F768CE2CABD
movq 0x58(%rbx), %rax
movq 0x9138(%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0x1100(%r14), %rsi
leaq 0x4c6f1(%rip), %rdi     # 0x90fa8
movl $0x8038, %edx           # imm = 0x8038
movq %rsi, 0x10(%rsp)
callq 0x18c80
leaq 0xc0(%r14), %r12
leaq 0xc0(%rsp), %r15
movl $0x698, %edx            # imm = 0x698
movq %r15, %rdi
movq %r12, %rsi
callq 0x18c80
movl $0x9140, %edx           # imm = 0x9140
movq %r14, %rdi
xorl %esi, %esi
callq 0x18880
movl $0x698, %edx            # imm = 0x698
movq %r12, %rdi
movq %r15, %rsi
callq 0x18c80
cmpl $0x0, 0x308(%r14)
jg 0x44916
movq %r14, %rdi
callq 0x19910
imulq %r13, %rbp
leaq 0x758(%r14), %rdi
leaq 0x760(%rsp), %rsi
movl $0x688, %edx            # imm = 0x688
callq 0x18c80
movaps 0x20(%rsp), %xmm0
movlps %xmm0, 0xde0(%r14)
movq 0x8(%rsp), %rax
movq %rax, 0xde8(%r14)
movaps 0x30(%rsp), %xmm0
movlps %xmm0, 0xdf0(%r14)
movaps 0x40(%rsp), %xmm0
movups %xmm0, 0xdf8(%r14)
movq 0x58(%rbx), %rcx
movq 0x308(%rcx), %rax
movq %rax, 0x254(%rbx)
movl 0x11c(%rbx), %eax
orl 0x310(%rcx), %eax
movq 0x245d5(%rip), %rdx     # 0x68f60
movq (%rdx), %rdx
movss 0x20(%rdx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x4499d
jnp 0x449a9
movl 0x744(%rcx), %edx
movl %edx, 0xdf4(%rcx)
xorl %ecx, %ecx
movq %rcx, 0x2b8(%rbx)
movq 0x24387(%rip), %rdx     # 0x68d40
decl %ebp
movslq %ebp, %rsi
imulq $0x9140, %rsi, %rdi     # imm = 0x9140
addq 0x408(%rdx), %rdi
movq %rdi, 0x58(%rbx)
movl $0x4, 0x118(%rbx)
movabsq $0x200000016, %rdx      # imm = 0x200000016
movq %rdx, 0x278(%rbx)
movl $0x1, 0x60(%rbx)
leaq 0xf996(%rip), %rdx      # 0x5438f
movq %rdx, 0x138(%rbx)
movl $0xc8, 0x1d8(%rbx)
movl $0x2, 0x108(%rbx)
movl %ecx, 0x260(%rbx)
movq 0x2437f(%rip), %rdx     # 0x68da0
movss 0x4(%rdx), %xmm0
addss 0xe4c6(%rip), %xmm0     # 0x52ef4
movss %xmm0, 0x1dc(%rbx)
movl $0x2010003, 0x10c(%rbx) # imm = 0x2010003
leaq 0x15a1b(%rip), %rdx     # 0x5a462
movq %rdx, 0x120(%rbx)
movq 0x23cc3(%rip), %rdx     # 0x68718
movq %rdx, 0x230(%rbx)
movq 0x23d5d(%rip), %rdx     # 0x687c0
movq %rdx, 0x238(%rbx)
movq %rcx, 0x308(%rbx)
andl $0xfffff7ff, %eax       # imm = 0xFFFFF7FF
movl %eax, 0x11c(%rbx)
andb $-0x3, 0xc8(%rbx)
movaps 0x12986(%rip), %xmm0    # 0x57410
movups %xmm0, 0xcc(%rbx)
movsd 0x12987(%rip), %xmm0    # 0x57420
movsd %xmm0, 0xdc(%rbx)
movl %ecx, 0x1c8(%rbx)
movq %rcx, 0x1c0(%rbx)
movl $0xb8, %edx
xorl %esi, %esi
callq 0x18880
movsd 0x58(%rsp), %xmm1
movaps 0x12919(%rip), %xmm0    # 0x573e0
movaps %xmm1, 0x20(%rsp)
mulps %xmm1, %xmm0
cvttps2dq %xmm0, %xmm0
pshuflw $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3,4,5,6,7]
movd %xmm0, 0x4(%r14)
movss 0x60(%rsp), %xmm1
movss 0xe410(%rip), %xmm0     # 0x52efc
movss %xmm1, 0x1c(%rsp)
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movw %ax, 0x8(%r14)
movq 0x243ba(%rip), %rax     # 0x68ec0
movq (%rax), %rax
movss 0x20(%rax), %xmm0
ucomiss 0xee7b(%rip), %xmm0     # 0x53990
jne 0x44b19
jnp 0x44b2d
movq 0x23af8(%rip), %rax     # 0x68618
movq (%rax), %rax
cvttss2si 0x20(%rax), %eax
testw %ax, %ax
js 0x44b66
leaq 0x10fac(%rip), %rsi     # 0x55ae0
movq %r12, %rdi
callq 0x18bb0
movq %rax, %rdi
callq 0x195e0
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x70(%r14)
testl %eax, %eax
jle 0x44b66
cmpl $0xa1, %eax
jb 0x44b6e
movl $0x43200000, 0x70(%r14) # imm = 0x43200000
jmp 0x44b6e
movl $0x42b40000, 0x70(%r14) # imm = 0x42B40000
leaq 0xdf8(%r14), %r15
leaq 0xcc(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0xd8(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x23a6c(%rip), %rcx     # 0x68600
movq 0x730(%r14), %rax
movq 0x40(%rax), %rdi
callq *0x40(%rcx)
movl %eax, 0x58(%r14)
xorl %eax, %eax
movl %eax, 0x40(%rbx)
movabsq $0xff000000ff, %rcx     # imm = 0xFF000000FF
movq %rcx, 0x28(%rbx)
movq %rbx, %rcx
movq 0x23995(%rip), %rdx     # 0x68558
subq (%rdx), %rcx
shrq $0x3, %rcx
imull $0x8ce2cabd, %ecx, %ecx # imm = 0x8CE2CABD
decl %ecx
movl %ecx, 0x3c(%rbx)
movl %eax, 0x38(%rbx)
leaq 0x4(%rbx), %rbp
movaps 0x20(%rsp), %xmm0
movlps %xmm0, 0x4(%rbx)
movss 0x1c(%rsp), %xmm1
addss 0xe335(%rip), %xmm1     # 0x52f28
movss %xmm1, 0xc(%rbx)
movlps %xmm0, 0x1c(%rbx)
movss %xmm1, 0x24(%rbx)
movss 0xf157(%rip), %xmm0     # 0x53d60
movss 0xf153(%rip), %xmm1     # 0x53d64
movss 0x64(%rsp,%rax,4), %xmm2
subss (%r15,%rax,4), %xmm2
mulss %xmm0, %xmm2
divss %xmm1, %xmm2
cvttss2si %xmm2, %ecx
movw %cx, -0xde4(%r15,%rax,2)
incq %rax
cmpq $0x3, %rax
jne 0x44c11
xorl %eax, %eax
movl %eax, 0x10(%rbx)
movss 0x68(%rsp), %xmm0
movss %xmm0, 0x14(%rbx)
movl %eax, 0x18(%rbx)
movl %eax, 0x1c(%r14)
movss 0x14(%rbx), %xmm0
movss %xmm0, 0x20(%r14)
movss 0x18(%rbx), %xmm0
movss %xmm0, 0x24(%r14)
movss 0x10(%rbx), %xmm0
movss %xmm0, 0xebc(%r14)
movss 0x14(%rbx), %xmm0
movss %xmm0, 0xec0(%r14)
movss 0x18(%rbx), %xmm0
movss %xmm0, 0xec4(%r14)
cmpl %eax, 0x750(%r14)
je 0x44dbc
movq $0x0, 0xf30(%r14)
movl $0x1, %ecx
movl %ecx, 0xe04(%r14)
movl %ecx, 0x118(%rbx)
movl %eax, 0x108(%rbx)
orb $0x1, 0xc8(%rbx)
movq 0x58(%rbx), %rcx
movl %eax, 0x58(%rcx)
movq %rbx, %rdi
movq 0x23926(%rip), %rax     # 0x68600
callq *0x90(%rax)
jmp 0x44dc7
movq 0x24274(%rip), %rax     # 0x68f60
movq (%rax), %rax
movss 0x20(%rax), %xmm1
ucomiss %xmm0, %xmm1
jne 0x44cff
jnp 0x44ecf
leaq 0x758(%r14), %r15
leaq 0x760(%rsp), %rdi
movl $0x688, %edx            # imm = 0x688
movq %r15, %rsi
callq 0x18c80
movq 0xde8(%r14), %r13
movq 0xdf0(%r14), %xmm0
movdqa %xmm0, 0x30(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, 0x10(%rsp)
movups 0xdf8(%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0xc0(%r14), %r12
leaq 0xc0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
movq %r12, %rsi
callq 0x18c80
movsd 0x748(%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
movl $0x688, %edx            # imm = 0x688
movq %r12, %rdi
movq %r15, %rsi
callq 0x18c80
movq %r13, 0x8(%rsp)
movq %r13, 0x750(%r14)
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x19a90
movl 0x10(%rsp), %eax
cmpl 0x744(%r14), %eax
jle 0x4488b
movl %eax, 0x744(%r14)
jmp 0x4488b
movl $0x0, 0xe04(%r14)
movq %rbx, %rdi
callq 0x18a90
testl %eax, %eax
jne 0x44e49
leaq 0x70(%rsp), %r15
movq 0x23989(%rip), %r12     # 0x68768
subq $0x8, %rsp
movq %r15, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
pushq $0x2010003              # imm = 0x2010003
movq 0x237fb(%rip), %rax     # 0x68600
callq *0x60(%rax)
addq $0x10, %rsp
movq 0xb0(%rsp), %r13
testq %r13, %r13
je 0x44e49
movq %r13, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %rbp, %r8
movq %r12, %r9
pushq $0x15
pushq $0x20
pushq $0x0
pushq $0x186a0                # imm = 0x186A0
callq 0x190f0
addq $0x20, %rsp
cmpl $0x0, 0x108(%r13)
je 0x44ddf
movq %rbx, %rdi
movq 0x237ad(%rip), %rax     # 0x68600
callq *0x90(%rax)
movq 0x50(%rsp), %rax
movq %rax, 0x9138(%r14)
leaq 0x4c13c(%rip), %rsi     # 0x90fa8
movl $0x8038, %edx           # imm = 0x8038
movq 0x10(%rsp), %rdi
callq 0x18c80
movq 0x28f9e(%rip), %rax     # 0x6de20
leaq 0x1(%rax), %rcx
movq %rcx, 0x28f93(%rip)     # 0x6de20
movq %rax, 0x9120(%r14)
movl $0x1, 0xf78(%r14)
orb $0x20, 0x1108(%r14)
movq 0x730(%r14), %rax
movq %rax, 0xe50(%r14)
movq %rbx, %rdi
callq 0x187a0
addq $0xde8, %rsp            # imm = 0xDE8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x760(%rsp), %rdi
movq $0x0, 0x8(%rsp)
movl $0x688, %edx            # imm = 0x688
xorl %esi, %esi
callq 0x18880
xorps %xmm1, %xmm1
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x20(%rsp)
jmp 0x4488b