// File: /workspace/github_c_makefile_star2_O3/Ziddykins[P]Dawn/src/combat.c
// Function: check_alive
int check_alive(struct Message *message) {
    int pindex = get_pindex(message->sender_nick);
    char out[MAX_MESSAGE_BUFFER];

    if (dawn->players[pindex].health <= 0 || dawn->players[pindex].fullness <= 0) {
        if (dawn->players[pindex].alive) {
            char reason[32];
            float newexp = (float) dawn->players[pindex].experience * 0.90f;
            strcpy(reason, dawn->players[pindex].health <= 0 ? "been killed" : "died from starvation");
            sprintf(out, "PRIVMSG %s :%s has %s! 10%% experience has been lost\r\n",
                    message->receiver, message->sender_nick, reason);
            add_msg(out, strlen(out));
            dawn->players[pindex].deaths++;
            dawn->players[pindex].alive = 0;
            dawn->players[pindex].experience = (unsigned long long) newexp;
            if (dawn->players[pindex].fullness <= 0)
                dawn->players[pindex].fullness = 100;
            return 0;
        }
    }

    if (dawn->global_monster.hp <= 0 && dawn->global_monster.active) {
        sprintf(out, "PRIVMSG %s :The %s has been killed!\r\n", message->receiver, dawn->global_monster.name);
        add_msg(out, strlen(out));
        for (int i = 0; i < dawn->player_count; i++) {
            if (dawn->players[i].contribution > 0) {
                int drop_chance = rand()%100;
                double percent = ((float) dawn->players[i].contribution / (float) dawn->global_monster.mhp);
                double expgain = percent * dawn->global_monster.exp;
                double goldgain = percent * dawn->global_monster.gold;

                //Need to change the sender_nick so the proper drops are awarded as well as level checking
                strncpy(message->sender_nick, dawn->players[i].username, MAX_NICK_LENGTH);
                dawn->players[i].experience += (unsigned long long) expgain;
                dawn->players[i].gold += (int) goldgain;
                sprintf(out, "PRIVMSG %s :%s has helped defeat the foe and receives %d experience and %d gold"
                                " for their efforts!\r\n", message->receiver, dawn->players[i].username, (int) expgain,
                        (int) goldgain);
                dawn->global_monster.active = 0;
                dawn->players[i].kills++;
                check_levelup(message);
                check_bounty(message);

                if (drop_chance < 85) {
                    generate_drop(message);
                }

                if (dawn->global_monster.alignment == EVIL) {
                    dawn->players[pindex].alignment++;
                    if (dawn->players[pindex].alignment == 26) {
                        snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s is enveloped by a glowing aura!\r\n", 
                                dawn->active_room, dawn->players[i].username);
                    }
                } else if (dawn->global_monster.alignment == GOOD) {
                    dawn->players[pindex].alignment--;
                    if (dawn->players[pindex].alignment == 26) {
                        snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s has grown horns and the town grows worrisome\r\n", 
                                dawn->active_room, dawn->players[i].username);
                    }
                }
                add_msg(out, strlen(out));
            }
        }
        return 0;
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, %rbx
callq 0xda6c
leaq 0x1d4a4(%rip), %rcx     # 0x21a80
movq (%rcx), %rcx
cltq
imulq $0x26a8, %rax, %rdi     # imm = 0x26A8
leaq (%rcx,%rdi), %rdx
addq $0x1a0, %rdx            # imm = 0x1A0
movq 0x2650(%rdx), %rax
testq %rax, %rax
movq %rdi, 0x8(%rsp)
jle 0x460e
cmpw $0x0, 0x26a2(%rdx)
jg 0x4639
leaq 0x1a0(%rcx), %rsi
cmpl $0x0, 0x2660(%rsi,%rdi)
je 0x4639
movq 0x2658(%rdx), %rcx
testq %rcx, %rcx
js 0x4898
cvtsi2ss %rcx, %xmm0
jmp 0x48ad
movl $0x1, %ebp
cmpl $0x0, 0xf2e40(%rcx)
jg 0x497b
cmpl $0x0, 0xf2e64(%rcx)
je 0x497b
addq $0xf2e00, %rcx          # imm = 0xF2E00
leaq 0xc0(%rbx), %rdx
leaq 0xd50b(%rip), %rsi      # 0x11b78
xorl %ebp, %ebp
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %rdx, 0x28(%rsp)
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
callq 0x20c0
movq %r12, %rdi
movq %rax, %rsi
callq 0xcefe
leaq 0x1d3e3(%rip), %rax     # 0x21a80
movq (%rax), %rax
cmpl $0x0, 0x148(%rax)
jle 0x497b
movl $0x1a0, %ebp            # imm = 0x1A0
movl $0x27d8, %r15d          # imm = 0x27D8
xorl %r12d, %r12d
cmpl $0x0, 0x34(%rax,%r15)
jle 0x4873
callq 0x2080
movslq %eax, %r14
imulq $0x51eb851f, %r14, %rax # imm = 0x51EB851F
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x25, %rax
addl %ecx, %eax
imull $0x64, %eax, %eax
subl %eax, %r14d
leaq 0x1d390(%rip), %r13     # 0x21a80
movq (%r13), %rax
leaq (%rax,%rbp), %rsi
xorps %xmm0, %xmm0
cvtsi2ssl 0x34(%rax,%r15), %xmm0
cvtsi2ssl 0xf2e44(%rax), %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0xf2e5c(%rax), %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
xorps %xmm1, %xmm1
cvtsi2sdl 0xf2e58(%rax), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0x1ab21(%rip), %rax     # 0x1f264
movl (%rax), %edx
movq %rbx, %rdi
callq 0x20e0
movapd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
cvttsd2si %xmm0, %r8d
subsd 0xd3ac(%rip), %xmm0     # 0x11b18
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq (%r13), %rcx
addq %rdx, 0x20(%rcx,%r15)
cvttsd2si 0x10(%rsp), %eax
movslq %eax, %r9
addq %r9, 0x10(%rcx,%r15)
addq %rbp, %rcx
leaq 0x60(%rsp), %rdi
leaq 0xd401(%rip), %rsi      # 0x11b9e
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
movq (%r13), %rax
movl $0x0, 0xf2e64(%rax)
incq (%rax,%r15)
movq %rbx, %rdi
callq 0xe32c
movq %rbx, %rdi
callq 0x3879
cmpl $0x54, %r14d
jg 0x47d9
movq %rbx, %rdi
callq 0x7fc0
leaq 0x1d2a0(%rip), %rax     # 0x21a80
movq (%rax), %r8
movl 0xf2e74(%r8), %eax
testl %eax, %eax
je 0x4813
cmpl $0x1, %eax
jne 0x4851
movq 0x8(%rsp), %rcx
movl 0x2834(%r8,%rcx), %eax
incl %eax
movl %eax, 0x2834(%r8,%rcx)
leaq 0xd3f2(%rip), %rdx      # 0x11c03
jmp 0x4831
movq 0x8(%rsp), %rcx
movl 0x2834(%r8,%rcx), %eax
decl %eax
movl %eax, 0x2834(%r8,%rcx)
leaq 0xd403(%rip), %rdx      # 0x11c34
cmpl $0x1a, %eax
jne 0x4851
leaq 0x100(%r8), %rcx
addq %rbp, %r8
movl $0x190, %esi            # imm = 0x190
leaq 0x60(%rsp), %rdi
xorl %eax, %eax
callq 0x2390
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xcefe
leaq 0x1d210(%rip), %rax     # 0x21a80
movq (%rax), %rax
incq %r12
movslq 0x148(%rax), %rcx
movl $0x26a8, %edx           # imm = 0x26A8
addq %rdx, %rbp
addq %rdx, %r15
cmpq %rcx, %r12
jl 0x46bb
xorl %ebp, %ebp
jmp 0x497b
movq %rcx, %rdx
shrq %rdx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
testq %rax, %rax
mulss 0xd254(%rip), %xmm0     # 0x11b0c
movaps %xmm0, 0x10(%rsp)
leaq 0xd25c(%rip), %rax      # 0x11b20
leaq 0xd261(%rip), %rsi      # 0x11b2c
cmovleq %rax, %rsi
leaq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x21c0
leaq 0xc0(%rbx), %rdx
leaq 0xd257(%rip), %rsi      # 0x11b41
xorl %ebp, %ebp
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xcefe
leaq 0x1d165(%rip), %rax     # 0x21a80
movq (%rax), %rax
movq 0x8(%rsp), %rdi
incq 0x27e0(%rax,%rdi)
movaps 0x10(%rsp), %xmm0
cvttss2si %xmm0, %rcx
movl $0x0, 0x2800(%rax,%rdi)
movq %rcx, %rdx
subss 0xd1c5(%rip), %xmm0     # 0x11b10
sarq $0x3f, %rdx
cvttss2si %xmm0, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
movq %rsi, 0x27f8(%rax,%rdi)
cmpw $0x0, 0x2842(%rax,%rdi)
jg 0x497b
addq %rdi, %rax
addq $0x2842, %rax           # imm = 0x2842
movw $0x64, (%rax)
movl %ebp, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq