// File: /workspace/github_c_makefile_star2_O2/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, -0x2c(%rbp)
movslq %edi, %rbx
imulq $0x48, %rbx, %rax
leaq 0x59b8(%rip), %r12      # 0x8180
leaq (%r12,%rax), %rcx
movq %rcx, -0x38(%rbp)
leaq (%rax,%r12), %r15
addq $0x19, %r15
leaq 0x20(%rax,%r12), %r14
movq 0x580c(%rip), %rax      # 0x7ff0
movq (%rax), %rdi
leaq 0x21ae(%rip), %rsi      # 0x499c
leaq 0x21d7(%rip), %rdx      # 0x49cc
movl $0x223, %ecx            # imm = 0x223
movq %r15, %r8
xorl %eax, %eax
callq 0x10d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x24b6
movl %eax, %r13d
movl %ebx, %edi
callq 0x12ac
testl %eax, %eax
jne 0x290f
pushq $0x6
popq %rbx
xorl %r14d, %r14d
cmpq $0x5, %r14
je 0x28ed
movl %r14d, %edi
callq 0x12ac
testl %eax, %eax
jne 0x2846
incq %r14
addq $0x48, %r12
jmp 0x2827
movq 0x57a3(%rip), %rax      # 0x7ff0
movq (%rax), %rdi
leaq 0x21be(%rip), %rsi      # 0x4a15
leaq 0x216e(%rip), %rdx      # 0x49cc
movl $0x22f, %ecx            # imm = 0x22F
movq %r15, %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x10d0
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1050
testl %eax, %eax
jne 0x283d
movq 0x5767(%rip), %rbx      # 0x7ff0
movq (%rbx), %rdi
leaq 0x21c0(%rip), %rsi      # 0x4a53
leaq 0x2132(%rip), %rdx      # 0x49cc
movl $0x231, %ecx            # imm = 0x231
movl -0x2c(%rbp), %r13d
movl %r13d, %r8d
movq %r15, %r9
xorl %eax, %eax
pushq %r12
pushq %r14
callq 0x10d0
addq $0x10, %rsp
movq (%rbx), %rdi
leaq 0x21d3(%rip), %rsi      # 0x4a95
leaq 0x2103(%rip), %rdx      # 0x49cc
movl $0x23a, %ecx            # imm = 0x23A
xorl %eax, %eax
callq 0x10d0
pushq $0x9
popq %rsi
movl %r13d, %edi
callq 0x1e05
movq -0x38(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x28fe
cmpl $0x4, %r13d
je 0x293f
movl -0x2c(%rbp), %edi
movl %r13d, %esi
callq 0x1e05
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x56da(%rip), %rax      # 0x7ff0
movq (%rax), %rdi
leaq 0x20c6(%rip), %rsi      # 0x49e6
leaq 0x20a5(%rip), %rdx      # 0x49cc
movl $0x227, %ecx            # imm = 0x227
movq -0x38(%rbp), %r8
xorl %eax, %eax
callq 0x10d0
pushq $0x5
popq %rsi
movl -0x2c(%rbp), %edi
jmp 0x28f9
movq 0x56aa(%rip), %rax      # 0x7ff0
movq (%rax), %rdi
leaq 0x2182(%rip), %rsi      # 0x4ad2
leaq 0x2075(%rip), %rdx      # 0x49cc
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 -0x2c(%rbp), %ebx
movl %ebx, %edi
callq 0x1e05
pushq $0x6
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1040
pushq $0x14
popq %rsi
movl %ebx, %edi
callq 0x19a3
jmp 0x28fe