// File: /workspace/github_c_makefile_star2_O1/ajpaulson[P]srogue11/state.c
// Function: rs_write_thing
int rs_write_thing(FILE *savef, struct thing *t)
{
    int i = -1;
    
    if (t == NULL)
    {
        rs_write_int(savef, RSID_THING_NULL);
        return(WRITESTAT);
    }
    
    rs_write_int(savef, RSID_THING);

    rs_write_stats(savef, &t->t_stats);
    rs_write_coord(savef, t->t_pos);
    rs_write_coord(savef, t->t_oldpos);

    /* 
        t_dest can be:
        0,0: NULL
        0,1: location of hero
        0,3: global coord 'delta'
        1,i: location of a thing (monster)
        2,i: location of an object
        3,i: location of gold in a room

        We need to remember what we are chasing rather than 
        the current location of what we are chasing.
    */

    if (t->t_dest == &hero)
    {
        rs_write_int(savef,0);
        rs_write_int(savef,1);
    }
    else if (t->t_dest != NULL)
    {
        i = find_thing_coord(mlist, t->t_dest);
            
        if (i >=0 )
        {
            rs_write_int(savef,1);
            rs_write_int(savef,i);
        }
        else
        {
            i = find_object_coord(lvl_obj, t->t_dest);
            
            if (i >= 0)
            {
                rs_write_int(savef,2);
                rs_write_int(savef,i);
            }
            else
            {
                i = find_room_coord(rooms, t->t_dest, MAXROOMS);
        
                if (i >= 0) 
                {
                    rs_write_int(savef,3);
                    rs_write_int(savef,i);
                }
                else 
                {
                    rs_write_int(savef, 0);
                    rs_write_int(savef,1); /* chase the hero anyway */
                }
            }
        }
    }
    else
    {
        rs_write_int(savef,0);
        rs_write_int(savef,0);
    }
    
    rs_write_object_list(savef, t->t_pack);
    rs_write_room_reference(savef, t->t_room);
    rs_write_long(savef, t->t_flags);
    rs_write_int(savef, t->t_indx);
    rs_write_int(savef, t->t_nomove);
    rs_write_int(savef, t->t_nocmd);
    rs_write_boolean(savef, t->t_turn);
    rs_write_char(savef, t->t_type);
    rs_write_char(savef, t->t_disguise);
    rs_write_char(savef, t->t_oldch);
    
    return(WRITESTAT);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
testq %rsi, %rsi
je 0x17e91
movq %rsi, %r14
movq %rbx, %rdi
movl $0xabcd0002, %esi       # imm = 0xABCD0002
callq 0x15842
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17155
movq 0x50(%r14), %rsi
movq %rsi, %r15
shrq $0x20, %r15
movq %rbx, %rdi
callq 0x15842
movq %rbx, %rdi
movl %r15d, %esi
callq 0x15842
movq 0x58(%r14), %rsi
movq %rsi, %r15
shrq $0x20, %r15
movq %rbx, %rdi
callq 0x15842
movq %rbx, %rdi
movl %r15d, %esi
callq 0x15842
movq 0x60(%r14), %rax
leaq 0x15e0c(%rip), %rcx     # 0x2dc60
addq $0x50, %rcx
cmpq %rcx, %rax
je 0x17ea3
testq %rax, %rax
je 0x17eac
leaq 0x159d7(%rip), %rcx     # 0x2d840
movq (%rcx), %rcx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x17eb4
xorl %edx, %edx
movq 0x10(%rcx), %rsi
addq $0x50, %rsi
cmpq %rax, %rsi
je 0x17eb2
incl %edx
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x17e78
jmp 0x17eb4
movq %rbx, %rdi
movl $0xdead0002, %esi       # imm = 0xDEAD0002
callq 0x15842
jmp 0x18016
movl $0x1, %ebp
xorl %esi, %esi
jmp 0x17f26
xorl %esi, %esi
xorl %ebp, %ebp
jmp 0x17f26
movl %edx, %ebp
movl $0x1, %esi
testl %ebp, %ebp
jns 0x17f26
leaq 0x15984(%rip), %rcx     # 0x2d848
movq (%rcx), %rcx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x17ee7
xorl %edx, %edx
cmpq %rax, 0x10(%rcx)
je 0x17ee5
incl %edx
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x17ed3
jmp 0x17ee7
movl %edx, %ebp
movl $0x2, %esi
testl %ebp, %ebp
jns 0x17f26
leaq 0x159b9(%rip), %rdx     # 0x2d8b0
addq $-0x58, %rdx
xorl %ecx, %ecx
addq $0x68, %rdx
cmpq %rax, %rdx
je 0x17f14
incq %rcx
cmpq $0x9, %rcx
jne 0x17efd
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
testl %ecx, %ecx
setns %al
leal (%rax,%rax,2), %esi
movl $0x1, %ebp
cmovnsl %ecx, %ebp
movq %rbx, %rdi
callq 0x15842
movq %rbx, %rdi
movl %ebp, %esi
callq 0x15842
movq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0x179ea
movq 0x70(%r14), %rax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x1595c(%rip), %rcx     # 0x2d8b0
xorl %edx, %edx
cmpq %rax, %rcx
cmovel %edx, %esi
incq %rdx
addq $0x68, %rcx
cmpq $0x9, %rdx
jne 0x17f56
movq %rbx, %rdi
callq 0x15842
movq 0x78(%r14), %rsi
movq %rbx, %rdi
callq 0x15a6b
movl 0x80(%r14), %esi
movq %rbx, %rdi
callq 0x15842
movl 0x84(%r14), %esi
movq %rbx, %rdi
callq 0x15842
movl 0x88(%r14), %esi
movq %rbx, %rdi
callq 0x15842
movb 0x8c(%r14), %al
leaq 0x4(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x157b1
movb 0x8d(%r14), %al
leaq 0x5(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x157b1
movb 0x8e(%r14), %al
leaq 0x6(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x157b1
movb 0x8f(%r14), %al
leaq 0x7(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x157b1
xorl %eax, %eax
cmpl $0x0, 0x1b465(%rip)     # 0x33484
sete %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq