#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen_helper(char const *, char *);
int parse_config_line(char *, char **, int);
int load_config(const char *filename, char **argv, int max_args) {
  FILE *fp = fopen_helper(filename, "r");
  if (!fp)
    return 0;
  int argc = 0;
  char line[128];
  while (fgets(line, sizeof(line), fp) && argc < max_args) {
    int new_args = parse_config_line(line, argv, max_args - argc);
    argc += new_args;
    argv += new_args;
  }
  fclose(fp);
  return argc;
}


x86 assembly (clang, -O0):
<load_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen_helper@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <load_config+0x41>
movl   $0x0,-0x4(%rbp)
jmp    1228 <load_config+0xe8>
movl   $0x0,-0x2c(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xb5(%rbp)
je     11c1 <load_config+0x81>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setl   %al
mov    %al,-0xb5(%rbp)
mov    -0xb5(%rbp),%al
test   $0x1,%al
jne    11d4 <load_config+0x94>
jmp    1219 <load_config+0xd9>
lea    -0xb0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    -0x2c(%rbp),%edx
call   1050 <parse_config_line@plt>
mov    %eax,-0xb4(%rbp)
mov    -0xb4(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0xb4(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1188 <load_config+0x48>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_config>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %edx,%r15d
mov    %rsi,%r13
lea    0xea2(%rip),%rsi
call   1060 <fopen_helper@plt>
test   %rax,%rax
je     11d0 <load_config+0x90>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11c6 <load_config+0x86>
test   %r15d,%r15d
jle    11c6 <load_config+0x86>
xor    %ebp,%ebp
mov    %rsp,%r12
nopl   0x0(%rax)
mov    %r15d,%edx
sub    %ebp,%edx
mov    %r12,%rdi
mov    %r13,%rsi
call   1050 <parse_config_line@plt>
mov    %eax,%ebx
add    %eax,%ebp
mov    %r12,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11c6 <load_config+0x86>
movslq %ebx,%rax
lea    0x0(%r13,%rax,8),%r13
cmp    %r15d,%ebp
jl     1190 <load_config+0x50>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11d2 <load_config+0x92>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @load_config(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca [128 x i8]
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = call i32* @fopen_helper(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 %17, label %16
16: ; preds = %3
store i32 0, i32* %4
br label %47
17: ; preds = %3
store i32 0, i32* %9
br label %18
18: ; preds = %29, %17
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%20 = load i32*, i32** %8
%21 = call i64 @fgets(i8* %19, i32 128, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* %9
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br label %27
27: ; preds = %23, %18
%28 = phi i1 [ false, %18 ], [ %26, %23 ]
br i1 %28, label %29, label %43
29: ; preds = %27
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%31 = load i8**, i8*** %6
%32 = load i32, i32* %7
%33 = load i32, i32* %9
%34 = sub nsw i32 %32, %33
%35 = call i32 @parse_config_line(i8* %30, i8** %31, i32 %34)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = load i32, i32* %9
%38 = add nsw i32 %37, %36
store i32 %38, i32* %9
%39 = load i32, i32* %11
%40 = load i8**, i8*** %6
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8*, i8** %40, i64 %41
store i8** %42, i8*** %6
br label %18
43: ; preds = %27
%44 = load i32*, i32** %8
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %9
store i32 %46, i32* %4
br label %47
47: ; preds = %43, %16
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @load_config(i8* %0, i8** %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = tail call i32* @fopen_helper(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %28, label %7
7: ; preds = %3
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = call i64 @fgets(i8* %8, i32 128, i32* %5)
%10 = icmp ne i64 %9, 0
%11 = icmp sgt i32 %2, 0
%12 = and i1 %10, %11
br i1 %12, label %13, label %25
13: ; preds = %7, %13
%14 = phi i32 [ %18, %13 ], [ 0, %7 ]
%15 = phi i8** [ %20, %13 ], [ %1, %7 ]
%16 = sub nsw i32 %2, %14
%17 = call i32 @parse_config_line(i8* %8, i8** %15, i32 %16)
%18 = add nsw i32 %17, %14
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8*, i8** %15, i64 %19
%21 = call i64 @fgets(i8* %8, i32 128, i32* %5)
%22 = icmp ne i64 %21, 0
%23 = icmp slt i32 %18, %2
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %13, label %25
25: ; preds = %13, %7
%26 = phi i32 [ 0, %7 ], [ %18, %13 ]
%27 = call i32 @fclose(i32* %5)
br label %28
28: ; preds = %3, %25
%29 = phi i32 [ %26, %25 ], [ 0, %3 ]
ret i32 %29
}