#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 count_chr(char *, char, int);
char *memrchr(char *, char, int);
int block_count_nl(char *buf, int len, int *nl_off) {
  int count;
  char *nl;
  count = count_chr(buf, '\n', len);
  if (count == 0) {
    return 0;
  }
  nl = memrchr(buf, '\n', len);
  *nl_off = nl - buf;
  if (*nl_off == len - 1) {
    count--;
    nl = memrchr(buf, '\n', *nl_off);
    *nl_off = nl - buf;
    if (nl == NULL) {
      return 0;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<block_count_nl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0xa,%esi
call   1030 <count_chr@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    115d <block_count_nl+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    11ef <block_count_nl+0xcf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0xa,%esi
call   1040 <memrchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11e9 <block_count_nl+0xc9>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1040 <memrchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x30(%rbp)
jne    11e4 <block_count_nl+0xc4>
movl   $0x0,-0x4(%rbp)
jmp    11ef <block_count_nl+0xcf>
jmp    11e9 <block_count_nl+0xc9>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_count_nl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0xa,%esi
mov    %ebp,%edx
call   1030 <count_chr@plt>
xor    %r15d,%r15d
test   %eax,%eax
je     1183 <block_count_nl+0x63>
mov    %eax,%r12d
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %ebp,%edx
call   1040 <memrchr@plt>
sub    %ebx,%eax
mov    %eax,(%r14)
add    $0xffffffff,%ebp
cmp    %eax,%ebp
jne    1180 <block_count_nl+0x60>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %eax,%edx
call   1040 <memrchr@plt>
mov    %eax,%ecx
sub    %ebx,%ecx
mov    %ecx,(%r14)
test   %rax,%rax
je     1183 <block_count_nl+0x63>
add    $0xffffffff,%r12d
mov    %r12d,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @block_count_nl(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @count_chr(i8* %10, i8 signext 10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 0, i32* %4
br label %52
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call i8* @memrchr(i8* %17, i8 signext 10, i32 %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = load i8*, i8** %5
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = load i32*, i32** %7
store i32 %25, i32* %26
%27 = load i32*, i32** %7
%28 = load i32, i32* %27
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, 1
%31 = icmp eq i32 %28, %30
br i1 %31, label %32, label %50
32: ; preds = %16
%33 = load i32, i32* %8
%34 = add nsw i32 %33, -1
store i32 %34, i32* %8
%35 = load i8*, i8** %5
%36 = load i32*, i32** %7
%37 = load i32, i32* %36
%38 = call i8* @memrchr(i8* %35, i8 signext 10, i32 %37)
store i8* %38, i8** %9
%39 = load i8*, i8** %9
%40 = load i8*, i8** %5
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = load i32*, i32** %7
store i32 %44, i32* %45
%46 = load i8*, i8** %9
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %49
48: ; preds = %32
store i32 0, i32* %4
br label %52
49: ; preds = %32
br label %50
50: ; preds = %49, %16
%51 = load i32, i32* %8
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %48, %15
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
define i32 @block_count_nl(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @count_chr(i8* %0, i8 signext 10, i32 %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = tail call i8* @memrchr(i8* %0, i8 signext 10, i32 %1)
%8 = ptrtoint i8* %7 to i64
%9 = ptrtoint i8* %0 to i64
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
store i32 %11, i32* %2
%12 = add nsw i32 %1, -1
%13 = icmp eq i32 %12, %11
br i1 %13, label %14, label %21
14: ; preds = %6
%15 = add nsw i32 %4, -1
%16 = tail call i8* @memrchr(i8* %0, i8 signext 10, i32 %11)
%17 = ptrtoint i8* %16 to i64
%18 = sub i64 %17, %9
%19 = trunc i64 %18 to i32
store i32 %19, i32* %2
%20 = icmp eq i8* %16, null
br i1 %20, label %23, label %21
21: ; preds = %14, %6
%22 = phi i32 [ %15, %14 ], [ %4, %6 ]
br label %23
23: ; preds = %14, %3, %21
%24 = phi i32 [ %22, %21 ], [ 0, %3 ], [ 0, %14 ]
ret i32 %24
}