// File: /workspace/github_c_makefile_star2_O3/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 0xda6c
leaq 0x1d0cd(%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 0xf1cc
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), %rcx
cmpl $0x0, 0x2800(%rcx,%rbp)
je 0x4ab7
cmpl $0x0, 0xc(%rsp)
je 0x4c60
cmpl $0x0, 0xf2e64(%rcx)
je 0x4c60
testl %edx, %edx
je 0x4aef
movq %r12, %r13
cmpl $0x0, 0xf2e48(%rcx)
movq %rbx, %r12
je 0x4b27
callq 0x2080
movq (%r13), %rcx
cltd
idivl 0xf2e48(%rcx)
jmp 0x4b29
leaq 0xc0(%rbx), %rdx
leaq 0xd1ad(%rip), %rsi      # 0x11c72
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 0xcefe
jmp 0x4c60
leaq 0xc0(%rbx), %rdx
leaq 0xd1ec(%rip), %rsi      # 0x11ce9
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 0xcefe
jmp 0x4c50
xorl %edx, %edx
subl %edx, %r15d
jle 0x4bc5
movl 0xf2e44(%rcx), %eax
leaq (%rcx,%rbp), %rdx
addq $0x280c, %rdx           # imm = 0x280C
cmpl %eax, %r15d
jge 0x4b62
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 (%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 0x4c0d
leaq 0xf67c(%rip), %r11      # 0x14215
leaq 0xd1c8(%rip), %r14      # 0x11d68
leaq 0xd176(%rip), %rsi      # 0x11d1d
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 0x4c2c
leaq 0xc0(%r12), %rdx
addq $0xf2e00, %rcx          # imm = 0xF2E00
leaq 0xd1d0(%rip), %rsi      # 0x11dab
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 0x4c72
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
jmp 0x4ae2
leaq 0xd158(%rip), %rsi      # 0x11d6c
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 0xcefe
movq %r12, %rdi
callq 0x45bc
testl %eax, %eax
je 0x4c60
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movl 0x8(%rsp), %edx
callq 0x4c72
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq