// File: /workspace/github_c_makefile_star2_O2/Ziddykins[P]Dawn/src/combat.c
// Function: player_attacks
void player_attacks(struct Message *message, int global) {
    int pindex = get_pindex(message->sender_nick);
    char out[MAX_MESSAGE_BUFFER];
    int player_attack, monster_defense, player_defense, critical;
    int ustats[6] = {dawn->players[pindex].max_health, dawn->players[pindex].max_mana,
                     dawn->players[pindex].strength, dawn->players[pindex].intelligence,
                     dawn->players[pindex].m_def, dawn->players[pindex].defense};

    get_stat(message, ustats);
    player_defense = rand() % dawn->players[pindex].defense;

    //15% chance to critical hit (double) attack
    if ((rand() % 100) < 15) {
        critical = 1;
        player_attack = (rand() % ustats[2]) * 2;
    } else {
        critical = 0;
        player_attack = rand() % ustats[2];
    }

    if (!dawn->players[pindex].alive) {
        sprintf(out, "PRIVMSG %s :%s, you are dead and can't make an attack; revive at a shrine"
                     ", use a potion, or have someone revive you!\r\n", message->receiver, message->sender_nick);
        add_msg(out, strlen(out));
        return;
    }

    if (global && dawn->global_monster.active) {
        if (player_attack == 0) {
            sprintf(out, "PRIVMSG %s :%s trips over his shoelace and misses\r\n", message->receiver,
                         message->sender_nick);
            add_msg(out, strlen(out));
            monster_attacks(message, player_defense, pindex);
        } else {
            //Avoid floating point exceptions
            if (!dawn->global_monster.def == 0) {
                monster_defense = rand() % dawn->global_monster.def;
            } else {
                monster_defense = 0;
            }
            if (player_attack - monster_defense > 0) {
                calc_contribution(pindex, (player_attack - monster_defense), dawn->global_monster.mhp,
                                  dawn->global_monster.hp);
                dawn->global_monster.hp -= (player_attack - monster_defense);
                if (critical) {
                    sprintf(out, "PRIVMSG %s :%s attacks the %s for %d %sCRITICAL%s damage! "
                            "Remaining (%d)\r\n",
                            message->receiver, message->sender_nick, dawn->global_monster.name,
                            player_attack - monster_defense, IRC_RED, IRC_NORMAL, dawn->global_monster.hp);
                } else {
                    sprintf(out, "PRIVMSG %s :%s attacks the %s for %d damage! (Remaining: %d)\r\n",
                            message->receiver, message->sender_nick, dawn->global_monster.name,
                            player_attack - monster_defense, dawn->global_monster.hp);
                }
                add_msg(out, strlen(out));
                if (check_alive(message)) {
                    monster_attacks(message, player_defense, pindex);
                }
            } else {
                sprintf(out, "PRIVMSG %s :The %s has blocked %s's attack!\r\n", message->receiver,
                        dawn->global_monster.name, message->sender_nick);
                monster_attacks(message, player_defense, pindex);
                add_msg(out, strlen(out));
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movl %esi, %r13d
movq %rdi, %rbx
callq 0xc860
leaq 0x1bec7(%rip), %r12     # 0x1fa80
movq (%r12), %rdx
movl %eax, 0x8(%rsp)
movslq %eax, %rcx
imulq $0x26a8, %rcx, %rbp     # imm = 0x26A8
movl 0x2810(%rdx,%rbp), %ecx
leaq 0x10(%rsp), %r15
movl %ecx, (%r15)
movl 0x2814(%rdx,%rbp), %ecx
movl %ecx, 0x4(%r15)
movl 0x2824(%rdx,%rbp), %ecx
movl %ecx, 0x8(%r15)
movl 0x2828(%rdx,%rbp), %ecx
movl %ecx, 0xc(%r15)
movl 0x2830(%rdx,%rbp), %ecx
movl %ecx, 0x10(%r15)
movl 0x282c(%rdx,%rbp), %eax
movl %eax, 0x14(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdee4
callq 0x2080
movq (%r12), %rcx
cltd
idivl 0x282c(%rcx,%rbp)
movl %edx, 0x4(%rsp)
callq 0x2080
pushq $0x64
popq %rcx
cltd
idivl %ecx
movl %edx, 0xc(%rsp)
cmpl $0xf, %edx
setl %r14b
callq 0x2080
cltd
idivl 0x8(%r15)
movl %edx, %r15d
movl %r14d, %ecx
shll %cl, %r15d
movq (%r12), %rcx
cmpl $0x0, 0x2800(%rcx,%rbp)
je 0x3ca4
testl %r13d, %r13d
je 0x3e43
cmpl $0x0, 0xf2e64(%rcx)
je 0x3e43
testl %edx, %edx
je 0x3cdc
cmpl $0x0, 0xf2e48(%rcx)
movq %rbx, %r13
je 0x3d14
callq 0x2080
movq (%r12), %rcx
cltd
idivl 0xf2e48(%rcx)
jmp 0x3d16
leaq 0xc0(%rbx), %rdx
leaq 0xcf88(%rip), %rsi      # 0x10c3a
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xbd04
jmp 0x3e43
leaq 0xc0(%rbx), %rdx
leaq 0xcfc7(%rip), %rsi      # 0x10cb1
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xbd04
jmp 0x3e33
xorl %edx, %edx
subl %edx, %r15d
jle 0x3db2
movl 0xf2e44(%rcx), %eax
leaq (%rcx,%rbp), %rdx
addq $0x280c, %rdx           # imm = 0x280C
cmpl %eax, %r15d
jge 0x3d4f
movl 0xf2e40(%rcx), %ecx
movl (%rdx), %esi
leal (%rsi,%r15), %edi
addl %ecx, %esi
cmpl %ecx, %edi
cmovlel %edi, %esi
cmpl %eax, %esi
cmovgel %eax, %esi
movl %esi, %eax
movl %eax, (%rdx)
movq (%r12), %rax
movl 0xf2e40(%rax), %r10d
subl %r15d, %r10d
leaq 0xc0(%r13), %rdx
leaq 0xf2e00(%rax), %r8
movl %r10d, 0xf2e40(%rax)
cmpl $0xe, 0xc(%rsp)
jg 0x3dee
subq $0x8, %rsp
leaq 0xf457(%rip), %r11      # 0x131dd
leaq 0xcfa3(%rip), %r14      # 0x10d30
leaq 0xcf51(%rip), %rsi      # 0x10ce5
leaq 0x38(%rsp), %rdi
movq %r13, %rcx
movl %r15d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r14
callq 0x2050
addq $0x20, %rsp
jmp 0x3e0f
leaq 0xc0(%r13), %rdx
addq $0xf2e00, %rcx          # imm = 0xF2E00
leaq 0xcfac(%rip), %rsi      # 0x10d73
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r13, %r8
xorl %eax, %eax
callq 0x2050
movq %r13, %rdi
movl 0x4(%rsp), %esi
movl 0x8(%rsp), %edx
callq 0x3e55
jmp 0x3cc4
subq $0x8, %rsp
leaq 0xcf3b(%rip), %rsi      # 0x10d34
leaq 0x38(%rsp), %rdi
movq %r13, %rcx
movl %r15d, %r9d
xorl %eax, %eax
pushq %r10
callq 0x2050
popq %rcx
popq %rdx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xbd04
movq %r13, %rdi
callq 0x3824
testl %eax, %eax
je 0x3e43
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movl 0x8(%rsp), %edx
callq 0x3e55
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq