#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 buffer {
  int dummy;
};

/* Variables and functions */
char buffer_data_at(struct buffer *, int);
scalar_t__ is_position_in_region(int, int, int);
scalar_t__ is_utf8(char);
int region_newlines(struct buffer *buf, int beg, int end) {
  int nl = 0, p = beg, way;
  char c;
  way = beg < end ? 1 : -1;
  do {
    c = buffer_data_at(buf, p);
    if (is_utf8(c) && c == '\n')
      nl++;
    p += way;
  } while (is_position_in_region(p, beg, end));
  return nl;
}


x86 assembly (clang, -O0):
<region_newlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%esi
mov    $0xffffffff,%eax
mov    $0x1,%ecx
cmp    %esi,%edx
cmovl  %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <buffer_data_at@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%edi
call   1030 <is_utf8@plt>
cmp    $0x0,%rax
je     119f <region_newlines+0x6f>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    119f <region_newlines+0x6f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <is_position_in_region@plt>
cmp    $0x0,%rax
jne    1167 <region_newlines+0x37>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<region_newlines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,(%rsp)
xor    %eax,%eax
cmp    %edx,%esi
setl   %al
lea    (%rax,%rax,1),%r14d
add    $0xffffffff,%r14d
xor    %ebx,%ebx
mov    %esi,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rdi
mov    %ebp,%esi
call   1050 <buffer_data_at@plt>
movsbl %al,%r13d
mov    %r13d,%edi
call   1030 <is_utf8@plt>
test   %rax,%rax
setne  %al
cmp    $0xa,%r13b
sete   %cl
and    %al,%cl
movzbl %cl,%eax
add    %eax,%ebx
add    %r14d,%ebp
mov    %ebp,%edi
mov    %r12d,%esi
mov    %r15d,%edx
call   1040 <is_position_in_region@plt>
test   %rax,%rax
jne    1160 <region_newlines+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
define i32 @region_newlines(%struct.buffer* %0, i32 %1, i32 %2) {
%4 = alloca %struct.buffer*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store %struct.buffer* %0, %struct.buffer** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* %5
store i32 %11, i32* %8
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
%15 = zext i1 %14 to i64
%16 = select i1 %14, i32 1, i32 -1
store i32 %16, i32* %9
br label %17
17: ; preds = %35, %3
%18 = load %struct.buffer*, %struct.buffer** %4
%19 = load i32, i32* %8
%20 = call signext i8 @buffer_data_at(%struct.buffer* %18, i32 %19)
store i8 %20, i8* %10
%21 = load i8, i8* %10
%22 = call i64 @is_utf8(i8 signext %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i8, i8* %10
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %31
31: ; preds = %28, %24, %17
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = add nsw i32 %33, %32
store i32 %34, i32* %8
br label %35
35: ; preds = %31
%36 = load i32, i32* %8
%37 = load i32, i32* %5
%38 = load i32, i32* %6
%39 = call i64 @is_position_in_region(i32 %36, i32 %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %17, label %41
41: ; preds = %35
%42 = load i32, i32* %7
ret i32 %42
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
define i32 @region_newlines(%struct.buffer* %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %1, %2
%5 = select i1 %4, i32 1, i32 -1
br label %6
6: ; preds = %6, %3
%7 = phi i32 [ %1, %3 ], [ %16, %6 ]
%8 = phi i32 [ 0, %3 ], [ %15, %6 ]
%9 = tail call signext i8 @buffer_data_at(%struct.buffer* %0, i32 %7)
%10 = tail call i64 @is_utf8(i8 signext %9)
%11 = icmp ne i64 %10, 0
%12 = icmp eq i8 %9, 10
%13 = and i1 %12, %11
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %8, %14
%16 = add nsw i32 %7, %5
%17 = tail call i64 @is_position_in_region(i32 %16, i32 %1, i32 %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %6
19: ; preds = %6
ret i32 %15
}