// File: 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
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0x130(%rbp), %rdi
xorl %esi, %esi
movl $0x101, %edx            # imm = 0x101
callq 0x40e0
movq -0x160(%rbp), %rdi
movl $0x0, 0x54(%rdi)
movq -0x18(%rbp), %rax
movq %rax, 0x58(%rdi)
movq -0x20(%rbp), %rax
movq %rax, 0x60(%rdi)
movl -0x24(%rbp), %eax
movl %eax, 0x68(%rdi)
callq 0x5ca0
testb $0x1, %al
jne 0x5412
movq $0x0, -0x8(%rbp)
jmp 0x58ec
movq -0x160(%rbp), %rax
movq (%rax), %rdi
callq 0x4040
movq -0x160(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x55bd
movq -0x160(%rbp), %rax
leaq -0x130(%rbp), %rdi
movq 0x60(%rax), %rdx
leaq 0x5c91(%rip), %rsi      # 0xb0dd
movb $0x0, %al
callq 0x4060
movq -0x160(%rbp), %rax
movq (%rax), %rdi
callq 0x4270
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x5473
jmp 0x55bb
movq -0x160(%rbp), %rax
movq (%rax), %rdx
leaq 0xad7c(%rip), %rdi      # 0x10200
movl $0x100, %esi            # imm = 0x100
callq 0x42a0
movq %rax, %rcx
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x54b6
movq -0x160(%rbp), %rdi
movl $0x2, %esi
callq 0x5900
movq $0x0, -0x8(%rbp)
jmp 0x58ec
leaq 0xad43(%rip), %rdi      # 0x10200
callq 0x5cf0
testb $0x1, %al
jne 0x54c8
jmp 0x54e6
movq -0x160(%rbp), %rdi
movl $0x8, %esi
callq 0x5900
movq $0x0, -0x8(%rbp)
jmp 0x58ec
movq -0x160(%rbp), %rax
movl 0x54(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x54(%rax)
leaq 0xad03(%rip), %rdi      # 0x10200
callq 0x40a0
subq $0x1, %rax
movl %eax, -0x13c(%rbp)
cmpl $0x0, -0x13c(%rbp)
jge 0x551f
movl $0x0, -0x13c(%rbp)
movslq -0x13c(%rbp), %rcx
leaq 0xacd3(%rip), %rax      # 0x10200
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x5548
movslq -0x13c(%rbp), %rcx
leaq 0xacbc(%rip), %rax      # 0x10200
movb $0x0, (%rax,%rcx)
movq -0x160(%rbp), %rdi
leaq 0xacaa(%rip), %rsi      # 0x10200
callq 0x5db0
movq %rax, %rcx
movq %rcx, -0x138(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x5571
jmp 0x5453
leaq -0x130(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x40a0
movq -0x170(%rbp), %rdi
movq -0x168(%rbp), %rsi
movq %rax, %rdx
callq 0x40b0
cmpl $0x0, %eax
je 0x55b9
jmp 0x5453
jmp 0x55bb
jmp 0x55bd
jmp 0x55bf
movq -0x160(%rbp), %rax
movq (%rax), %rdi
callq 0x4270
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x55df
jmp 0x58d3
movq -0x160(%rbp), %rax
movq (%rax), %rdx
leaq 0xac10(%rip), %rdi      # 0x10200
movl $0x100, %esi            # imm = 0x100
callq 0x42a0
movq %rax, %rcx
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x5622
movq -0x160(%rbp), %rdi
movl $0x4, %esi
callq 0x5900
movq $0x0, -0x8(%rbp)
jmp 0x58ec
leaq 0xabd7(%rip), %rdi      # 0x10200
callq 0x5cf0
testb $0x1, %al
jne 0x5634
jmp 0x5652
movq -0x160(%rbp), %rdi
movl $0x8, %esi
callq 0x5900
movq $0x0, -0x8(%rbp)
jmp 0x58ec
movq -0x160(%rbp), %rax
movl 0x54(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x54(%rax)
leaq 0xab97(%rip), %rdi      # 0x10200
callq 0x40a0
subq $0x1, %rax
movl %eax, -0x13c(%rbp)
cmpl $0x0, -0x13c(%rbp)
jge 0x568b
movl $0x0, -0x13c(%rbp)
movslq -0x13c(%rbp), %rcx
leaq 0xab67(%rip), %rax      # 0x10200
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x56b4
movslq -0x13c(%rbp), %rcx
leaq 0xab50(%rip), %rax      # 0x10200
movb $0x0, (%rax,%rcx)
movq -0x160(%rbp), %rdi
leaq 0xab3e(%rip), %rsi      # 0x10200
callq 0x5db0
movq %rax, %rcx
movq %rcx, -0x138(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x56dd
jmp 0x55bf
movq -0x160(%rbp), %rcx
xorl %eax, %eax
cmpq 0x60(%rcx), %rax
je 0x5719
movq -0x138(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5b, %eax
jne 0x5719
movq -0x160(%rbp), %rdi
movl $0x4, %esi
callq 0x5900
movq $0x0, -0x8(%rbp)
jmp 0x58ec
movq -0x160(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x40a0
movq %rax, %rcx
movq -0x160(%rbp), %rax
movl %ecx, -0x140(%rbp)
movq 0x58(%rax), %rdi
movq -0x138(%rbp), %rsi
movslq -0x140(%rbp), %rdx
callq 0x40b0
cmpl $0x0, %eax
je 0x575a
jmp 0x55bf
movq -0x138(%rbp), %rax
movslq -0x140(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x141(%rbp)
movsbl -0x141(%rbp), %eax
cmpl $0x20, %eax
je 0x57b1
movsbl -0x141(%rbp), %eax
cmpl $0xd, %eax
je 0x57b1
movsbl -0x141(%rbp), %eax
cmpl $0x9, %eax
je 0x57b1
movsbl -0x141(%rbp), %eax
cmpl $0xa, %eax
je 0x57b1
movsbl -0x141(%rbp), %eax
cmpl $0x3d, %eax
jne 0x58ce
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jle 0x57c4
jmp 0x55bf
movq -0x160(%rbp), %rdi
movl -0x140(%rbp), %ecx
movq -0x138(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rsi
callq 0x5e50
movq %rax, -0x150(%rbp)
xorl %eax, %eax
cmpq -0x150(%rbp), %rax
jne 0x5821
movq -0x160(%rbp), %rdi
movl $0x4, %esi
callq 0x5900
movq $0x0, -0x8(%rbp)
jmp 0x58ec
movq -0x150(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x150(%rbp), %rdi
callq 0x40a0
movq %rax, %rcx
movq -0x178(%rbp), %rax
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, -0x179(%rbp)
je 0x5897
movq -0x158(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x9, %ecx
movb %al, -0x179(%rbp)
je 0x5897
movq -0x158(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
sete %al
movb %al, -0x179(%rbp)
movb -0x179(%rbp), %al
testb $0x1, %al
jne 0x58a3
jmp 0x58c1
movq -0x158(%rbp), %rax
movb $0x0, (%rax)
movq -0x158(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x5853
movq -0x150(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x58ec
jmp 0x55bf
movq -0x160(%rbp), %rdi
movl $0x4, %esi
callq 0x5900
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)