// File: /workspace/github_c_makefile_star2_O1/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 0xd89c
movl %eax, %r12d
leaq 0x8d42(%rip), %rsi      # 0x144ab
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb805
leaq 0x8d30(%rip), %rsi      # 0x144b0
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb809
leaq 0x8d2b(%rip), %rsi      # 0x144be
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb810
leaq 0x8d12(%rip), %rsi      # 0x144b8
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb817
leaq 0x8d09(%rip), %rsi      # 0x144c2
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb81e
leaq 0x8cfd(%rip), %rsi      # 0x144c9
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb825
leaq 0x8cef(%rip), %rsi      # 0x144ce
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
je 0xb82c
leaq 0x8ce2(%rip), %rsi      # 0x144d4
movq %r14, %rdi
callq 0x22d0
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
orl $0x7, %ecx
jmp 0xb831
xorl %ecx, %ecx
jmp 0xb831
movl $0x1, %ecx
jmp 0xb831
movl $0x2, %ecx
jmp 0xb831
movl $0x3, %ecx
jmp 0xb831
movl $0x4, %ecx
jmp 0xb831
movl $0x5, %ecx
jmp 0xb831
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 0xb9a8
leaq 0x1621e(%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 0xb8a7
addq %r15, %r9
cmpq %rsi, %r9
jne 0xb8bc
leaq 0xc0(%rbx), %rdx
leaq 0xacfd(%rip), %rsi      # 0x165a2
jmp 0xb8e2
subq %r15, %r9
jge 0xb8f9
leaq 0xc0(%rbx), %rdx
leaq 0xacb1(%rip), %rsi      # 0x1656b
jmp 0xb8e2
movq 0x2648(%rdi), %r9
movslq 0xf2e78(%r8,%rcx,4), %rsi
imulq %r15, %rsi
subq %rsi, %r9
jge 0xb933
leaq 0xc0(%rbx), %rdx
leaq 0xad26(%rip), %rsi      # 0x16608
leaq 0x10(%rsp), %rdi
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x2050
jmp 0xb990
movslq 0xf2e78(%r8,%rcx,4), %r8
imulq %r15, %r8
movq 0x2648(%rdi), %r10
addq %r8, %r10
cmpq %rsi, %r10
jne 0xb959
leaq 0xc0(%rbx), %rdx
leaq 0xabe4(%rip), %rsi      # 0x16506
leaq 0x10(%rsp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
jmp 0xb990
movq %r9, 0x2648(%rdi)
addq %rdx, %rax
addq %r15, 0x25f8(%rax,%rcx,8)
leaq 0xc0(%rbx), %rdx
movq %rsi, (%rsp)
leaq 0xac78(%rip), %rsi      # 0x165cf
jmp 0xb97b
movq %r9, 0x25f8(%rdi,%rcx,8)
addq %r8, 0x2648(%rax,%rdx)
leaq 0xc0(%rbx), %rdx
movq %r8, (%rsp)
leaq 0xabbc(%rip), %rsi      # 0x16537
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 0xcd1c
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq