// File: /workspace/github_c_makefile_star2_O1/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, 0xc(%rsp)
movq %rdi, %rbx
callq 0xd89c
leaq 0x1d137(%rip), %r12     # 0x21a80
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 0xeeec
callq 0x2080
movq (%r12), %rcx
cltd
idivl 0x282c(%rcx,%rbp)
movl %edx, 0x4(%rsp)
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
cmpl $0xf, %r14d
setl %r13b
callq 0x2080
cltd
idivl 0x8(%r15)
movl %edx, %r15d
movl %r13d, %ecx
shll %cl, %r15d
movq (%r12), %rax
cmpl $0x0, 0x2800(%rax,%rbp)
je 0x4a4d
cmpl $0x0, 0xc(%rsp)
je 0x4bfe
cmpl $0x0, 0xf2e64(%rax)
je 0x4bfe
testl %edx, %edx
je 0x4a85
movq %r12, %r13
cmpl $0x0, 0xf2e48(%rax)
movq %rbx, %r12
je 0x4abd
callq 0x2080
movq (%r13), %rcx
cltd
idivl 0xf2e48(%rcx)
jmp 0x4abf
leaq 0xc0(%rbx), %rdx
leaq 0xd1df(%rip), %rsi      # 0x11c3a
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 0xcd1c
jmp 0x4bfe
leaq 0xc0(%rbx), %rdx
leaq 0xd21e(%rip), %rsi      # 0x11cb1
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xcd1c
jmp 0x4bee
xorl %edx, %edx
subl %edx, %r15d
jle 0x4af9
movq (%r13), %rax
movl 0xf2e44(%rax), %ecx
cmpl %ecx, %r15d
jge 0x4b43
movl 0xf2e40(%rax), %edx
movl 0x280c(%rax,%rbp), %esi
leal (%rsi,%r15), %edi
addl %edx, %esi
cmpl %edx, %edi
cmovlel %edi, %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
movl %esi, 0x280c(%rax,%rbp)
jmp 0x4b4a
leaq 0xc0(%r12), %rdx
movl $0xf2e00, %ecx          # imm = 0xF2E00
addq (%r13), %rcx
leaq 0xd262(%rip), %rsi      # 0x11d73
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r12, %r8
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
movl 0x4(%rsp), %esi
movl 0x8(%rsp), %edx
callq 0x4c10
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
jmp 0x4a78
movl %ecx, 0x280c(%rax,%rbp)
movq (%r13), %rax
movl 0xf2e40(%rax), %r10d
subl %r15d, %r10d
leaq 0xc0(%r12), %rdx
leaq 0xf2e00(%rax), %r8
movl %r10d, 0xf2e40(%rax)
subq $0x8, %rsp
cmpl $0xe, %r14d
jg 0x4bab
leaq 0xf65e(%rip), %r11      # 0x141dd
leaq 0xd1aa(%rip), %r14      # 0x11d30
leaq 0xd158(%rip), %rsi      # 0x11ce5
leaq 0x38(%rsp), %rdi
movq %r12, %rcx
movl %r15d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r14
callq 0x2050
addq $0x20, %rsp
jmp 0x4bca
leaq 0xd182(%rip), %rsi      # 0x11d34
leaq 0x38(%rsp), %rdi
movq %r12, %rcx
movl %r15d, %r9d
xorl %eax, %eax
pushq %r10
callq 0x2050
addq $0x10, %rsp
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xcd1c
movq %r12, %rdi
callq 0x4564
testl %eax, %eax
je 0x4bfe
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movl 0x8(%rsp), %edx
callq 0x4c10
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq