// File: /workspace/github_c_makefile_star2_O2/Ziddykins[P]Dawn/src/spells.c
// Function: cast_heal
void cast_heal(const char *username, const char *target) {
    int pindex = get_pindex(username);
    int tindex = get_pindex(target);
    int stats[7] = {0};
    struct Message temp = {0};
    char out[MAX_MESSAGE_BUFFER];
    strncpy(temp.sender_nick, target, MAX_NICK_LENGTH);

    if (tindex == -1) {
        snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :Invalid target\r\n", dawn->active_room);
        add_msg(out, strlen(out));
        return;
    }

    get_stat(&temp, stats);
    printf("%s stat0: %d\n", temp.sender_nick, stats[0]);

    if (!dawn->players[pindex].spellbook.heal.learned) {
        snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s, you do not know this spell\r\n",
                 dawn->active_room, username);
        add_msg(out, strlen(out));
        return;
    }

    if (dawn->players[tindex].health <= 0) {
        snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s, your magic powers are not yet strong enough "
                "to revive the dead\r\n", dawn->active_room, username);
        add_msg(out, strlen(out));
        return;
    }

    if (dawn->players[tindex].health == dawn->players[tindex].max_health+stats[0]) {
        snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s is already at full health\r\n",
                 dawn->active_room, target);
        add_msg(out, strlen(out));
        return;
    }

    int heal_cost = 1 + dawn->players[pindex].spellbook.heal.level * 5;
    int heal_amount = 10 + rand() % (dawn->players[pindex].level * 3);

    if (dawn->players[pindex].mana >= heal_cost) {
        dawn->players[tindex].health += heal_amount;
        if (dawn->players[tindex].health >= dawn->players[tindex].max_health + stats[0])
            dawn->players[tindex].health = dawn->players[tindex].max_health + stats[0];
        sprintf(out, "PRIVMSG %s :%s has healed %s for %d HP!\r\n", dawn->active_room, username, target, heal_amount);
    } else {
        sprintf(out, "PRIVMSG %s :%s, you don't have enough mana to cast this spell\r\n", dawn->active_room, username);
    }

    add_msg(out, strlen(out));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xab8, %rsp            # imm = 0xAB8
movq %rsi, %r14
movq %rdi, %rbx
callq 0xc860
movl %eax, %r15d
movq %r14, %rdi
callq 0xc860
movl %eax, %ebp
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rsp)
movaps %xmm0, (%rsp)
leaq 0x1b8(%rsp), %r12
movl $0x900, %edx            # imm = 0x900
movq %r12, %rdi
xorl %esi, %esi
callq 0x20f0
leaq 0xfcf5(%rip), %rax      # 0x1d264
movl (%rax), %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x20e0
cmpl $-0x1, %ebp
je 0xd625
leaq 0x1b8(%rsp), %r12
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xdee4
movl (%r13), %edx
leaq 0x97ff(%rip), %rdi      # 0x16da5
movq %r12, %rsi
xorl %eax, %eax
callq 0x2040
leaq 0x124c9(%rip), %r13     # 0x1fa80
movq (%r13), %rcx
movslq %r15d, %rax
imulq $0x26a8, %rax, %r12     # imm = 0x26A8
cmpl $0x0, 0x12f8(%rcx,%r12)
je 0xd64e
leaq 0x1a0(%rcx), %rdx
movslq %ebp, %rax
imulq $0x26a8, %rax, %r15     # imm = 0x26A8
movq 0x2650(%rdx,%r15), %rax
testq %rax, %rax
jle 0xd65e
addq %r15, %rdx
movslq 0x2670(%rdx), %rdx
movslq (%rsp), %rsi
addq %rdx, %rsi
cmpq %rsi, %rax
jne 0xd6aa
addq $0x100, %rcx            # imm = 0x100
leaq 0x981b(%rip), %rdx      # 0x16e31
leaq 0x20(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
movq %r14, %r8
jmp 0xd679
leaq 0x12454(%rip), %rax     # 0x1fa80
movl $0x100, %ecx            # imm = 0x100
addq (%rax), %rcx
leaq 0x974d(%rip), %rdx      # 0x16d88
leaq 0x20(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
xorl %eax, %eax
callq 0x23a0
jmp 0xd680
addq $0x100, %rcx            # imm = 0x100
leaq 0x9757(%rip), %rdx      # 0x16db3
jmp 0xd66c
addq $0x100, %rcx            # imm = 0x100
leaq 0x9774(%rip), %rdx      # 0x16de0
leaq 0x20(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
movq %rbx, %r8
xorl %eax, %eax
callq 0x23a0
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xbd04
addq $0xab8, %rsp            # imm = 0xAB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%rcx,%r12), %rax
addq $0x12f0, %rax           # imm = 0x12F0
imull $0x5, (%rax), %ebp
callq 0x2080
movq (%r13), %rcx
imull $0x3, 0x2808(%rcx,%r12), %esi
cltd
idivl %esi
cmpl %ebp, 0x2820(%rcx,%r12)
jle 0xd73d
movl %edx, %r9d
leaq 0x1a0(%rcx), %rax
addl $0xa, %r9d
movslq %r9d, %rsi
addq 0x2650(%rax,%r15), %rsi
movq %rsi, 0x2650(%rax,%r15)
movslq 0x2670(%rax,%r15), %rdi
movslq (%rsp), %rdx
addq %rdi, %rdx
cmpq %rdx, %rsi
jl 0xd715
addq %r15, %rax
movq %rdx, 0x2650(%rax)
addq $0x100, %rcx            # imm = 0x100
leaq 0x9739(%rip), %rsi      # 0x16e5c
leaq 0x20(%rsp), %rdi
movq %rcx, %rdx
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x2050
jmp 0xd680
addq $0x100, %rcx            # imm = 0x100
leaq 0x973b(%rip), %rsi      # 0x16e86
leaq 0x20(%rsp), %rdi
movq %rcx, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
jmp 0xd680