// File: /workspace/github_cpp_makefile_star2_O3/maclellant[P]ReliableGoBackN/server.cpp
// Function: gremlin(char*, int, int, int)
int gremlin(char *data, int corrupt_chance, int loss_chance, int delay_chance)
{
    if (corrupt_chance < 0)
        corrupt_chance = 0;
    if (corrupt_chance > 100)
        corrupt_chance = 100;
    if (loss_chance < 0)
        loss_chance = 0;
    if (loss_chance > 100)
        loss_chance = 100;
    if (delay_chance < 0)
        delay_chance = 0;
    if (delay_chance > 100)
        delay_chance = 100;
    int corrupt_roll = rand() % 101;
    int loss_roll = rand() % 101;
    int delay_roll = rand() % 101;
    if (loss_roll <= loss_chance) 
    {
        return LOST;
    }
    if (corrupt_roll <= corrupt_chance)
    {
        int num_corrupt = rand() % 101;
        if (num_corrupt <= 70)
        {
            int corrupt_byte = rand() % (PACKET_SIZE - HEADER_SIZE);
            data[corrupt_byte] = ~data[corrupt_byte];
        }
        else if (num_corrupt <= 90)
        {
            int corrupt_byte = rand() % (PACKET_SIZE - HEADER_SIZE);
            data[corrupt_byte] = ~data[corrupt_byte];

            int prev_corrupt = corrupt_byte;
            while (prev_corrupt == corrupt_byte)
            {
                corrupt_byte = rand() % (PACKET_SIZE - HEADER_SIZE);
            }
            data[corrupt_byte] = ~data[corrupt_byte];
        }
        else
        {
            int corrupt_byte = rand() % (PACKET_SIZE - HEADER_SIZE);
            data[corrupt_byte] = ~data[corrupt_byte];

            int prev_corrupt1 = corrupt_byte;
            while (prev_corrupt1 == corrupt_byte)
            {
                corrupt_byte = rand() % (PACKET_SIZE - HEADER_SIZE);
            }
            data[corrupt_byte] = ~data[corrupt_byte];

            int prev_corrupt2 = corrupt_byte;
            while (prev_corrupt1 == corrupt_byte
                || prev_corrupt2 == corrupt_byte)
            {
                corrupt_byte = rand() % (PACKET_SIZE - HEADER_SIZE);
            }
            data[corrupt_byte] = ~data[corrupt_byte];
        }
    }
    if (delay_roll <= delay_chance)
    {
        return DELAYED;
    }
    return FINE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebx
movl %edx, %r12d
xorl %r14d, %r14d
testl %edx, %edx
cmovlel %r14d, %r12d
movl $0x64, %r15d
cmpl %r15d, %r12d
cmovgel %r15d, %r12d
movl %esi, -0x30(%rbp)
testl %ecx, %ecx
cmovlel %r14d, %ebx
movq %rdi, -0x38(%rbp)
cmpl %r15d, %ebx
cmovgel %r15d, %ebx
callq 0x2090
movl %eax, -0x2c(%rbp)
callq 0x2090
movslq %eax, %r13
imulq $0x288df0cb, %r13, %rax # imm = 0x288DF0CB
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x24, %rax
addl %ecx, %eax
imull $0x65, %eax, %eax
subl %eax, %r13d
callq 0x2090
movl %eax, %ecx
movl $0x1, %eax
cmpl %r12d, %r13d
jle 0x3693
movslq %ecx, %r12
imulq $0x288df0cb, %r12, %rax # imm = 0x288DF0CB
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x24, %rax
addl %ecx, %eax
imull $0x65, %eax, %eax
subl %eax, %r12d
movslq -0x2c(%rbp), %rax
imulq $0x288df0cb, %rax, %rcx # imm = 0x288DF0CB
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x24, %rcx
addl %edx, %ecx
imull $0x65, %ecx, %ecx
subl %ecx, %eax
movl -0x30(%rbp), %ecx
testl %ecx, %ecx
cmovgl %ecx, %r14d
cmpl $0x64, %r14d
cmovll %r14d, %r15d
cmpl %r15d, %eax
jg 0x3689
callq 0x2090
movslq %eax, %r14
imulq $0x288df0cb, %r14, %rax # imm = 0x288DF0CB
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x24, %rax
addl %ecx, %eax
imull $0x65, %eax, %eax
subl %eax, %r14d
cmpl $0x46, %r14d
jg 0x35ae
callq 0x2090
cltq
imulq $-0x7e7b7257, %rax, %rcx # imm = 0x81848DA9
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x8, %ecx
addl %edx, %ecx
imull $0x1fa, %ecx, %ecx      # imm = 0x1FA
subl %ecx, %eax
movq -0x38(%rbp), %r13
jmp 0x3682
callq 0x2090
movslq %eax, %r15
imulq $-0x7e7b7257, %r15, %rax # imm = 0x81848DA9
shrq $0x20, %rax
addl %r15d, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x8, %eax
addl %ecx, %eax
imull $0x1fa, %eax, %eax      # imm = 0x1FA
subl %eax, %r15d
movslq %r15d, %rax
movq -0x38(%rbp), %r13
notb (%r13,%rax)
cmpl $0x5b, %r14d
jae 0x3616
callq 0x2090
cltq
imulq $-0x7e7b7257, %rax, %rcx # imm = 0x81848DA9
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x8, %ecx
addl %edx, %ecx
imull $0x1fa, %ecx, %ecx      # imm = 0x1FA
subl %ecx, %eax
cmpl %eax, %r15d
je 0x35e9
jmp 0x3682
callq 0x2090
movslq %eax, %r14
imulq $-0x7e7b7257, %r14, %rax # imm = 0x81848DA9
shrq $0x20, %rax
addl %r14d, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x8, %eax
addl %ecx, %eax
imull $0x1fa, %eax, %eax      # imm = 0x1FA
subl %eax, %r14d
cmpl %r14d, %r15d
je 0x3616
movslq %r14d, %rax
notb (%r13,%rax)
callq 0x2090
cltq
imulq $-0x7e7b7257, %rax, %rcx # imm = 0x81848DA9
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x8, %ecx
addl %edx, %ecx
imull $0x1fa, %ecx, %ecx      # imm = 0x1FA
subl %ecx, %eax
cmpl %eax, %r15d
sete %cl
cmpl %eax, %r14d
sete %dl
orb %cl, %dl
jne 0x364c
cltq
notb (%r13,%rax)
xorl %eax, %eax
cmpl %ebx, %r12d
setle %al
addl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq