// File: /workspace/github_c_makefile_star2_O1/pipde[P]deos-songlib/utils/processtab/tabchecker.c
// Function: newCompositionFromTab
Composition *
newCompositionFromTab(ASTTab *astTab)
    {
    Composition *c = newComposition();
    Part *part;
    Measure *measure;
    Section *section = 0;
    int i, j, k, partIndex, measureCount = 0;
    ASTCommand *astCommand;
    ASTStave *astStave;
    ASTCourse *astCourse;
    char *astEvents, *astTimingEvents;
    ASTAttributeList *attributeList = 0, *originalList = 0;
    bool attributeAdded;
        
    /* First pass checks all the stave courses and creates parts */
    
    for (i = 0; i < astTab->commandCount; i++)
        {
        astCommand = astTab->commands[i];
        if (astCommand->commandType == StaveCommand)
            {
            astStave = astCommand->un.stave;
            for (j = 0; j < astStave->courseCount; j++)
                {
                astCourse = astStave->courses[j];
                part = 0;
                /* Add measure to each part */
                for (partIndex = 0; partIndex < c->partCount; partIndex++)
                    {                
                    if (strcmp(astCourse->name, c->parts[partIndex]->voiceName) == 0) 
                        {
                        part = c->parts[partIndex];
                        break;
                        }
                    }
                    
                if (part == 0)
                    {
                    /* Set up part */
                    part = newPart();
                    part->voiceName = StringDup(astCourse->name);
                    addPartToComposition(part, c);
                    }
                }
            }
        }
    
    /* Second pass reads attributes and staves and populates sections, measures, and note instances */
    
    for (i = 0; i < astTab->commandCount; i++)
        {
        astCommand = astTab->commands[i];
        /* attribute command */
        if (astCommand->commandType == AttributeCommand) 
            {
            attributeList = addAttributeToList(astCommand->un.attribute, attributeList);
                        
            /* add attribute name to composition if it hasn't already been added */
            attributeAdded = false;
            for (j = 0; j < c->attributeNameCount; j++)
                {
                if (strcmp(astCommand->un.attribute->name, c->attributeNames[j]) == 0) attributeAdded = true;
                }
            if (!attributeAdded)
                {
                addAttributeNameToComposition(astCommand->un.attribute->name, c);
                }
           }
        /* stave command */
        else if (astCommand->commandType == StaveCommand)
            {
            astStave = astCommand->un.stave;
            
            /* Begin a section at each new stave */
            section = newSection();
            section->measureNumber = measureCount;
            addSectionToComposition(section, c);
            
            /* Add current attributes to section, removing them from the list */
            while (attributeList != 0)
                {
                addAttributeToSection(attributeList->attribute, section);
                originalList = attributeList;
                attributeList = attributeList->next;
                free(originalList); 
                originalList = 0;
                }
            
            /* Add measures to each part */
            for (partIndex = 0; partIndex < c->partCount; partIndex++)
                {                
                part = c->parts[partIndex];
                
                for (j = 0; j < astStave->timingCourse->measureCount; j++)
                    {
                    measure = newMeasure();
                    measure->beatsInMeasure = 4; /* TODO: replace with attribute */
                    measure->beatLength = 4;     /* TODO: replace with attribute */
                    addMeasureToPart(measure, part);
                    
                    astTimingEvents = astStave->timingCourse->measures[j]->events;
                    
                    /* find course in this stave that matches the part, then populate noteInstances */
                    for (k = 0; k < astStave->courseCount; k++)
                        {
                        astCourse = astStave->courses[k];
                        if (strcmp(astCourse->name, part->voiceName) != 0) continue;
                        
                        /* build note instances based on timing events */            
                        astEvents = astCourse->measures[j]->events;
                        buildMeasureFromASTEvents(measure, astEvents, astTimingEvents);
                        }
                    
                    /* course for this part not found; populate with rests */
                    if (measure->noteInstances == 0) buildMeasureFromASTEvents(measure, 0, astTimingEvents);
                    }
                }
            
            /* Advance measure count */
            measureCount += astStave->timingCourse->measureCount;
            }
        /* comment command */
        else if (astCommand->commandType == CommentCommand) 
            {
            /* skip */
            }
        /* unknown command */
        else
            {
            Fatal("AstCommand of type '%i' found; aborting\n", astCommand->commandType);
            return 0;
            }
        }
    
    return c;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl $0x30, %edi
callq 0x4c71
movq %rax, %rbp
xorl %eax, %eax
movq %rax, (%rbp)
movl %eax, 0x8(%rbp)
movq %rax, 0x10(%rbp)
movl %eax, 0x18(%rbp)
cmpl %eax, 0x8(%rbx)
movq %rbp, 0x18(%rsp)
movq %rbx, 0x30(%rsp)
jle 0x40bd
xorl %ecx, %ecx
movq (%rbx), %rax
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rax
cmpl $0x2, (%rax)
jne 0x40a3
movq 0x8(%rax), %rcx
cmpl $0x0, 0x8(%rcx)
jle 0x40a3
xorl %r13d, %r13d
movq %rcx, 0x28(%rsp)
movq (%rcx), %rax
movq (%rax,%r13,8), %rax
movq %rax, 0x8(%rsp)
movslq 0x8(%rbp), %rbx
testq %rbx, %rbx
jle 0x400c
movq 0x8(%rsp), %rax
movq (%rax), %r15
movq 0x18(%rsp), %rax
movq (%rax), %r14
xorl %r12d, %r12d
movq (%r14,%r12,8), %rbp
movq 0x10(%rbp), %rsi
movq %r15, %rdi
callq 0x1110
testl %eax, %eax
je 0x4007
incq %r12
cmpq %r12, %rbx
jne 0x3fe9
jmp 0x400c
testq %rbp, %rbp
jne 0x4089
movl $0x20, %edi
callq 0x4c71
movq %rax, %r15
movq $0x0, (%rax)
movl $0x0, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x4c54
movq %rax, 0x10(%r15)
movq 0x18(%rsp), %r14
movq (%r14), %rbx
movl 0x8(%r14), %eax
leal 0x8(,%rax,8), %edi
callq 0x4c71
movq %rax, (%r14)
movslq 0x8(%r14), %rax
testq %rax, %rax
jle 0x4078
xorl %ecx, %ecx
movq (%rbx,%rcx,8), %rdx
movq (%r14), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4065
movq (%r14), %rcx
movq %r15, (%rcx,%rax,8)
incl %eax
movl %eax, 0x8(%r14)
movq %r14, 0x18(%r15)
incq %r13
movq 0x28(%rsp), %rcx
movslq 0x8(%rcx), %rax
cmpq %rax, %r13
movq 0x18(%rsp), %rbp
jl 0x3fc1
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x30(%rsp), %rbx
movslq 0x8(%rbx), %rax
cmpq %rax, %rcx
jl 0x3f96
cmpl $0x0, 0x8(%rbx)
jle 0x443c
xorl %ecx, %ecx
xorl %r15d, %r15d
movl $0x0, 0x14(%rsp)
movq (%rbx), %rax
movq (%rax,%rcx,8), %r14
movl (%r14), %esi
cmpl $0x3, %esi
je 0x441a
cmpl $0x2, %esi
movq %rcx, 0x20(%rsp)
je 0x4132
cmpl $0x1, %esi
jne 0x442c
movq 0x8(%r14), %rbx
movl $0x10, %edi
callq 0x4c71
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rbx, (%rax)
testq %r15, %r15
je 0x4374
movq %r15, %rdx
movq %rdx, %rcx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
jne 0x411d
movq %rax, 0x8(%rcx)
jmp 0x4377
movq 0x8(%r14), %r14
movl $0x18, %edi
callq 0x4c71
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl 0x14(%rsp), %eax
movl %eax, 0xc(%r12)
movq 0x10(%rbp), %rbx
movl 0x18(%rbp), %eax
leal 0x8(,%rax,8), %edi
callq 0x4c71
movq %rax, 0x10(%rbp)
movslq 0x18(%rbp), %rax
testq %rax, %rax
jle 0x4190
xorl %ecx, %ecx
movq (%rbx,%rcx,8), %rdx
movq 0x10(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x417c
movq 0x10(%rbp), %rcx
movq %r12, (%rcx,%rax,8)
incl %eax
movl %eax, 0x18(%rbp)
movq %rbp, 0x10(%r12)
testq %r15, %r15
je 0x4207
movq (%r15), %rbx
movq (%r12), %r13
movl 0x8(%r12), %eax
leal 0x8(,%rax,8), %edi
callq 0x4c71
movq %rax, (%r12)
movslq 0x8(%r12), %rax
testq %rax, %rax
jle 0x41e4
xorl %ecx, %ecx
movq (%r13,%rcx,8), %rdx
movq (%r12), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x41cf
movq (%r12), %rcx
movq %rbx, (%rcx,%rax,8)
incl %eax
movl %eax, 0x8(%r12)
movq 0x8(%r15), %rbx
movq %r15, %rdi
callq 0x1030
movq %rbx, %r15
testq %rbx, %rbx
jne 0x41a7
cmpl $0x0, 0x8(%rbp)
jle 0x435d
movq $0x0, 0x28(%rsp)
movq 0x10(%r14), %rax
cmpl $0x0, 0x10(%rax)
jle 0x433e
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rbp
movq $0x0, 0x8(%rsp)
movl $0x20, %edi
callq 0x4c71
movq %rax, %r15
movq $0x0, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl $0x4, %eax
movl %eax, 0xc(%r15)
movl %eax, 0x10(%r15)
movq (%rbp), %rbx
movl 0x8(%rbp), %eax
leal 0x8(,%rax,8), %edi
callq 0x4c71
movq %rax, (%rbp)
movslq 0x8(%rbp), %rax
testq %rax, %rax
jle 0x42a0
xorl %ecx, %ecx
movq (%rbx,%rcx,8), %rdx
movq (%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x428c
movq (%rbp), %rcx
movq %r15, (%rcx,%rax,8)
incl %eax
movl %eax, 0x8(%rbp)
movq %rbp, 0x18(%r15)
movq 0x10(%r14), %rax
movq 0x8(%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %r12
cmpl $0x0, 0x8(%r14)
jle 0x430d
xorl %ebx, %ebx
movq (%r14), %rax
movq (%rax,%rbx,8), %r13
movq (%r13), %rdi
movq 0x10(%rbp), %rsi
callq 0x1110
testl %eax, %eax
jne 0x4301
movq 0x8(%r13), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x444e
incq %rbx
movslq 0x8(%r14), %rax
cmpq %rax, %rbx
jl 0x42ce
cmpq $0x0, (%r15)
jne 0x4320
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x444e
movq 0x8(%rsp), %rdx
incq %rdx
movq 0x10(%r14), %rax
movslq 0x10(%rax), %rax
movq %rdx, 0x8(%rsp)
cmpq %rax, %rdx
jl 0x4242
movq 0x28(%rsp), %rdx
incq %rdx
movq 0x18(%rsp), %rbp
movslq 0x8(%rbp), %rax
movq %rdx, 0x28(%rsp)
cmpq %rax, %rdx
jl 0x421a
movq 0x10(%r14), %rax
movl 0x14(%rsp), %ecx
addl 0x10(%rax), %ecx
movl %ecx, 0x14(%rsp)
xorl %r15d, %r15d
jmp 0x4410
movq %rax, %r15
movslq 0x28(%rbp), %r13
testq %r13, %r13
jle 0x43c8
movq %r14, 0x8(%rsp)
movq 0x8(%r14), %rax
movq (%rax), %r12
movq 0x20(%rbp), %rbx
xorl %ebp, %ebp
xorl %r14d, %r14d
movq (%rbx,%rbp,8), %rsi
movq %r12, %rdi
callq 0x1110
testl %eax, %eax
movzbl %r14b, %r14d
movl $0x1, %eax
cmovel %eax, %r14d
incq %rbp
cmpq %rbp, %r13
jne 0x4395
testb $0x1, %r14b
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r14
jne 0x4410
movq 0x8(%r14), %rax
movq (%rax), %rbx
movq 0x20(%rbp), %r14
leal 0x8(,%r13,8), %edi
callq 0x4c71
movq %rax, 0x20(%rbp)
movslq 0x28(%rbp), %rax
testq %rax, %rax
jle 0x4403
xorl %ecx, %ecx
movq (%r14,%rcx,8), %rdx
movq 0x20(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x43ef
movq 0x20(%rbp), %rcx
movq %rbx, (%rcx,%rax,8)
incl %eax
movl %eax, 0x28(%rbp)
movq 0x30(%rsp), %rbx
movq 0x20(%rsp), %rcx
incq %rcx
movslq 0x8(%rbx), %rax
cmpq %rax, %rcx
jl 0x40d4
jmp 0x443c
leaq 0x1f8d(%rip), %rdi      # 0x63c0
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x4b44
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq