// File: /workspace/github_c_makefile_star2_O2/Ziddykins[P]Dawn/src/market.c
// Function: market_buysell
void market_buysell(struct Message *m, int buysell, char *which, long amount) {
    int pindex = get_pindex(m->sender_nick);
    int material = get_itemID(which);
    char out[MAX_MESSAGE_BUFFER];

    if (material == -1 || amount < 1 || amount >= LONG_MAX) return;

    if (buysell == 0) {
        //sell
        if (dawn->players[pindex].materials[material] >= amount) {
            if (dawn->players[pindex].gold + dawn->market.materials[material] * amount >= LONG_MAX) {
                sprintf(out, "PRIVMSG %s :%s, you can't carry that much gold\r\n", m->receiver, m->sender_nick);
                add_msg(out, strlen(out));
                return;
            }
            dawn->players[pindex].materials[material] -= amount;
            dawn->players[pindex].gold += dawn->market.materials[material] * amount;
            sprintf(out, "PRIVMSG %s :%s, you have sold %ld %s for %ld gold\r\n",
                    m->receiver, m->sender_nick, amount, which, dawn->market.materials[material] * amount);

        } else {
            sprintf(out, "PRIVMSG %s :%s, you do not have enough %s to do that\r\n",
                    m->receiver, m->sender_nick, which);
        }
    } else {
        //buy
        if (dawn->players[pindex].materials[material] + amount >= LONG_MAX) {
            sprintf(out, "PRIVMSG %s :%s, you can't carry anymore %s\r\n", m->receiver, m->sender_nick, which);
            add_msg(out, strlen(out));
            return;
        }
        if (dawn->players[pindex].gold >= dawn->market.materials[material] * amount) {
            dawn->players[pindex].gold -= dawn->market.materials[material] * amount;
            dawn->players[pindex].materials[material] += amount;
            sprintf(out, "PRIVMSG %s :%s, you have purchased %ld %s for %ld gold\r\n",
                    m->receiver, m->sender_nick, amount, which, dawn->market.materials[material] * amount);
        } else {
            sprintf(out, "PRIVMSG %s :%s, you do not have enough gold to purchase that much %s\r\n",
                    m->receiver, m->sender_nick, which);
        }
    }
    add_msg(out, strlen(out));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rcx, %r15
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
callq 0xc860
movl %eax, %r12d
leaq 0x8c58(%rip), %rsi      # 0x134ab
movq %r14, %rdi
callq 0x22e0
testl %eax, %eax
je 0xa8ef
leaq 0x8c46(%rip), %rsi      # 0x134b0
movq %r14, %rdi
callq 0x22e0
testl %eax, %eax
je 0xa8f3
leaq 0x8c41(%rip), %rsi      # 0x134be
movq %r14, %rdi
callq 0x22e0
testl %eax, %eax
je 0xa8f7
leaq 0x8c28(%rip), %rsi      # 0x134b8
movq %r14, %rdi
callq 0x22e0
testl %eax, %eax
je 0xa8fb
leaq 0x8c1f(%rip), %rsi      # 0x134c2
movq %r14, %rdi
callq 0x22e0
testl %eax, %eax
je 0xa8ff
leaq 0x8c13(%rip), %rsi      # 0x134c9
movq %r14, %rdi
callq 0x22e0
testl %eax, %eax
je 0xa903
leaq 0x8c05(%rip), %rsi      # 0x134ce
movq %r14, %rdi
callq 0x22e0
testl %eax, %eax
je 0xa907
leaq 0x8bf8(%rip), %rsi      # 0x134d4
movq %r14, %rdi
callq 0x22e0
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
orl $0x7, %ecx
jmp 0xa90a
xorl %ecx, %ecx
jmp 0xa90a
pushq $0x1
jmp 0xa909
pushq $0x2
jmp 0xa909
pushq $0x3
jmp 0xa909
pushq $0x4
jmp 0xa909
pushq $0x5
jmp 0xa909
pushq $0x6
popq %rcx
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
cmpl $-0x1, %ecx
sete %al
leaq (%r15,%rsi), %rdx
addq $0x2, %rdx
leaq 0x3(%rsi), %rdi
cmpq %rdi, %rdx
setb %dl
orb %al, %dl
jne 0xaa81
leaq 0x15145(%rip), %rax     # 0x1fa80
movq (%rax), %r8
leaq 0x1a0(%r8), %rax
movslq %r12d, %rdx
imulq $0x26a8, %rdx, %rdx     # imm = 0x26A8
leaq (%r8,%rdx), %rdi
addq $0x1a0, %rdi            # imm = 0x1A0
movl %ecx, %ecx
movq 0x25f8(%rdi,%rcx,8), %r9
testl %ebp, %ebp
je 0xa980
addq %r15, %r9
cmpq %rsi, %r9
jne 0xa995
leaq 0xc0(%rbx), %rdx
leaq 0xac24(%rip), %rsi      # 0x155a2
jmp 0xa9bb
subq %r15, %r9
jge 0xa9d2
leaq 0xc0(%rbx), %rdx
leaq 0xabd8(%rip), %rsi      # 0x1556b
jmp 0xa9bb
movq 0x2648(%rdi), %r9
movslq 0xf2e78(%r8,%rcx,4), %rsi
imulq %r15, %rsi
subq %rsi, %r9
jge 0xaa0c
leaq 0xc0(%rbx), %rdx
leaq 0xac4d(%rip), %rsi      # 0x15608
leaq 0x10(%rsp), %rdi
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x2050
jmp 0xaa69
movslq 0xf2e78(%r8,%rcx,4), %r8
imulq %r15, %r8
movq 0x2648(%rdi), %r10
addq %r8, %r10
cmpq %rsi, %r10
jne 0xaa32
leaq 0xc0(%rbx), %rdx
leaq 0xab0b(%rip), %rsi      # 0x15506
leaq 0x10(%rsp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
jmp 0xaa69
movq %r9, 0x2648(%rdi)
addq %rdx, %rax
addq %r15, 0x25f8(%rax,%rcx,8)
leaq 0xc0(%rbx), %rdx
movq %rsi, (%rsp)
leaq 0xab9f(%rip), %rsi      # 0x155cf
jmp 0xaa54
movq %r9, 0x25f8(%rdi,%rcx,8)
addq %r8, 0x2648(%rax,%rdx)
leaq 0xc0(%rbx), %rdx
movq %r8, (%rsp)
leaq 0xaae3(%rip), %rsi      # 0x15537
leaq 0x10(%rsp), %rdi
movq %rbx, %rcx
movq %r15, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x2050
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xbd04
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq