// File: /workspace/github_c_makefile_star2_O1/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 0x7b35(%rip), %r12      # 0x9180
leaq (%r15,%r12), %r13
addq $0x19, %r13
leaq 0x20(%r15,%r12), %rax
movq %rax, -0x30(%rbp)
movq 0x798d(%rip), %rax      # 0x8ff0
movq (%rax), %rdi
leaq 0x432f(%rip), %rsi      # 0x599c
leaq 0x4358(%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 0x3a01
cmpl $0x5, %ebx
jae 0x1829
movl %eax, %r13d
addq %r12, %r15
movl %r14d, %eax
imulq $0x48, %rax, %rax
cmpl $0x1, 0xc(%rax,%r12)
ja 0x17a4
movq %r15, -0x38(%rbp)
movl %r14d, -0x30(%rbp)
xorl %ebx, %ebx
movq -0x40(%rbp), %r15
movq 0x7926(%rip), %r14      # 0x8ff0
cmpl $0x2, 0xc(%r12)
jae 0x16e4
incq %rbx
addq $0x48, %r12
cmpq $0x5, %rbx
jne 0x16ca
jmp 0x1782
movq (%r14), %rdi
leaq 0x4327(%rip), %rsi      # 0x5a15
leaq 0x42d7(%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 0x16d2
movq (%r14), %rdi
leaq 0x4330(%rip), %rsi      # 0x5a53
leaq 0x42a2(%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 0x789f(%rip), %rax      # 0x8ff0
movq (%rax), %rdi
leaq 0x433a(%rip), %rsi      # 0x5a95
movq %r13, %rdx
movl $0x23a, %ecx            # imm = 0x23A
xorl %eax, %eax
callq 0x10d0
pushq $0x9
popq %rsi
movl %r14d, %edi
callq 0x357d
movq -0x38(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x1793
cmpl $0x4, %r13d
je 0x17d3
movl -0x30(%rbp), %edi
movl %r13d, %esi
callq 0x357d
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7845(%rip), %rax      # 0x8ff0
movq (%rax), %rdi
leaq 0x4231(%rip), %rsi      # 0x59e6
leaq 0x4210(%rip), %rdx      # 0x59cc
movl $0x227, %ecx            # imm = 0x227
movq %r15, %r8
xorl %eax, %eax
callq 0x10d0
pushq $0x5
popq %rsi
movl %r14d, %edi
jmp 0x178e
movq (%r14), %rdi
leaq 0x42f5(%rip), %rsi      # 0x5ad2
leaq 0x41e8(%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 0x357d
pushq $0x6
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1040
pushq $0x14
popq %rsi
movl %ebx, %edi
callq 0x3255
jmp 0x1793
leaq 0x3821(%rip), %rdi      # 0x5051
leaq 0x37dd(%rip), %rsi      # 0x5014
leaq 0x37df(%rip), %rcx      # 0x501d
pushq $0x4a
popq %rdx
callq 0x1090