// File: /workspace/github_c_makefile_star2_O2/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, %r13
xorl %r14d, %r14d
callq 0x3a48
movq %rax, %r12
xorl %ecx, %ecx
movq %rax, 0x8(%rsp)
movq %r13, 0x20(%rsp)
movslq 0x8(%r13), %rax
cmpq %rax, %rcx
jge 0x3beb
movq (%r13), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %rax
cmpl $0x2, (%rax)
jne 0x3bd4
movq 0x8(%rax), %r13
xorl %ebp, %ebp
movslq 0x8(%r13), %rax
cmpq %rax, %rbp
jge 0x3bd4
movq (%r13), %rax
movq (%rax,%rbp,8), %r12
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ebx
testl %ebx, %ebx
cmovlel %r14d, %ebx
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x3b5f
movq (%r12), %rdi
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rax
movq 0x10(%rax), %rsi
callq 0x1110
incq %r15
testl %eax, %eax
jne 0x3b38
jmp 0x3bcc
callq 0x3a64
movq %rax, %r15
movq (%r12), %rdi
callq 0x47a8
movq %rax, 0x10(%r15)
movq 0x8(%rsp), %r12
movq (%r12), %rbx
movl 0x8(%r12), %eax
leal 0x8(,%rax,8), %edi
callq 0x47c5
movq %rax, (%r12)
movslq 0x8(%r12), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x3bb9
movq (%rbx,%rdx,8), %rsi
movq (%r12), %rdi
movq %rsi, (%rdi,%rdx,8)
incq %rdx
jmp 0x3ba3
movq (%r12), %rcx
movq %r15, (%rcx,%rax,8)
incl %eax
movl %eax, 0x8(%r12)
movq %r12, 0x18(%r15)
incq %rbp
jmp 0x3b12
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %r12
movq 0x20(%rsp), %r13
jmp 0x3ae9
xorl %ecx, %ecx
movl $0x0, 0x14(%rsp)
xorl %r15d, %r15d
cltq
cmpq %rax, %rcx
jge 0x3f51
movq (%r13), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %r14
movl (%r14), %esi
cmpl $0x3, %esi
je 0x3f2f
cmpl $0x2, %esi
je 0x3c5f
cmpl $0x1, %esi
jne 0x3f40
movq 0x8(%r14), %rbx
pushq $0x10
popq %rdi
callq 0x47c5
andq $0x0, 0x8(%rax)
movq %rbx, (%rax)
testq %r15, %r15
je 0x3e77
movq %r15, %rdx
movq %rdx, %rcx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
jne 0x3c4a
movq %rax, 0x8(%rcx)
jmp 0x3e7a
movq 0x8(%r14), %r14
callq 0x3aab
movq %r12, %r13
movq %rax, %r12
movl 0x14(%rsp), %eax
movl %eax, 0xc(%r12)
movq 0x10(%r13), %rbx
movl 0x18(%r13), %eax
leal 0x8(,%rax,8), %edi
callq 0x47c5
movq %rax, 0x10(%r13)
movslq 0x18(%r13), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x3cba
movq (%rbx,%rdx,8), %rsi
movq 0x8(%rsp), %rdi
movq 0x10(%rdi), %rdi
movq %rsi, (%rdi,%rdx,8)
incq %rdx
jmp 0x3c9f
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rcx
movq %r12, (%rcx,%rax,8)
incl %eax
movl %eax, 0x18(%rdx)
movq %rdx, 0x10(%r12)
testq %r15, %r15
je 0x3d3a
movq (%r15), %rbx
movq (%r12), %r13
movl 0x8(%r12), %eax
leal 0x8(,%rax,8), %edi
callq 0x47c5
movq %rax, (%r12)
movslq 0x8(%r12), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x3d1a
movq (%r13,%rdx,8), %rsi
movq (%r12), %rdi
movq %rsi, (%rdi,%rdx,8)
incq %rdx
jmp 0x3d03
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
jmp 0x3cd1
xorl %ecx, %ecx
movq 0x8(%rsp), %r12
movslq 0x8(%r12), %rax
cmpq %rax, %rcx
jge 0x3e63
movq (%r12), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %r13
xorl %ecx, %ecx
movq 0x10(%r14), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %rcx
jge 0x3e56
movq %rcx, 0x18(%rsp)
callq 0x3a7e
movq %rax, %r15
movabsq $0x400000004, %rax      # imm = 0x400000004
movq %rax, 0xc(%r15)
movq (%r13), %rbx
movl 0x8(%r13), %eax
leal 0x8(,%rax,8), %edi
callq 0x47c5
movq %rax, (%r13)
movslq 0x8(%r13), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x3dc8
movq (%rbx,%rdx,8), %rsi
movq (%r13), %rdi
movq %rsi, (%rdi,%rdx,8)
incq %rdx
jmp 0x3db2
movq (%r13), %rcx
movq %r15, (%rcx,%rax,8)
incl %eax
movl %eax, 0x8(%r13)
movq %r13, 0x18(%r15)
movq 0x10(%r14), %rax
movq 0x8(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %r12
xorl %ebx, %ebx
movslq 0x8(%r14), %rax
cmpq %rax, %rbx
jge 0x3e31
movq (%r14), %rax
movq (%rax,%rbx,8), %rbp
movq (%rbp), %rdi
movq 0x10(%r13), %rsi
callq 0x1110
testl %eax, %eax
jne 0x3e2c
movq 0x8(%rbp), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x3f63
incq %rbx
jmp 0x3df0
cmpq $0x0, (%r15)
jne 0x3e44
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3f63
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %r12
jmp 0x3d5e
movq 0x30(%rsp), %rcx
incq %rcx
jmp 0x3d41
movq 0x10(%r14), %rax
movl 0x14(%rsp), %ecx
addl 0x10(%rax), %ecx
movl %ecx, 0x14(%rsp)
xorl %r15d, %r15d
jmp 0x3ec6
movq %rax, %r15
movl 0x28(%r12), %r12d
xorl %ebx, %ebx
testl %r12d, %r12d
movl $0x0, %r13d
cmovgl %r12d, %r13d
xorl %ebp, %ebp
cmpq %rbx, %r13
je 0x3ebb
movq 0x8(%r14), %rax
movq (%rax), %rdi
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%rbx,8), %rsi
callq 0x1110
testl %eax, %eax
sete %al
orb %al, %bpl
incq %rbx
jmp 0x3e90
testb $0x1, %bpl
je 0x3ecd
movq 0x8(%rsp), %r12
movq 0x20(%rsp), %r13
jmp 0x3f2f
movq 0x8(%r14), %rax
movq (%rax), %rbx
movq 0x8(%rsp), %rax
movq 0x20(%rax), %r14
leal 0x8(,%r12,8), %edi
movq %rax, %r12
callq 0x47c5
movq %rax, 0x20(%r12)
movslq 0x28(%r12), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
xorl %edx, %edx
movq 0x20(%rsp), %r13
cmpq %rdx, %rcx
je 0x3f1f
movq (%r14,%rdx,8), %rsi
movq 0x20(%r12), %rdi
movq %rsi, (%rdi,%rdx,8)
incq %rdx
jmp 0x3f08
movq 0x20(%r12), %rcx
movq %rbx, (%rcx,%rax,8)
incl %eax
movl %eax, 0x28(%r12)
movq 0x28(%rsp), %rcx
incq %rcx
movl 0x8(%r13), %eax
jmp 0x3bf8
leaq 0x245b(%rip), %rdi      # 0x63a2
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x4610
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq