// File: zgimbutas[P]mwrap/src/mwrap-typecheck.cc
// Function: assign_tinfo(Var*, int)
int assign_tinfo(Var* v, int line)
{
    if (is_scalar_type(v->basetype)) {

        return assign_scalar_tinfo(v, line, VT_scalar, VT_p_scalar, 
                                   VT_r_scalar, VT_array, VT_rarray);

    } else if (is_cscalar_type(v->basetype)) {

        return assign_scalar_tinfo(v, line, VT_cscalar, VT_p_cscalar, 
                                   VT_r_cscalar, VT_carray, VT_unk);

    } else if (is_zscalar_type(v->basetype)) {

        return assign_scalar_tinfo(v, line, VT_zscalar, VT_p_zscalar, 
                                   VT_r_zscalar, VT_zarray, VT_unk);

    } else if (strcmp(v->basetype, "const") == 0) {

        if (v->qual) {
            fprintf(stderr, "Error (%d): ", line);
            fprintf(stderr, "Constant %s cannot have modifiers\n", v->name);
            return 1;
        }
        v->tinfo = VT_const;
        if (v->name[0] == '\'') {
            char* p_out = v->name;
            char* p_in  = v->name;
            for (; *p_in; ++p_in) {
                if (*p_in != '\'')
                    *p_out++ = *p_in;
            }
            *p_out++ = 0;
        }

    } else if (strcmp(v->basetype, "cstring") == 0) {

        // String type
        if (v->qual && v->qual->qual != 'a') {
            fprintf(stderr, "Error (%d): ", line);
            fprintf(stderr, "String type %s cannot have modifiers\n",
                    v->name);
            return 1;
        }
        if (v->qual && v->qual->args && v->qual->args->next) {
            fprintf(stderr, "Error (%d): ", line);
            fprintf(stderr, "Strings are one dimensional\n");
            return 1;
        }
        v->tinfo = VT_string;

    } else if (strcmp(v->basetype, "mxArray") == 0) {

        // MATLAB intrinsic type
        if (v->qual) {
            fprintf(stderr, "Error (%d): ", line);
            fprintf(stderr, "mxArray %s cannot have modifiers\n",
                    v->name);
            return 1;
        }
        v->tinfo = VT_mx;

    } else {

        // Object type
        if (!v->qual)
            v->tinfo = VT_obj;
        else if (v->qual->qual == '*')
            v->tinfo = VT_p_obj;
        else if (v->qual->qual == '&')
            v->tinfo = VT_r_obj;
        else if (v->qual->qual == 'a' || v->qual->qual == 'r') {
            fprintf(stderr, "Error (%d): ", line);
            fprintf(stderr, "%s cannot be an array of object %s\n",
                    v->name, v->basetype);
            return 1;
        } else
            assert(0);

    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x8020
testb $0x1, %al
jne 0xbcd2
jmp 0xbd03
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl $0x6, %edx
movl $0xc, %ecx
movl $0x10, %r8d
movl $0x2, %r9d
movl $0x5, (%rsp)
callq 0xba70
movl %eax, -0x4(%rbp)
jmp 0xc0f7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x80f0
testb $0x1, %al
jne 0xbd16
jmp 0xbd49
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl $0x7, %edx
movl $0xd, %ecx
movl $0x11, %r8d
movl $0x3, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0xba70
movl %eax, -0x4(%rbp)
jmp 0xc0f7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x81c0
testb $0x1, %al
jne 0xbd5c
jmp 0xbd8f
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl $0x8, %edx
movl $0xe, %ecx
movl $0x12, %r8d
movl $0x4, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0xba70
movl %eax, -0x4(%rbp)
jmp 0xc0f7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0xf97f(%rip), %rsi      # 0x1b71d
callq 0x3390
cmpl $0x0, %eax
jne 0xbe84
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xbdfe
movq 0x1921a(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x8df8(%rip), %rsi      # 0x14bc3
movb $0x0, %al
callq 0x33a0
movq 0x191ff(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x8f44(%rip), %rsi      # 0x14d2f
movb $0x0, %al
callq 0x33a0
movl $0x1, -0x4(%rbp)
jmp 0xc0f7
movq -0x10(%rbp), %rax
movl $0x13, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movsbl (%rax), %eax
cmpl $0x27, %eax
jne 0xbe7f
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpb $0x0, (%rax)
je 0xbe6d
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x27, %eax
je 0xbe5d
movq -0x28(%rbp), %rax
movb (%rax), %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
jmp 0xbe5f
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xbe31
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, (%rax)
jmp 0xc0ea
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x8ebf(%rip), %rsi      # 0x14d52
callq 0x3390
cmpl $0x0, %eax
jne 0xbf7f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xbf03
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x61, %eax
je 0xbf03
movq 0x19115(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x8cf3(%rip), %rsi      # 0x14bc3
movb $0x0, %al
callq 0x33a0
movq 0x190fa(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x8e6a(%rip), %rsi      # 0x14d5a
movb $0x0, %al
callq 0x33a0
movl $0x1, -0x4(%rbp)
jmp 0xc0f7
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xbf6f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
cmpq $0x0, 0x8(%rax)
je 0xbf6f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x10(%rax)
je 0xbf6f
movq 0x190a1(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x8c7f(%rip), %rsi      # 0x14bc3
movb $0x0, %al
callq 0x33a0
movq 0x19086(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
leaq 0x8e24(%rip), %rsi      # 0x14d80
movb $0x0, %al
callq 0x33a0
movl $0x1, -0x4(%rbp)
jmp 0xc0f7
movq -0x10(%rbp), %rax
movl $0x9, 0x20(%rax)
jmp 0xc0e8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x8500(%rip), %rsi      # 0x1448e
callq 0x3390
cmpl $0x0, %eax
jne 0xbffa
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xbfea
movq 0x1902e(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x8c0c(%rip), %rsi      # 0x14bc3
movb $0x0, %al
callq 0x33a0
movq 0x19013(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x8dc6(%rip), %rsi      # 0x14d9d
movb $0x0, %al
callq 0x33a0
movl $0x1, -0x4(%rbp)
jmp 0xc0f7
movq -0x10(%rbp), %rax
movl $0xa, 0x20(%rax)
jmp 0xc0e6
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xc015
movq -0x10(%rbp), %rax
movl $0x1, 0x20(%rax)
jmp 0xc0e4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x2a, %eax
jne 0xc035
movq -0x10(%rbp), %rax
movl $0xb, 0x20(%rax)
jmp 0xc0e2
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x26, %eax
jne 0xc055
movq -0x10(%rbp), %rax
movl $0xf, 0x20(%rax)
jmp 0xc0e0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x61, %eax
je 0xc075
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x72, %eax
jne 0xc0c1
movq 0x18f5c(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x8b3a(%rip), %rsi      # 0x14bc3
movb $0x0, %al
callq 0x33a0
movq 0x18f41(%rip), %rax     # 0x24fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
leaq 0x8d0e(%rip), %rsi      # 0x14dbf
movb $0x0, %al
callq 0x33a0
movl $0x1, -0x4(%rbp)
jmp 0xc0f7
leaq 0x8ec5(%rip), %rdi      # 0x14f8d
leaq 0x8c10(%rip), %rsi      # 0x14cdf
movl $0x107, %edx            # imm = 0x107
leaq 0x8d08(%rip), %rcx      # 0x14de3
callq 0x3160
jmp 0xc0e2
jmp 0xc0e4
jmp 0xc0e6
jmp 0xc0e8
jmp 0xc0ea
jmp 0xc0ec
jmp 0xc0ee
jmp 0xc0f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq