#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ line_is_empty(char *);
int /*<<< orphan*/ mark_line_end(char *);
char *skip_chars(char *);
char *skip_leading(char *);
int /*<<< orphan*/ trim_line_end(char *);
int brain_confscan_(char const *in_pathname,
                    int (*in_line_handler)(long in_line_number,
                                           char const *in_key,
                                           char const *in_value)) {
  int err = 0;
  long line_number = 0;
  FILE *fp = fopen(in_pathname, "r");
  if (!fp)
    return errno;
  static char line_buffer[4096];
  while (fgets(line_buffer, 4096, fp)) {
    line_number++;
    mark_line_end(line_buffer);
    if (line_is_empty(line_buffer))
      continue;
    trim_line_end(line_buffer);
    char *name = skip_leading(line_buffer);
    char *name_end = skip_chars(name);
    char *value = skip_leading(name_end);
    *name_end = 0;
    err = in_line_handler(line_number, name, value);
    if (err)
      break;
  }
  fclose(fp);
  return err;
}


x86 assembly (clang, -O0):
<brain_confscan_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cf <brain_confscan_+0x4f>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129b <brain_confscan_+0x11b>
jmp    11d4 <brain_confscan_+0x54>
mov    -0x30(%rbp),%rdx
lea    0x2ea1(%rip),%rdi
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     128c <brain_confscan_+0x10c>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
lea    0x2e7a(%rip),%rdi
call   1080 <mark_line_end@plt>
lea    0x2e6e(%rip),%rdi
call   1060 <line_is_empty@plt>
cmp    $0x0,%rax
je     1226 <brain_confscan_+0xa6>
jmp    11d4 <brain_confscan_+0x54>
lea    0x2e53(%rip),%rdi
call   1050 <trim_line_end@plt>
lea    0x2e47(%rip),%rdi
call   1090 <skip_leading@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <skip_chars@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1090 <skip_leading@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   *%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1287 <brain_confscan_+0x107>
jmp    128c <brain_confscan_+0x10c>
jmp    11d4 <brain_confscan_+0x54>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brain_confscan_>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     124c <brain_confscan_+0xcc>
mov    %rax,%r12
lea    0x2ed3(%rip),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1259 <brain_confscan_+0xd9>
mov    $0x1,%ebp
lea    0x2eaf(%rip),%r13
jmp    11f9 <brain_confscan_+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
add    $0x1,%rbp
test   %rax,%rax
je     1257 <brain_confscan_+0xd7>
mov    %r13,%rdi
call   1080 <mark_line_end@plt>
mov    %r13,%rdi
call   1060 <line_is_empty@plt>
test   %rax,%rax
jne    11e0 <brain_confscan_+0x60>
mov    %r13,%rdi
call   1050 <trim_line_end@plt>
mov    %r13,%rdi
call   1090 <skip_leading@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <skip_chars@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <skip_leading@plt>
movb   $0x0,(%r15)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   *%r14
test   %eax,%eax
je     11e0 <brain_confscan_+0x60>
mov    %eax,%ebx
jmp    1259 <brain_confscan_+0xd9>
mov    0x2d8d(%rip),%rax
mov    (%rax),%ebx
jmp    1261 <brain_confscan_+0xe1>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@brain_confscan_.line_buffer = internal global [4096 x i8] zeroinitializer
define i32 @brain_confscan_(i8* %0, i32 (i64, i8*, i8*)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32 (i64, i8*, i8*)*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %4
store i32 (i64, i8*, i8*)* %1, i32 (i64, i8*, i8*)** %5
store i32 0, i32* %6
store i64 0, i64* %7
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = load i32, i32* @errno
store i32 %17, i32* %3
br label %51
18: ; preds = %2
br label %19
19: ; preds = %46, %29, %18
%20 = load i32*, i32** %8
%21 = call i64 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0), i32 4096, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %47
23: ; preds = %19
%24 = load i64, i64* %7
%25 = add nsw i64 %24, 1
store i64 %25, i64* %7
%26 = call i32 @mark_line_end(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
%27 = call i64 @line_is_empty(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
br label %19
30: ; preds = %23
%31 = call i32 @trim_line_end(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
%32 = call i8* @skip_leading(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = call i8* @skip_chars(i8* %33)
store i8* %34, i8** %10
%35 = load i8*, i8** %10
%36 = call i8* @skip_leading(i8* %35)
store i8* %36, i8** %11
%37 = load i8*, i8** %10
store i8 0, i8* %37
%38 = load i32 (i64, i8*, i8*)*, i32 (i64, i8*, i8*)** %5
%39 = load i64, i64* %7
%40 = load i8*, i8** %9
%41 = load i8*, i8** %11
%42 = call i32 %38(i64 %39, i8* %40, i8* %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %30
br label %47
46: ; preds = %30
br label %19
47: ; preds = %45, %19
%48 = load i32*, i32** %8
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* %6
store i32 %50, i32* %3
br label %51
51: ; preds = %47, %16
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@brain_confscan_.line_buffer = internal global [4096 x i8] zeroinitializer
define i32 @brain_confscan_(i8* nocapture %0, i32 (i64, i8*, i8*)* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i64 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0), i32 4096, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %26, label %10
8: ; preds = %2
%9 = load i32, i32* @errno
br label %29
10: ; preds = %5, %16
%11 = phi i64 [ %12, %16 ], [ 0, %5 ]
%12 = add nuw nsw i64 %11, 1
%13 = tail call i32 @mark_line_end(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
%14 = tail call i64 @line_is_empty(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %10, %19
%17 = tail call i64 @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0), i32 4096, i32* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %10
19: ; preds = %10
%20 = tail call i32 @trim_line_end(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
%21 = tail call i8* @skip_leading(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @brain_confscan_.line_buffer, i64 0, i64 0))
%22 = tail call i8* @skip_chars(i8* %21)
%23 = tail call i8* @skip_leading(i8* %22)
store i8 0, i8* %22
%24 = tail call i32 %1(i64 %12, i8* %21, i8* %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %16, label %26
26: ; preds = %16, %19, %5
%27 = phi i32 [ 0, %5 ], [ %24, %19 ], [ 0, %16 ]
%28 = tail call i32 @fclose(i32* %3)
br label %29
29: ; preds = %26, %8
%30 = phi i32 [ %27, %26 ], [ %9, %8 ]
ret i32 %30
}