// File: /workspace/github_c_makefile_star2_O3/Ziddykins[P]Dawn/src/items.c
// Function: generate_drop
void generate_drop(struct Message *message) {
    char out[MAX_MESSAGE_BUFFER];
    const char *rarity[]  = {NULL, "Common ", "Uncommon ", "Rare ", "Mythical ", "Epic ", "Legendary ", "Godly ", "Forsaken "};
    const char *weapons[] = {"Sword", "Mace", "Scimitar", "Axe", "Club", "Staff", "Wand", "Pike", "Polearm", "Lance", "Spear", "Floppy Dildo"};
    const char *shields[] = {"Tower Shield", "Round Shield", "Small Shield", "Large Shield", "Medium Shield"};
    const char *armor[]   = {"Leather Armor", "Breastplate Armor", "Fullplate Armor", "Chainmail Armor", "Cloth Armor"};
    char item_name[200];
    item_name[0] = '\0';

    float rarity_chance = rand()/(RAND_MAX/100.0f);
    int type_chance     = rand()%MAX_ITEM_TYPE;
    int p_index = get_pindex(message->sender_nick);
    int drop_level = dawn->global_monster.drop_level;
    int str, def, intel, mdef, hp;
    str = def = intel = mdef = hp = 0;

    if (dawn->players[p_index].available_slots == 0) {
        sprintf(out, "PRIVMSG %s :%s has found an item, but has no more room in his inventory!\r\n",
                message->receiver, message->sender_nick);
        add_msg(out, strlen(out));
        return;
    }

    struct Inventory item_dropped;
    item_dropped.type = type_chance;

    if (rarity_chance <= COMMON) {
        item_dropped.rarity = 1;
    } else if (rarity_chance <= UNCOMMON) {
        strcat(item_name, IRC_CYAN);
        item_dropped.rarity = 2;
    } else if (rarity_chance <= RARE) {
        strcat(item_name, IRC_ORANGE);
        item_dropped.rarity = 3;
    } else if (rarity_chance <= MYTHICAL) {
        strcat(item_name, IRC_PURPLE);
        item_dropped.rarity = 4;
    } else if (rarity_chance <= EPIC) {
        strcat(item_name, IRC_DBLUE);
        item_dropped.rarity = 5;
    } else if (rarity_chance <= LEGENDARY) {
        strcat(item_name, IRC_BROWN);
        item_dropped.rarity = 6;
    } else if (rarity_chance <= GODLY) {
        strcat(item_name, IRC_BOLD);
        strcat(item_name, IRC_PINK);
        item_dropped.rarity = 7;
    } else if (rarity_chance <= FORSAKEN) {
        strcat(item_name, IRC_BOLD);
        strcat(item_name, IRC_UNDRL);
        strcat(item_name, IRC_RED);
        item_dropped.rarity = 8;
    } else {
        enum material {WOOD, LEATHER, STONE, ORE, BRONZE, MAIL, STEEL, DIAMOND};
        char *mat_types[] = {"wood", "leather", "stone", "ore", "bronze", "mail", "steel", "diamond"};
        char plural[7], mat_name[10];
        int type, amount;

        if (drop_level < 4)       type = rand() % 2;
        else if (drop_level < 6)  type = rand() % 3;
        else if (drop_level < 8)  type = rand() % 4;
        else if (drop_level < 10) type = rand() % 6;
        else if (drop_level < 11) type = rand() % 7;
        else                      type = rand() % MAX_MATERIAL_TYPE;

        amount = 1 + rand() % 4;
        amount > 1 ? strcpy(plural, "scraps") : strcpy(plural, "scrap");
        strcpy(mat_name, mat_types[type]);
        dawn->players[p_index].materials[type] += amount;
/*
        switch (type) {
            case WOOD:    dawn->players[p_index].wood    += amount; break;
            case LEATHER: dawn->players[p_index].leather += amount; break;
            case STONE:   dawn->players[p_index].stone   += amount; break;
            case ORE:     dawn->players[p_index].ore     += amount; break;
            case BRONZE:  dawn->players[p_index].bronze  += amount; break;
            case MAIL:    dawn->players[p_index].mail    += amount; break;
            case STEEL:   dawn->players[p_index].steel   += amount; break;
            case DIAMOND: dawn->players[p_index].diamond += amount; break;
        }
*/
        sprintf(out, "PRIVMSG %s :%s finds %d %s %s\r\n",
                message->receiver, message->sender_nick, amount, mat_name, plural);
        add_msg(out, strlen(out));

        return;
    }

    strcat(item_name, rarity[item_dropped.rarity]);

    switch (type_chance) {
        case 0:
        {
            char weapon_type[48];
            str = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            intel = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            def   = 1 + rand() % str;
            mdef  = 1 + rand() % str;
            strcpy(weapon_type, weapons[rand()%MAX_WEAPON_TYPE]);
            strcat(item_name, weapon_type);
            break;
        }
        case 1:
        {
            char shield_type[48];
            def = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            intel = 1 + rand() % def;
            str   = 1 + rand() % def;
            mdef = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            strcpy(shield_type, shields[rand()%MAX_SHIELD_TYPE]);
            strcat(item_name, shield_type);
            break;
        }
        case 2:
        {
            char armor_type[48];
            def = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            intel = 1 + rand() % def;
            str   = 1 + rand() % def;
            mdef = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            strcpy(armor_type, armor[rand()%MAX_ARMOR_TYPE]);
            strcat(item_name, armor_type);
            break;
        }
        default:
            PRINTERR("ITEM ERROR")
    }

    if (item_dropped.rarity >= 3) {
        hp = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
    }

        
    strcat(item_name, IRC_NORMAL);
    //strcpy(item_name, item_name); programming drunk. not even once.
    size_t len = strlen(item_name);
    item_name[len+1] = '\0';

    strcpy(item_dropped.name, item_name);

    item_dropped.attr_strength     = str;
    item_dropped.attr_defense      = def;
    item_dropped.attr_intelligence = intel;
    item_dropped.attr_mdef         = mdef;
    item_dropped.attr_health = hp;
    item_dropped.req_level = 1;
    item_dropped.weight = 5;
    item_dropped.socket_one = 0;
    item_dropped.socket_two = 0;
    item_dropped.socket_three = 0;
    item_dropped.attr_mana = 0;

    //see inventory.h
    item_dropped.bitfield &= ~(1 << RUSTED);     //Not rusted
    item_dropped.bitfield &= ~(1 << EQUIPPED);   //Not equipped
    item_dropped.bitfield |=  (1 << EQUIPPABLE); //Equippable
    item_dropped.bitfield |=  (1 << NEW);        //New
    item_dropped.bitfield &= ~(1 << FAVORITE);  //Not favorite


    for (int i=0; i<MAX_INVENTORY_SLOTS; i++) {
        printf("%d. '%s'\n", i, dawn->players[p_index].inventory[i].name);
        if (strlen(dawn->players[p_index].inventory[i].name) < 2) {
            if (dawn->players[p_index].available_slots > 0) {
                dawn->players[p_index].inventory[i] = item_dropped;
                dawn->players[p_index].available_slots--;
                printf("%s got %s %d slots left\n", message->sender_nick, item_dropped.name, dawn->players[p_index].available_slots);
                break;
            } else {
                sprintf(out, "PRIVMSG %s :%s, you have no room in your inventory!\r\n",
                        message->receiver, message->sender_nick);
                add_msg(out, strlen(out));
                return;
            }
        } else {
            printf("wat?\n");
        }
    }

    sprintf(out, "PRIVMSG %s :%s has found a %s [S: %u - D: %u I: %u - MD: %u] on the corpse of the monster!\r\n",
            message->receiver, message->sender_nick, item_name, str, def, intel, mdef);
    add_msg(out, strlen(out));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
movq %rdi, %r14
movb $0x0, 0x50(%rsp)
callq 0x2080
movl %eax, %ebx
callq 0x2080
movl %eax, %ebp
movq %r14, %rdi
callq 0xda6c
leaq 0x19a8a(%rip), %rcx     # 0x21a80
movq (%rcx), %rcx
cltq
imulq $0x26a8, %rax, %r13     # imm = 0x26A8
cmpl $0x0, 0x2818(%rcx,%r13)
je 0x80f6
movq %r14, 0x10(%rsp)
cvtsi2ss %ebx, %xmm0
divss 0xc316(%rip), %xmm0     # 0x14338
movslq %ebp, %rbx
imulq $0x55555556, %rbx, %rax # imm = 0x55555556
movq %rax, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rax
addl %edx, %eax
leal (%rax,%rax,2), %eax
subl %eax, %ebx
movl 0xf2e60(%rcx), %r15d
movl %ebx, 0x1b0(%rsp)
movss 0xc2e8(%rip), %xmm1     # 0x1433c
ucomiss %xmm0, %xmm1
jae 0x8117
movss 0xc2db(%rip), %xmm1     # 0x14340
ucomiss %xmm0, %xmm1
setb %r14b
jae 0x8124
movss 0xc2ca(%rip), %xmm1     # 0x14344
ucomiss %xmm0, %xmm1
jae 0x8134
movss 0xc2bd(%rip), %xmm1     # 0x14348
ucomiss %xmm0, %xmm1
jae 0x8144
movss 0xc2b0(%rip), %xmm1     # 0x1434c
ucomiss %xmm0, %xmm1
jae 0x8151
movss 0xc2a3(%rip), %xmm1     # 0x14350
ucomiss %xmm0, %xmm1
jae 0x815e
movss 0xc296(%rip), %xmm1     # 0x14354
ucomiss %xmm0, %xmm1
jae 0x816b
movss 0xc289(%rip), %xmm1     # 0x14358
ucomiss %xmm0, %xmm1
jae 0x8185
cmpl $0x3, %r15d
jg 0x8677
callq 0x2080
movl %eax, %ebx
shrl $0x1f, %eax
addl %ebx, %eax
andl $-0x2, %eax
jmp 0x86ba
leaq 0xc0(%r14), %rdx
leaq 0xc38c(%rip), %rsi      # 0x14490
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rcx
jmp 0x8617
movl $0x1, %ebp
xorl %r14d, %r14d
jmp 0x81d5
movl $0x2, %ebp
movl $0x303103, %r12d        # imm = 0x303103
jmp 0x81c6
movl $0x3, %ebp
movl $0x373003, %r12d        # imm = 0x373003
jmp 0x81c6
movl $0x4, %ebp
movl $0x363003, %r12d        # imm = 0x363003
jmp 0x81c6
movl $0x5, %ebp
movl $0x323003, %r12d        # imm = 0x323003
jmp 0x81c6
movl $0x6, %ebp
movl $0x353003, %r12d        # imm = 0x353003
jmp 0x81c6
movl %r14d, 0x18(%rsp)
movq %r13, %r14
movl $0x7, %ebp
movl $0x333103, %r12d        # imm = 0x333103
movw $0x2, %r13w
jmp 0x81ae
movl %r14d, 0x18(%rsp)
movq %r13, %r14
leaq 0x50(%rsp), %rdi
callq 0x20c0
movw $0x2, 0x50(%rsp,%rax)
movl $0x8, %ebp
movl $0x343003, %r12d        # imm = 0x343003
movw $0x1f, %r13w
leaq 0x50(%rsp), %rdi
callq 0x20c0
movw %r13w, 0x50(%rsp,%rax)
movq %r14, %r13
movl 0x18(%rsp), %r14d
leaq 0x50(%rsp), %rdi
callq 0x20c0
movl %r12d, 0x50(%rsp,%rax)
movl %ebp, 0x1b8(%rsp)
movl %ebp, %eax
leaq 0x16b6b(%rip), %rcx     # 0x1ed50
movq (%rcx,%rax,8), %rsi
leaq 0x50(%rsp), %rdi
callq 0x2410
cmpl $0x2, %ebx
je 0x82be
cmpl $0x1, %ebx
je 0x8365
testl %ebx, %ebx
jne 0x8643
callq 0x2080
leaq 0x19867(%rip), %rcx     # 0x21a80
movl %r14d, %r12d
movq %rcx, %r14
movq (%rcx), %rcx
leal (%r15,%r15), %ebx
movl 0x2808(%rcx,%r13), %ecx
imull %ebx, %ecx
imull %ebp, %ecx
cltd
idivl %ecx
movq %r15, 0x38(%rsp)
movl %edx, %r15d
incl %r15d
callq 0x2080
movq (%r14), %rcx
movl %r12d, %r14d
imull 0x2808(%rcx,%r13), %ebx
imull %ebp, %ebx
cltd
idivl %ebx
incl %edx
movq %rdx, 0x28(%rsp)
callq 0x2080
cltd
idivl %r15d
incl %edx
movq %rdx, 0x18(%rsp)
callq 0x2080
cltd
movl %r15d, 0xc(%rsp)
idivl %r15d
movq 0x38(%rsp), %r15
incl %edx
movq %rdx, 0x20(%rsp)
callq 0x2080
cltq
imulq $0x2aaaaaab, %rax, %rcx # imm = 0x2AAAAAAB
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x21, %rcx
addl %edx, %ecx
shll $0x2, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
cltq
leaq 0xc437(%rip), %rcx      # 0x146f0
jmp 0x8407
callq 0x2080
movl %r14d, %r12d
leaq 0x197b3(%rip), %r14     # 0x21a80
movq (%r14), %rcx
leal (%r15,%r15), %ebx
movl 0x2808(%rcx,%r13), %ecx
imull %ebx, %ecx
imull %ebp, %ecx
cltd
idivl %ecx
movl %edx, %r14d
incl %r14d
callq 0x2080
cltd
idivl %r14d
incl %edx
movq %rdx, 0x28(%rsp)
callq 0x2080
cltd
movq %r14, 0x18(%rsp)
idivl %r14d
incl %edx
movl %edx, 0xc(%rsp)
callq 0x2080
leaq 0x19765(%rip), %rcx     # 0x21a80
movq (%rcx), %rcx
movl %r12d, %r14d
imull 0x2808(%rcx,%r13), %ebx
imull %ebp, %ebx
cltd
idivl %ebx
incl %edx
movq %rdx, 0x20(%rsp)
callq 0x2080
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
cltq
leaq 0xc3d4(%rip), %rcx      # 0x14734
jmp 0x8407
callq 0x2080
movl %r14d, %r12d
leaq 0x1970c(%rip), %r14     # 0x21a80
movq (%r14), %rcx
leal (%r15,%r15), %ebx
movl 0x2808(%rcx,%r13), %ecx
imull %ebx, %ecx
imull %ebp, %ecx
cltd
idivl %ecx
movl %edx, %r14d
incl %r14d
callq 0x2080
cltd
idivl %r14d
incl %edx
movq %rdx, 0x28(%rsp)
callq 0x2080
cltd
movq %r14, 0x18(%rsp)
idivl %r14d
incl %edx
movl %edx, 0xc(%rsp)
callq 0x2080
leaq 0x196be(%rip), %rcx     # 0x21a80
movq (%rcx), %rcx
movl %r12d, %r14d
imull 0x2808(%rcx,%r13), %ebx
imull %ebp, %ebx
cltd
idivl %ebx
incl %edx
movq %rdx, 0x20(%rsp)
callq 0x2080
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
cltq
leaq 0xc319(%rip), %rcx      # 0x14720
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rdi
callq 0x21c0
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x2410
xorl %r12d, %r12d
movl $0x0, %ebx
testb %r14b, %r14b
je 0x845f
callq 0x2080
leaq 0x1963c(%rip), %rcx     # 0x21a80
movq (%rcx), %rcx
imull 0x2808(%rcx,%r13), %r15d
imull %ebp, %r15d
addl %r15d, %r15d
cltd
idivl %r15d
movl %edx, %ebx
incl %ebx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movw $0xf, 0x50(%rsp,%rax)
movq %r14, %rdi
callq 0x20c0
movb $0x0, 0x1(%rax,%r14)
leaq 0x124(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x21c0
movl 0xc(%rsp), %eax
movl %eax, 0x70(%r15)
movq 0x18(%rsp), %rax
movl %eax, 0x68(%r15)
movq 0x28(%rsp), %rax
movl %eax, 0x6c(%r15)
movq 0x20(%rsp), %rax
movl %eax, 0x74(%r15)
movl %ebx, 0x64(%r15)
movsd 0xbe9d(%rip), %xmm0     # 0x14360
movups %xmm0, 0x78(%r15)
movl %r12d, 0x88(%r15)
movl %r12d, 0x90(%r15)
movb 0x98(%r15), %al
andb $-0x20, %al
orb $0x9, %al
movb %al, 0x98(%r15)
movq %r13, 0x38(%rsp)
addq $0x330, %r13            # imm = 0x330
leaq 0xc06d(%rip), %r12      # 0x14568
leaq 0xc1e6(%rip), %rbx      # 0x146e8
xorl %r15d, %r15d
leaq 0x19574(%rip), %r14     # 0x21a80
movq (%r14), %rdx
addq %r13, %rdx
movq %r12, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x2040
movq (%r14), %r14
leaq (%r14,%r13), %rbp
movq %rbp, %rdi
callq 0x20c0
cmpq $0x1, %rax
jbe 0x854e
movq %rbx, %rdi
callq 0x2330
incq %r15
addq $0x9c, %r13
cmpq $0x19, %r15
jne 0x8505
jmp 0x85b4
movq 0x38(%rsp), %r15
leaq (%r14,%r15), %rax
addq $0x1a0, %rax            # imm = 0x1A0
cmpl $0x0, 0x2678(%rax)
jle 0x85f9
leaq 0x124(%rsp), %rbx
movl $0x9c, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2170
leaq 0x194f7(%rip), %rax     # 0x21a80
movq (%rax), %rax
movl 0x2818(%rax,%r15), %ecx
decl %ecx
movl %ecx, 0x2818(%rax,%r15)
leaq 0xbfcd(%rip), %rdi      # 0x14572
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2040
movq 0x10(%rsp), %rcx
leaq 0xc0(%rcx), %rdx
subq $0x8, %rsp
leaq 0xbff6(%rip), %rsi      # 0x145c1
leaq 0x1c8(%rsp), %rbx
leaq 0x58(%rsp), %r8
movq %rbx, %rdi
movl 0x14(%rsp), %r9d
xorl %eax, %eax
pushq 0x28(%rsp)
pushq 0x38(%rsp)
pushq 0x30(%rsp)
callq 0x2050
addq $0x20, %rsp
jmp 0x861e
movq 0x10(%rsp), %rcx
leaq 0xc0(%rcx), %rdx
leaq 0xbf7f(%rip), %rsi      # 0x1458b
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2050
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xcefe
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbeef(%rip), %rdi      # 0x14539
callq 0x2190
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0x842b
cmpl $0x5, %r15d
ja 0x869e
callq 0x2080
movslq %eax, %rbx
imulq $0x55555556, %rbx, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
leal (%rax,%rax,2), %eax
jmp 0x86ba
cmpl $0x7, %r15d
ja 0x8771
callq 0x2080
movl %eax, %ebx
leal 0x3(%rbx), %eax
testl %ebx, %ebx
cmovnsl %ebx, %eax
andl $-0x4, %eax
subl %eax, %ebx
movq 0x10(%rsp), %r15
callq 0x2080
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
movl %eax, %edx
subl %ecx, %edx
negl %ecx
leal (%rax,%rcx), %ebp
incl %ebp
testl %edx, %edx
jle 0x86ea
movl $0x737061, 0x33(%rsp)   # imm = 0x737061
jmp 0x86f1
movw $0x70, 0x34(%rsp)
movl $0x61726373, 0x30(%rsp) # imm = 0x61726373
movslq %ebx, %r14
leaq 0xc045(%rip), %rax      # 0x14748
movslq (%rax,%r14,4), %rsi
addq %rax, %rsi
leaq 0x46(%rsp), %rbx
movq %rbx, %rdi
callq 0x21c0
movslq %ebp, %r8
leaq 0x1935f(%rip), %rax     # 0x21a80
addq (%rax), %r13
addq %r8, 0x2798(%r13,%r14,8)
leaq 0xc0(%r15), %rdx
subq $0x8, %rsp
leaq 0x38(%rsp), %r10
leaq 0xbdd6(%rip), %rsi      # 0x14519
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rcx
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x2050
addq $0x10, %rsp
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
jmp 0x8629
cmpl $0x9, %r15d
ja 0x879a
callq 0x2080
movslq %eax, %rbx
imulq $0x2aaaaaab, %rbx, %rax # imm = 0x2AAAAAAB
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
addl %eax, %eax
jmp 0x8699
callq 0x2080
cmpl $0xa, %r15d
jne 0x87cf
movslq %eax, %rcx
imulq $-0x6db6db6d, %rcx, %rbx # imm = 0x92492493
shrq $0x20, %rbx
addl %eax, %ebx
movl %ebx, %ecx
shrl $0x1f, %ecx
sarl $0x2, %ebx
addl %ecx, %ebx
leal (,%rbx,8), %ecx
subl %ecx, %ebx
addl %eax, %ebx
jmp 0x86bc
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x8, %ecx
subl %ecx, %eax
movl %eax, %ebx
jmp 0x86bc