// File: /workspace/github_c_makefile_star2_O3/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 0xda6c
movl %eax, %r12d
leaq 0x8c70(%rip), %rsi      # 0x144db
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb907
leaq 0x8c5e(%rip), %rsi      # 0x144e0
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb90b
leaq 0x8c59(%rip), %rsi      # 0x144ee
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb912
leaq 0x8c40(%rip), %rsi      # 0x144e8
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb919
leaq 0x8c37(%rip), %rsi      # 0x144f2
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb920
leaq 0x8c2b(%rip), %rsi      # 0x144f9
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb927
leaq 0x8c1d(%rip), %rsi      # 0x144fe
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb92e
leaq 0x8c10(%rip), %rsi      # 0x14504
movq %r14, %rdi
callq 0x22d0
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
orl $0x7, %ecx
jmp 0xb933
xorl %ecx, %ecx
jmp 0xb933
movl $0x1, %ecx
jmp 0xb933
movl $0x2, %ecx
jmp 0xb933
movl $0x3, %ecx
jmp 0xb933
movl $0x4, %ecx
jmp 0xb933
movl $0x5, %ecx
jmp 0xb933
movl $0x6, %ecx
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 0xbaaa
leaq 0x1611c(%rip), %rax     # 0x21a80
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 0xb9a9
addq %r15, %r9
cmpq %rsi, %r9
jne 0xb9be
leaq 0xc0(%rbx), %rdx
leaq 0xac4b(%rip), %rsi      # 0x165f2
jmp 0xb9e4
subq %r15, %r9
jge 0xb9fb
leaq 0xc0(%rbx), %rdx
leaq 0xabff(%rip), %rsi      # 0x165bb
jmp 0xb9e4
movq 0x2648(%rdi), %r9
movslq 0xf2e78(%r8,%rcx,4), %rsi
imulq %r15, %rsi
subq %rsi, %r9
jge 0xba35
leaq 0xc0(%rbx), %rdx
leaq 0xac74(%rip), %rsi      # 0x16658
leaq 0x10(%rsp), %rdi
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x2050
jmp 0xba92
movslq 0xf2e78(%r8,%rcx,4), %r8
imulq %r15, %r8
movq 0x2648(%rdi), %r10
addq %r8, %r10
cmpq %rsi, %r10
jne 0xba5b
leaq 0xc0(%rbx), %rdx
leaq 0xab32(%rip), %rsi      # 0x16556
leaq 0x10(%rsp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
jmp 0xba92
movq %r9, 0x2648(%rdi)
addq %rdx, %rax
addq %r15, 0x25f8(%rax,%rcx,8)
leaq 0xc0(%rbx), %rdx
movq %rsi, (%rsp)
leaq 0xabc6(%rip), %rsi      # 0x1661f
jmp 0xba7d
movq %r9, 0x25f8(%rdi,%rcx,8)
addq %r8, 0x2648(%rax,%rdx)
leaq 0xc0(%rbx), %rdx
movq %r8, (%rsp)
leaq 0xab0a(%rip), %rsi      # 0x16587
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 0xcefe
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq