// File: /workspace/github_c_makefile_star2_O1/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 0xd89c
movl %eax, %r15d
movq %r14, %rdi
callq 0xd89c
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 0x10d1d(%rip), %rax     # 0x1f264
movl (%rax), %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x20e0
cmpl $-0x1, %ebp
je 0xe5fd
leaq 0x1b8(%rsp), %r12
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xeeec
movl (%r13), %edx
leaq 0x97f7(%rip), %rdi      # 0x17d75
movq %r12, %rsi
xorl %eax, %eax
callq 0x2040
leaq 0x134f1(%rip), %r12     # 0x21a80
movq (%r12), %rcx
movslq %r15d, %rax
imulq $0x26a8, %rax, %r13     # imm = 0x26A8
cmpl $0x0, 0x12f8(%rcx,%r13)
je 0xe626
leaq 0x1a0(%rcx), %rdx
movslq %ebp, %rax
imulq $0x26a8, %rax, %r15     # imm = 0x26A8
movq 0x2650(%rdx,%r15), %rax
testq %rax, %rax
jle 0xe636
addq %r15, %rdx
movslq 0x2670(%rdx), %rdx
movslq (%rsp), %rsi
addq %rdx, %rsi
cmpq %rsi, %rax
jne 0xe682
addq $0x100, %rcx            # imm = 0x100
leaq 0x9813(%rip), %rdx      # 0x17e01
leaq 0x20(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
movq %r14, %r8
jmp 0xe651
leaq 0x1347c(%rip), %rax     # 0x21a80
movl $0x100, %ecx            # imm = 0x100
addq (%rax), %rcx
leaq 0x9745(%rip), %rdx      # 0x17d58
leaq 0x20(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
xorl %eax, %eax
callq 0x2390
jmp 0xe658
addq $0x100, %rcx            # imm = 0x100
leaq 0x974f(%rip), %rdx      # 0x17d83
jmp 0xe644
addq $0x100, %rcx            # imm = 0x100
leaq 0x976c(%rip), %rdx      # 0x17db0
leaq 0x20(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
movq %rbx, %r8
xorl %eax, %eax
callq 0x2390
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xcd1c
addq $0xab8, %rsp            # imm = 0xAB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%rcx,%r13), %rax
addq $0x12f0, %rax           # imm = 0x12F0
movl (%rax), %eax
leal (%rax,%rax,4), %ebp
callq 0x2080
movq (%r12), %rcx
movl 0x2808(%rcx,%r13), %edx
movl 0x2820(%rcx,%r13), %esi
leal (%rdx,%rdx,2), %edi
cltd
idivl %edi
cmpl %ebp, %esi
jle 0xe71b
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 0xe6f3
addq %r15, %rax
movq %rdx, 0x2650(%rax)
addq $0x100, %rcx            # imm = 0x100
leaq 0x972b(%rip), %rsi      # 0x17e2c
leaq 0x20(%rsp), %rdi
movq %rcx, %rdx
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x2050
jmp 0xe658
addq $0x100, %rcx            # imm = 0x100
leaq 0x972d(%rip), %rsi      # 0x17e56
leaq 0x20(%rsp), %rdi
movq %rcx, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
jmp 0xe658