#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ add_user(int, char *);
int /*<<< orphan*/ clear_client(int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rm_user_by_fd(int);
scalar_t__ send_ack(int, char, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void handle_login(int fd, char *msg) {
  char *nick = malloc((strlen(msg) - 1) * sizeof(char));
  strcpy(nick, msg + 2);
  printf("TRACE: adding nick: \"%s\"\n", nick);
  if (add_user(fd, nick) == 0) {
    if (send_ack(fd, '0', 0)) {
      printf("ERROR: Cannot send ack!\n");
      rm_user_by_fd(fd);
      clear_client(fd);
    }
  } else {
    if (send_ack(fd, '1',
                 "Cannot add user, server full or nick already exists") != 0) {
      printf("ERROR: Cannot send nack!\n");
      clear_client(fd);
    }
  }
}


x86 assembly (clang, -O0):
<handle_login>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1080 <add_user@plt>
cmp    $0x0,%rax
jne    1223 <handle_login+0xa3>
mov    -0x4(%rbp),%edi
mov    $0x30,%esi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <send_ack@plt>
cmp    $0x0,%rax
je     121e <handle_login+0x9e>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <rm_user_by_fd@plt>
mov    -0x4(%rbp),%edi
call   1040 <clear_client@plt>
jmp    125c <handle_login+0xdc>
mov    -0x4(%rbp),%edi
mov    $0x31,%esi
lea    0xe01(%rip),%rdx
call   10a0 <send_ack@plt>
cmp    $0x0,%rax
je     1257 <handle_login+0xd7>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%edi
call   1040 <clear_client@plt>
jmp    125c <handle_login+0xdc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_login>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1070 <strlen@plt>
lea    -0x1(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%rbp
add    $0x2,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xe3d(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1090 <add_user@plt>
test   %rax,%rax
je     1204 <handle_login+0x74>
lea    0xe36(%rip),%rdx
mov    %r14d,%edi
mov    $0x31,%esi
call   10b0 <send_ack@plt>
test   %rax,%rax
je     1238 <handle_login+0xa8>
lea    0xe51(%rip),%rdi
call   1050 <puts@plt>
jmp    122c <handle_login+0x9c>
mov    %r14d,%edi
mov    $0x30,%esi
xor    %edx,%edx
call   10b0 <send_ack@plt>
test   %rax,%rax
je     1238 <handle_login+0xa8>
lea    0xe48(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
call   1060 <rm_user_by_fd@plt>
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <clear_client@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"TRACE: adding nick: \22%s\22\0A\00"
@.str.1 = [25 x i8] c"ERROR: Cannot send ack!\0A\00"
@.str.2 = [52 x i8] c"Cannot add user, server full or nick already exists\00"
@.str.3 = [26 x i8] c"ERROR: Cannot send nack!\0A\00"
define void @handle_login(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i32 @strlen(i8* %6)
%8 = sub nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 1
%11 = trunc i64 %10 to i32
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 2
%16 = call i32 @strcpy(i8* %13, i8* %15)
%17 = load i8*, i8** %5
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %3
%20 = load i8*, i8** %5
%21 = call i64 @add_user(i32 %19, i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %2
%24 = load i32, i32* %3
%25 = call i64 @send_ack(i32 %24, i8 signext 48, i8* null)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* %3
%30 = call i32 @rm_user_by_fd(i32 %29)
%31 = load i32, i32* %3
%32 = call i32 @clear_client(i32 %31)
br label %33
33: ; preds = %27, %23
br label %43
34: ; preds = %2
%35 = load i32, i32* %3
%36 = call i64 @send_ack(i32 %35, i8 signext 49, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%40 = load i32, i32* %3
%41 = call i32 @clear_client(i32 %40)
br label %42
42: ; preds = %38, %34
br label %43
43: ; preds = %42, %33
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"TRACE: adding nick: \22%s\22\0A\00"
@.str.2 = [52 x i8] c"Cannot add user, server full or nick already exists\00"
@str = [25 x i8] c"ERROR: Cannot send nack!\00"
@str.4 = [24 x i8] c"ERROR: Cannot send ack!\00"
define void @handle_login(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = add nsw i32 %3, -1
%5 = tail call i8* @malloc(i32 %4)
%6 = getelementptr inbounds i8, i8* %1, i64 2
%7 = tail call i32 @strcpy(i8* %5, i8* %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %5)
%9 = tail call i64 @add_user(i32 %0, i8* %5)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = tail call i64 @send_ack(i32 %0, i8 signext 48, i8* null)
%13 = icmp eq i64 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0))
%16 = tail call i32 @rm_user_by_fd(i32 %0)
br label %22
17: ; preds = %2
%18 = tail call i64 @send_ack(i32 %0, i8 signext 49, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %14, %20
%23 = tail call i32 @clear_client(i32 %0)
br label %24
24: ; preds = %22, %17, %11
ret void
}