// File: /workspace/github_cpp_makefile_star2_O3/sundale[P]gcode/inifile.cc
// Function: IniFile::Find(char const*, char const*, int)
const char *
IniFile::Find(const char *_tag, const char *_section, int _num)
{
    // WTF, return a pointer to the middle of a local buffer?
    // FIX: this is totally non-reentrant.
    static char                 line[LINELEN + 2] = "";        /* 1 for newline, 1 for NULL */
    char                        bracketSection[LINELEN + 2] = "";
    char                        *nonWhite;
    int                         newLinePos;                /* position of newline to strip */
    int                         len;
    char                        tagEnd;
    char                        *valueString;
    char                        *endValueString;

    // For exceptions.
    lineNo = 0;
    tag = _tag;
    section = _section;
    num = _num;

    /* check valid file */
    if(!CheckIfOpen())
        return(NULL);

    /* start from beginning */
    rewind(fp);

    /* check for section first-- if it's non-NULL, then position file at
       line after [section] */
    if(section != NULL){
        sprintf(bracketSection, "[%s]", section);

        /* find [section], and position fp just after it */
        while (!feof(fp)) {

            if (NULL == fgets(line, LINELEN + 1, fp)) {
                /* got to end of file without finding it */
                ThrowException(ERR_SECTION_NOT_FOUND);
                return(NULL);
            }

            if(check_line_endings(line)) {
                ThrowException(ERR_CONVERSION);
                return(NULL);
            }

            /* got a line */
            lineNo++;

            /* strip off newline */
            newLinePos = strlen(line) - 1;        /* newline is on back from 0 */
            if (newLinePos < 0) {
                newLinePos = 0;
            }
            if (line[newLinePos] == '\n') {
                line[newLinePos] = 0;        /* make the newline 0 */
            }

            if (NULL == (nonWhite = SkipWhite(line))) {
                /* blank line-- skip */
                continue;
            }

            /* not a blank line, and nonwhite is first char */
            if (strncmp(bracketSection, nonWhite, strlen(bracketSection)) != 0){
                /* not on this line */
                continue;
            }

            /* it matches-- fp is now set up for search on tag */
            break;
        }
    }

    while (!feof(fp)) {
        /* check for end of file */
        if (NULL == fgets(line, LINELEN + 1, (FILE *) fp)) {
            /* got to end of file without finding it */
            ThrowException(ERR_TAG_NOT_FOUND);
            return(NULL);
        }

        if(check_line_endings(line)) {
            ThrowException(ERR_CONVERSION);
            return(NULL);
        }

        /* got a line */
        lineNo++;

        /* strip off newline */
        newLinePos = strlen(line) - 1;        /* newline is on back from 0 */
        if (newLinePos < 0) {
            newLinePos = 0;
        }
        if (line[newLinePos] == '\n') {
            line[newLinePos] = 0;        /* make the newline 0 */
        }

        /* skip leading whitespace */
        if (NULL == (nonWhite = SkipWhite(line))) {
            /* blank line-- skip */
            continue;
        }

        /* check for '[' char-- if so, it's a section tag, and we're out
           of our section */
        if (NULL != section && nonWhite[0] == '[') {
            ThrowException(ERR_TAG_NOT_FOUND);
            return(NULL);
        }

        len = strlen(tag);
        if (strncmp(tag, nonWhite, len) != 0) {
            /* not on this line */
            continue;
        }

        /* it matches the first part of the string-- if whitespace or = is
           next char then call it a match */
        tagEnd = nonWhite[len];
        if (tagEnd == ' ' || tagEnd == '\r' || tagEnd == '\t'
            || tagEnd == '\n' || tagEnd == '=') {
            /* it matches-- return string after =, or NULL */
            if (--_num > 0) {
                /* Not looking for this one, so skip it... */
                continue;
            }
            nonWhite += len;
            valueString = AfterEqual(nonWhite);
            /* Eliminate white space at the end of a line also. */
            if (NULL == valueString) {
                ThrowException(ERR_TAG_NOT_FOUND);
                return(NULL);
            }
            endValueString = valueString + strlen(valueString) - 1;
            while (*endValueString == ' ' || *endValueString == '\t'
                   || *endValueString == '\r') {
                *endValueString = 0;
                endValueString--;
            }
            return(valueString);
        }
        /* else continue */
    }

    ThrowException(ERR_TAG_NOT_FOUND);
    return(NULL);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %ecx, %r12d
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq -0x150(%rbp), %rdi
movl $0x101, %edx            # imm = 0x101
xorl %esi, %esi
callq 0x40e0
movl $0x0, 0x54(%rbx)
movq %r15, 0x58(%rbx)
movq %r14, 0x60(%rbx)
movl %r12d, 0x68(%rbx)
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4f03
movq %r12, -0x38(%rbp)
callq 0x4040
movq 0x60(%rbx), %rdx
movabsq $0x100002600, %r12      # imm = 0x100002600
testq %rdx, %rdx
je 0x4d98
leaq 0x4346(%rip), %rsi      # 0x90bd
xorl %r13d, %r13d
leaq -0x150(%rbp), %rdi
xorl %eax, %eax
callq 0x4060
movq (%rbx), %rdi
callq 0x4240
testl %eax, %eax
je 0x4f17
leaq 0x9441(%rip), %r15      # 0xe1e0
movabsq $0x800000800000001, %r14 # imm = 0x800000800000001
movq %rbx, -0x30(%rbp)
movq (%rbx), %rdi
callq 0x4240
movl $0x4, %esi
testl %eax, %eax
jne 0x4fcd
movq (%rbx), %rdx
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4270
testq %rax, %rax
je 0x4f0d
callq 0x52e3
testb %al, %al
jne 0x4fc8
incl 0x54(%rbx)
movq %r15, %rdi
callq 0x40a0
decl %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
cmpb $0xa, (%rax,%r15)
jne 0x4e0b
movb $0x0, (%rax,%r15)
movq %r15, %rbx
movq %r15, %r13
movzbl (%r13), %eax
cmpq $0x3b, %rax
ja 0x4e4d
btq %rax, %r12
jae 0x4e27
incq %r13
jmp 0x4e11
btq %rax, %r14
jae 0x4e4d
movq -0x30(%rbp), %r13
movq (%r13), %rdi
callq 0x4240
testl %eax, %eax
movq %rbx, %r15
movq %r13, %rbx
je 0x4dc2
jmp 0x4f0d
movq -0x30(%rbp), %rdx
cmpq $0x0, 0x60(%rdx)
setne %cl
cmpb $0x5b, %al
sete %al
testb %al, %cl
jne 0x503d
movq 0x58(%rdx), %r14
movq %r14, %rdi
callq 0x40a0
movq %rax, -0x40(%rbp)
movslq %eax, %r15
movq %r14, %rdi
movabsq $0x800000800000001, %r14 # imm = 0x800000800000001
movq %r13, %rsi
movq %r15, %rdx
callq 0x40b0
testl %eax, %eax
jne 0x4e2d
movzbl (%r13,%r15), %eax
cmpq $0x3d, %rax
ja 0x4e2d
movabsq $0x2000000100002600, %rcx # imm = 0x2000000100002600
btq %rax, %rcx
jae 0x4e2d
movq -0x38(%rbp), %rdx
leal -0x1(%rdx), %ecx
cmpl $0x1, %edx
movq %rcx, -0x38(%rbp)
movq %rbx, %r15
movq -0x30(%rbp), %rbx
jg 0x4dad
movslq -0x40(%rbp), %rcx
leaq (%rcx,%r13), %r15
leaq (%rcx,%r13), %r14
incq %r14
movl $0x4, %esi
testb %al, %al
je 0x4fcd
movzbl %al, %eax
cmpl $0x3d, %eax
je 0x4ff3
incq %r15
movb (%r14), %al
incq %r14
jmp 0x4ee4
movl $0x1, %esi
jmp 0x4fcd
movl $0x4, %esi
jmp 0x4fcd
leaq 0x92c2(%rip), %r15      # 0xe1e0
movq (%rbx), %rdx
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4270
testq %rax, %rax
je 0x5036
callq 0x52e3
testb %al, %al
jne 0x4fc8
incl 0x54(%rbx)
movq %r15, %rdi
callq 0x40a0
decl %eax
testl %eax, %eax
cmovlel %r13d, %eax
cmpb $0xa, (%rax,%r15)
jne 0x4f63
movb $0x0, (%rax,%r15)
movq %r15, %r14
movzbl (%r14), %eax
cmpq $0x3b, %rax
ja 0x4f8b
btq %rax, %r12
jae 0x4f7b
incq %r14
jmp 0x4f66
movabsq $0x800000800000001, %rcx # imm = 0x800000800000001
btq %rax, %rcx
jb 0x4fb3
leaq -0x150(%rbp), %r13
movq %r13, %rdi
callq 0x40a0
movq %r13, %rdi
xorl %r13d, %r13d
movq %r14, %rsi
movq %rax, %rdx
callq 0x40b0
testl %eax, %eax
je 0x4d98
movq (%rbx), %rdi
callq 0x4240
testl %eax, %eax
je 0x4f1e
jmp 0x4d98
movl $0x8, %esi
movq %rbx, %rdi
callq 0x5048
xorl %r14d, %r14d
movq %r14, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incq %r15
incq %r14
movzbl (%r14), %eax
cmpq $0x20, %rax
ja 0x5008
btq %rax, %r12
jb 0x4fed
testq %rax, %rax
je 0x4fcd
movq %r14, %rdi
callq 0x40a0
addq %rax, %r15
movabsq $0x100002200, %rax      # imm = 0x100002200
movzbl (%r15), %ecx
cmpq $0x20, %rcx
ja 0x4fd8
btq %rcx, %rax
jae 0x4fd8
movb $0x0, (%r15)
decq %r15
jmp 0x501d
movl $0x2, %esi
jmp 0x4fcd
movl $0x4, %esi
movq %rdx, %rbx
jmp 0x4fcd
nop