// File: /workspace/github_c_makefile_star2_O3/wierton[P]socket-based-naive-game/server.c
// Function: client_command_user_login
int client_command_user_login(int uid) {
	int is_dup = 0;
	client_message_t *pcm = &sessions[uid].cm;
	char *user_name = pcm->user_name;
	char *password = pcm->password;
	log("user '%s' try to login\n", user_name);
	int message = check_user_registered(user_name, password);

	if(query_session_built(uid)) {
		log("user '%s' has logined\n", sessions[uid].user_name);
		send_to_client(uid, SERVER_RESPONSE_YOU_HAVE_LOGINED);
		return 0;
	}


	for(int i = 0; i < USER_CNT; i++) {
		if(query_session_built(i)) {
			logi("check dup user id: '%s' vs. '%s'\n", user_name, sessions[i].user_name);
			if(strncmp(user_name, sessions[i].user_name, USERNAME_SIZE - 1) == 0) {
				log("user %d@%s duplicate with %dth user '%s'\n", uid, user_name, i, sessions[i].user_name);
				is_dup = 1;
				break;
			}
		}
	}

	// no duplicate user ids found
	if(is_dup) {
		log("send fail dup id message to client.\n");
		send_to_client(uid, SERVER_RESPONSE_LOGIN_FAIL_DUP_USERID);
		sessions[uid].state = USER_STATE_NOT_LOGIN;
	}else if(message == SERVER_RESPONSE_LOGIN_SUCCESS){
		log("user '%s' login success\n", user_name);
		sessions[uid].state = USER_STATE_LOGIN;
		send_to_client(uid, SERVER_RESPONSE_LOGIN_SUCCESS);
		strncpy(sessions[uid].user_name, user_name, USERNAME_SIZE - 1);
		inform_friends(uid, SERVER_MESSAGE_FRIEND_LOGIN);
	}else{
		send_to_client(uid, message);
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %r14d
movslq %edi, %rbx
imulq $0x48, %rbx, %r15
leaq 0x7b4c(%rip), %r12      # 0x9180
leaq (%r15,%r12), %r13
addq $0x19, %r13
leaq 0x20(%r15,%r12), %rax
movq %rax, -0x30(%rbp)
movq 0x79a4(%rip), %rax      # 0x8ff0
movq (%rax), %rdi
leaq 0x4346(%rip), %rsi      # 0x599c
leaq 0x436f(%rip), %rdx      # 0x59cc
movl $0x223, %ecx            # imm = 0x223
movq %r13, %r8
xorl %eax, %eax
callq 0x10d0
movq %r13, -0x40(%rbp)
movq %r13, %rdi
movq -0x30(%rbp), %rsi
callq 0x3a18
cmpl $0x5, %ebx
jae 0x1812
movl %eax, %r13d
addq %r12, %r15
movl %r14d, %eax
imulq $0x48, %rax, %rax
cmpl $0x1, 0xc(%rax,%r12)
ja 0x178d
movq %r15, -0x38(%rbp)
movl %r14d, -0x30(%rbp)
xorl %ebx, %ebx
movq -0x40(%rbp), %r15
movq 0x793d(%rip), %r14      # 0x8ff0
cmpl $0x2, 0xc(%r12)
jae 0x16cd
incq %rbx
addq $0x48, %r12
cmpq $0x5, %rbx
jne 0x16b3
jmp 0x176b
movq (%r14), %rdi
leaq 0x433e(%rip), %rsi      # 0x5a15
leaq 0x42ee(%rip), %rdx      # 0x59cc
movl $0x22f, %ecx            # imm = 0x22F
movq %r15, %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x10d0
movq %r15, %rdi
movq %r12, %rsi
pushq $0x6
popq %rdx
callq 0x1050
testl %eax, %eax
jne 0x16bb
movq (%r14), %rdi
leaq 0x4347(%rip), %rsi      # 0x5a53
leaq 0x42b9(%rip), %r13      # 0x59cc
movq %r13, %rdx
movl $0x231, %ecx            # imm = 0x231
movl -0x30(%rbp), %r14d
movl %r14d, %r8d
movq %r15, %r9
xorl %eax, %eax
pushq %r12
pushq %rbx
callq 0x10d0
addq $0x10, %rsp
movq 0x78b6(%rip), %rax      # 0x8ff0
movq (%rax), %rdi
leaq 0x4351(%rip), %rsi      # 0x5a95
movq %r13, %rdx
movl $0x23a, %ecx            # imm = 0x23A
xorl %eax, %eax
callq 0x10d0
pushq $0x9
popq %rsi
movl %r14d, %edi
callq 0x359e
movq -0x38(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x177c
cmpl $0x4, %r13d
je 0x17bc
movl -0x30(%rbp), %edi
movl %r13d, %esi
callq 0x359e
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x785c(%rip), %rax      # 0x8ff0
movq (%rax), %rdi
leaq 0x4248(%rip), %rsi      # 0x59e6
leaq 0x4227(%rip), %rdx      # 0x59cc
movl $0x227, %ecx            # imm = 0x227
movq %r15, %r8
xorl %eax, %eax
callq 0x10d0
pushq $0x5
popq %rsi
movl %r14d, %edi
jmp 0x1777
movq (%r14), %rdi
leaq 0x430c(%rip), %rsi      # 0x5ad2
leaq 0x41ff(%rip), %rdx      # 0x59cc
movl $0x23e, %ecx            # imm = 0x23E
movq %r15, %r8
xorl %eax, %eax
callq 0x10d0
movq -0x38(%rbp), %r14
movl $0x2, 0xc(%r14)
pushq $0x4
popq %rsi
movl -0x30(%rbp), %ebx
movl %ebx, %edi
callq 0x359e
pushq $0x6
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1040
pushq $0x14
popq %rsi
movl %ebx, %edi
callq 0x326c
jmp 0x177c
leaq 0x3838(%rip), %rdi      # 0x5051
leaq 0x37f4(%rip), %rsi      # 0x5014
leaq 0x37f6(%rip), %rcx      # 0x501d
pushq $0x4a
popq %rdx
callq 0x1090