#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 */
struct stat {
  int st_size;
};
typedef int ssize_t;

/* Variables and functions */
int __findline(int, int (*)(char const *, void *), void *, int /*<<< orphan*/,
               int, int);
scalar_t__ fstat(int, struct stat *);
ssize_t findfirstline(int fd, int (*comp)(const char *, void *), void *param,
                      int maxlen) {
  struct stat state;
  ssize_t pos;
  if (fstat(fd, &state))
    return -1;
  ssize_t init_pos = __findline(fd, comp, param, 0, state.st_size, maxlen);
  while (init_pos > 0) {
    pos = __findline(fd, comp, param, 0, init_pos, maxlen);
    if (pos == -1)
      break;
    init_pos = pos;
  }
  return init_pos;
}


x86 assembly (clang, -O0):
<findfirstline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <fstat@plt>
cmp    $0x0,%rax
je     1158 <findfirstline+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <findfirstline+0x9c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
xor    %ecx,%ecx
call   1040 <__findline@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    11b6 <findfirstline+0x96>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
xor    %ecx,%ecx
call   1040 <__findline@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ab <findfirstline+0x8b>
jmp    11b6 <findfirstline+0x96>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1175 <findfirstline+0x55>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findfirstline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1030 <fstat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1190 <findfirstline+0x70>
mov    0x8(%rsp),%r8d
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
mov    %r14d,%r9d
call   1040 <__findline@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%ebp
test   %eax,%eax
jle    1190 <findfirstline+0x70>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
mov    %ebp,%r8d
mov    %r14d,%r9d
call   1040 <__findline@plt>
cmp    $0xffffffff,%eax
jne    1170 <findfirstline+0x50>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @findfirstline(i32 %0, i32 (i8*, i8*)* %1, i8* %2, i32 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = call i64 @fstat(i32 %0, %struct.stat* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %4
%10 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @__findline(i32 %0, i32 (i8*, i8*)* %1, i8* %2, i32 0, i32 %11, i32 %3)
br label %13
13: ; preds = %16, %9
%14 = phi i32 [ %12, %9 ], [ %17, %16 ]
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @__findline(i32 %0, i32 (i8*, i8*)* %1, i8* %2, i32 0, i32 %14, i32 %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %13
19: ; preds = %13, %16, %4
%20 = phi i32 [ -1, %4 ], [ %14, %16 ], [ %14, %13 ]
ret i32 %20
}