// File: /workspace/github_c_makefile_star2_O2/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 0x16965
movq %rsi, %r14
movq %rbx, %rdi
movl $0xabcd0002, %esi       # imm = 0xABCD0002
callq 0x14801
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15da5
movq 0x50(%r14), %rsi
movq %rbx, %rdi
callq 0x15389
movq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0x15389
movq 0x60(%r14), %r15
leaq 0x17315(%rip), %rax     # 0x2dc50
addq $0x50, %rax
cmpq %rax, %r15
je 0x16977
testq %r15, %r15
je 0x1697e
leaq 0x16ee0(%rip), %rax     # 0x2d830
movq (%rax), %rdi
movq %r15, %rsi
callq 0x1685e
testl %eax, %eax
js 0x16984
movl %eax, %ebp
pushq $0x1
jmp 0x169a2
movq %rbx, %rdi
movl $0xdead0002, %esi       # imm = 0xDEAD0002
callq 0x14801
jmp 0x16a46
pushq $0x1
popq %rbp
xorl %esi, %esi
jmp 0x169a3
xorl %esi, %esi
xorl %ebp, %ebp
jmp 0x169a3
leaq 0x16ead(%rip), %rax     # 0x2d838
movq (%rax), %rdi
movq %r15, %rsi
callq 0x1687f
testl %eax, %eax
js 0x16a5d
movl %eax, %ebp
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x14801
movq %rbx, %rdi
movl %ebp, %esi
callq 0x14801
movq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0x16571
movq 0x70(%r14), %rsi
movq %rbx, %rdi
callq 0x1589b
movq 0x78(%r14), %rsi
movq %rbx, %rdi
callq 0x149e9
movl 0x80(%r14), %esi
movq %rbx, %rdi
callq 0x14801
movl 0x84(%r14), %esi
movq %rbx, %rdi
callq 0x14801
movl 0x88(%r14), %esi
movq %rbx, %rdi
callq 0x14801
movzbl 0x8c(%r14), %esi
movq %rbx, %rdi
callq 0x147df
movsbl 0x8d(%r14), %esi
movq %rbx, %rdi
callq 0x147bd
movsbl 0x8e(%r14), %esi
movq %rbx, %rdi
callq 0x147bd
movsbl 0x8f(%r14), %esi
movq %rbx, %rdi
callq 0x147bd
xorl %eax, %eax
cmpl $0x0, 0x1ca25(%rip)     # 0x33474
sete %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16e3c(%rip), %rdi     # 0x2d8a0
pushq $0x9
popq %rdx
movq %r15, %rsi
callq 0x168ca
xorl %ecx, %ecx
testl %eax, %eax
setns %cl
leal (%rcx,%rcx,2), %esi
pushq $0x1
popq %rbp
cmovnsl %eax, %ebp
jmp 0x169a3