// File: /workspace/github_cpp_makefile_star2_O1/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 $0x118, %rsp            # imm = 0x118
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq -0x140(%rbp), %rdi
movl $0x101, %edx            # imm = 0x101
xorl %esi, %esi
callq 0x40e0
movl $0x0, 0x54(%r12)
movq %r15, 0x58(%r12)
movq %r14, 0x60(%r12)
movl %ebx, 0x68(%r12)
cmpq $0x0, (%r12)
je 0x4f10
movq %rbx, -0x38(%rbp)
movq (%r12), %rdi
callq 0x4040
movq %r12, -0x30(%rbp)
movq 0x60(%r12), %rdx
movabsq $0x100002600, %rbx      # imm = 0x100002600
testq %rdx, %rdx
je 0x4dc0
leaq 0x4322(%rip), %rsi      # 0x90bd
xorl %r14d, %r14d
leaq -0x140(%rbp), %rdi
xorl %eax, %eax
callq 0x4060
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x4250
testl %eax, %eax
je 0x4f89
leaq 0x9419(%rip), %r15      # 0xe1e0
movq -0x30(%rbp), %r12
movq (%r12), %rdi
callq 0x4250
movl $0x4, %esi
testl %eax, %eax
jne 0x505c
movq (%r12), %rdx
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4280
testq %rax, %rax
je 0x4f1a
callq 0x5317
testb %al, %al
jne 0x4f24
incl 0x54(%r12)
movq %r15, %rdi
callq 0x40a0
decl %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
cmpb $0xa, (%rax,%r15)
jne 0x4e2d
movb $0x0, (%rax,%r15)
movq %r15, %r13
movzbl (%r13), %eax
cmpq $0x3b, %rax
ja 0x4e59
btq %rax, %rbx
jae 0x4e46
incq %r13
jmp 0x4e30
movabsq $0x800000800000001, %rcx # imm = 0x800000800000001
btq %rax, %rcx
jae 0x4e59
xorl %r13d, %r13d
testq %r13, %r13
je 0x4eb9
cmpq $0x0, 0x60(%r12)
je 0x4e71
cmpb $0x5b, (%r13)
je 0x4f1a
movq -0x30(%rbp), %rax
movq 0x58(%rax), %r12
movq %r12, %rdi
callq 0x40a0
movq %rax, %r14
movslq %r14d, %r15
movq %r12, %rdi
movq -0x30(%rbp), %r12
movq %r13, %rsi
movq %r15, %rdx
callq 0x40b0
testl %eax, %eax
jne 0x4eb9
movzbl (%r13,%r15), %eax
cmpq $0x3d, %rax
ja 0x4eb9
movabsq $0x2000000100002600, %rcx # imm = 0x2000000100002600
btq %rax, %rcx
jb 0x4ed3
movq (%r12), %rdi
callq 0x4250
testl %eax, %eax
leaq 0x9315(%rip), %r15      # 0xe1e0
je 0x4de1
jmp 0x4f1a
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movq %rax, -0x38(%rbp)
leaq 0x92f8(%rip), %r15      # 0xe1e0
jg 0x4dcb
movslq %r14d, %rax
leaq (%rax,%r13), %r14
incq %r14
movl $0x4, %esi
movzbl -0x1(%r14), %eax
testl %eax, %eax
je 0x4f46
cmpl $0x3d, %eax
je 0x4f31
incq %r14
jmp 0x4efd
movl $0x1, %esi
jmp 0x505c
movl $0x4, %esi
jmp 0x505c
movl $0x8, %esi
jmp 0x505c
incq %r14
movzbl (%r14), %eax
cmpq $0x20, %rax
ja 0x4f49
btq %rax, %rbx
jb 0x4f2e
testq %rax, %rax
jne 0x4f49
xorl %r14d, %r14d
testq %r14, %r14
je 0x505c
movq %r14, %rdi
callq 0x40a0
addq %r14, %rax
decq %rax
movabsq $0x100002200, %rcx      # imm = 0x100002200
movzbl (%rax), %edx
cmpq $0x20, %rdx
ja 0x5067
btq %rdx, %rcx
jae 0x5067
movb $0x0, (%rax)
decq %rax
jmp 0x4f6a
leaq 0x9250(%rip), %r15      # 0xe1e0
leaq -0x140(%rbp), %r12
movq -0x30(%rbp), %rax
movq (%rax), %rdx
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4280
testq %rax, %rax
je 0x504c
callq 0x5317
testb %al, %al
jne 0x5053
movq -0x30(%rbp), %rax
incl 0x54(%rax)
movq %r15, %rdi
callq 0x40a0
decl %eax
testl %eax, %eax
cmovlel %r14d, %eax
cmpb $0xa, (%rax,%r15)
jne 0x4fe4
movb $0x0, (%rax,%r15)
movq %r15, %r13
movzbl (%r13), %eax
cmpq $0x3b, %rax
ja 0x5010
btq %rax, %rbx
jae 0x4ffd
incq %r13
jmp 0x4fe7
movabsq $0x800000800000001, %rcx # imm = 0x800000800000001
btq %rax, %rcx
jae 0x5010
xorl %r13d, %r13d
testq %r13, %r13
je 0x5033
movq %r12, %rdi
callq 0x40a0
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x40b0
testl %eax, %eax
je 0x4dc0
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x4250
testl %eax, %eax
je 0x4f97
jmp 0x4dc0
movl $0x2, %esi
jmp 0x5058
movl $0x8, %esi
movq -0x30(%rbp), %r12
movq %r12, %rdi
callq 0x507c
xorl %r14d, %r14d
movq %r14, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq