#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ get_next_line(int, char **);
int open(char const *, int);
int for_each_line_file(const char *file_path, int (*f)(const char *, void *),
                       int oflag, void *param) {
  int fd;
  int user_ret;
  char *line;
  if ((fd = open(file_path, oflag)) < 0)
    return (-1);
  while (get_next_line(fd, &line) > 0) {
    if ((user_ret = f(line, param)) != 0) {
      free(line);
      close(fd);
      return (user_ret);
    }
    free(line);
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<for_each_line_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    117b <for_each_line_file+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <for_each_line_file+0xa9>
jmp    1180 <for_each_line_file+0x40>
mov    -0x2c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1060 <get_next_line@plt>
cmp    $0x0,%rax
jle    11da <for_each_line_file+0x9a>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     11cc <for_each_line_file+0x8c>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e9 <for_each_line_file+0xa9>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1180 <for_each_line_file+0x40>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<for_each_line_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%r15
mov    %edx,%esi
call   1050 <open@plt>
test   %eax,%eax
js     11aa <for_each_line_file+0x6a>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <get_next_line@plt>
xor    %r12d,%r12d
test   %rax,%rax
jle    11b5 <for_each_line_file+0x75>
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rsp),%rdi
mov    %r14,%rsi
call   *%r15
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jne    11b2 <for_each_line_file+0x72>
mov    %ebx,%edi
mov    %r13,%rsi
call   1060 <get_next_line@plt>
test   %rax,%rax
jg     1180 <for_each_line_file+0x40>
jmp    11b5 <for_each_line_file+0x75>
mov    $0xffffffff,%r12d
jmp    11bc <for_each_line_file+0x7c>
mov    %ebp,%r12d
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @for_each_line_file(i8* %0, i32 (i8*, i8*)* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32 (i8*, i8*)*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %6
%14 = load i32, i32* %8
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %41
18: ; preds = %4
br label %19
19: ; preds = %35, %18
%20 = load i32, i32* %10
%21 = call i64 @get_next_line(i32 %20, i8** %12)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %38
23: ; preds = %19
%24 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %7
%25 = load i8*, i8** %12
%26 = load i8*, i8** %9
%27 = call i32 %24(i8* %25, i8* %26)
store i32 %27, i32* %11
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = load i8*, i8** %12
%31 = call i32 @free(i8* %30)
%32 = load i32, i32* %10
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %11
store i32 %34, i32* %5
br label %41
35: ; preds = %23
%36 = load i8*, i8** %12
%37 = call i32 @free(i8* %36)
br label %19
38: ; preds = %19
%39 = load i32, i32* %10
%40 = call i32 @close(i32 %39)
store i32 0, i32* %5
br label %41
41: ; preds = %38, %29, %17
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
define i32 @for_each_line_file(i8* nocapture %0, i32 (i8*, i8*)* nocapture %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = tail call i32 @open(i8* %0, i32 %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %24, label %9
9: ; preds = %4
%10 = call i64 @get_next_line(i32 %7, i8** %5)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9, %18
%13 = load i8*, i8** %5
%14 = call i32 %1(i8* %13, i8* %3)
%15 = icmp eq i32 %14, 0
%16 = load i8*, i8** %5
%17 = call i32 @free(i8* %16)
br i1 %15, label %18, label %21
18: ; preds = %12
%19 = call i64 @get_next_line(i32 %7, i8** %5)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %12, label %21
21: ; preds = %18, %12, %9
%22 = phi i32 [ 0, %9 ], [ 0, %18 ], [ %14, %12 ]
%23 = call i32 @close(i32 %7)
br label %24
24: ; preds = %21, %4
%25 = phi i32 [ -1, %4 ], [ %22, %21 ]
ret i32 %25
}