// File: /workspace/github_c_makefile_star2_O3/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 0x17d2c
movq %rsi, %r14
movq %rbx, %rdi
movl $0xabcd0002, %esi       # imm = 0xABCD0002
callq 0x156d6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x16f87
movq 0x50(%r14), %rsi
movq %rsi, %r15
shrq $0x20, %r15
movq %rbx, %rdi
callq 0x156d6
movq %rbx, %rdi
movl %r15d, %esi
callq 0x156d6
movq 0x58(%r14), %rsi
movq %rsi, %r15
shrq $0x20, %r15
movq %rbx, %rdi
callq 0x156d6
movq %rbx, %rdi
movl %r15d, %esi
callq 0x156d6
movq 0x60(%r14), %rax
leaq 0x15fb3(%rip), %rcx     # 0x2dc60
addq $0x50, %rcx
movl $0x1, %ebp
cmpq %rcx, %rax
je 0x17d28
testq %rax, %rax
je 0x17d3e
leaq 0x15b79(%rip), %rcx     # 0x2d840
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x17ce8
xorl %edx, %edx
movq 0x10(%rcx), %rsi
addq $0x50, %rsi
cmpq %rax, %rsi
je 0x17d44
incl %edx
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x17cd1
leaq 0x15b59(%rip), %rcx     # 0x2d848
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x17d09
xorl %edx, %edx
cmpq %rax, 0x10(%rcx)
je 0x17d54
incl %edx
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x17cf9
leaq 0x15ba0(%rip), %rdx     # 0x2d8b0
addq $-0x58, %rdx
xorl %ecx, %ecx
addq $0x68, %rdx
cmpq %rax, %rdx
je 0x17d4b
incq %rcx
cmpq $0x9, %rcx
jne 0x17d16
xorl %esi, %esi
jmp 0x17d5b
movq %rbx, %rdi
movl $0xdead0002, %esi       # imm = 0xDEAD0002
callq 0x156d6
jmp 0x17e4b
xorl %esi, %esi
xorl %ebp, %ebp
jmp 0x17d5b
movl $0x1, %esi
jmp 0x17d59
movl $0x3, %esi
movl %ecx, %ebp
jmp 0x17d5b
movl $0x2, %esi
movl %edx, %ebp
movq %rbx, %rdi
callq 0x156d6
movq %rbx, %rdi
movl %ebp, %esi
callq 0x156d6
movq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0x17840
movq 0x70(%r14), %rax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x15b27(%rip), %rcx     # 0x2d8b0
xorl %edx, %edx
cmpq %rax, %rcx
cmovel %edx, %esi
incq %rdx
addq $0x68, %rcx
cmpq $0x9, %rdx
jne 0x17d8b
movq %rbx, %rdi
callq 0x156d6
movq 0x78(%r14), %rsi
movq %rbx, %rdi
callq 0x158d7
movl 0x80(%r14), %esi
movq %rbx, %rdi
callq 0x156d6
movl 0x84(%r14), %esi
movq %rbx, %rdi
callq 0x156d6
movl 0x88(%r14), %esi
movq %rbx, %rdi
callq 0x156d6
movb 0x8c(%r14), %al
leaq 0x4(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x15645
movb 0x8d(%r14), %al
leaq 0x5(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x15645
movb 0x8e(%r14), %al
leaq 0x6(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x15645
movb 0x8f(%r14), %al
leaq 0x7(%rsp), %rsi
movb %al, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x15645
xorl %eax, %eax
cmpl $0x0, 0x1b630(%rip)     # 0x33484
sete %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq