#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 */
int /*<<< orphan*/ buffer_data_at(struct buffer *, int);
int buffer_get_line_beginning(struct buffer *);
int buffer_get_line_end(struct buffer *);
scalar_t__ is_utf8(int /*<<< orphan*/);
int buffer_get_line_length(struct buffer *buf) {
  int p, end, l = 0;
  p = buffer_get_line_beginning(buf);
  end = buffer_get_line_end(buf);
  while (p < end) {
    if (is_utf8(buffer_data_at(buf, p)))
      l++;
    p++;
  }
  return l;
}


x86 assembly (clang, -O0):
<buffer_get_line_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <buffer_get_line_beginning@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <buffer_get_line_end@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11ab <buffer_get_line_length+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <buffer_data_at@plt>
mov    %eax,%edi
call   1030 <is_utf8@plt>
cmp    $0x0,%rax
je     119d <buffer_get_line_length+0x5d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116b <buffer_get_line_length+0x2b>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_get_line_length>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1060 <buffer_get_line_beginning@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <buffer_get_line_end@plt>
xor    %ebx,%ebx
cmp    %eax,%ebp
jge    1190 <buffer_get_line_length+0x50>
mov    %eax,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <buffer_data_at@plt>
mov    %eax,%edi
call   1030 <is_utf8@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    1170 <buffer_get_line_length+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
define i32 @buffer_get_line_length(%struct.buffer* %0) {
%2 = alloca %struct.buffer*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store %struct.buffer* %0, %struct.buffer** %2
store i32 0, i32* %5
%6 = load %struct.buffer*, %struct.buffer** %2
%7 = call i32 @buffer_get_line_beginning(%struct.buffer* %6)
store i32 %7, i32* %3
%8 = load %struct.buffer*, %struct.buffer** %2
%9 = call i32 @buffer_get_line_end(%struct.buffer* %8)
store i32 %9, i32* %4
br label %10
10: ; preds = %23, %1
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load %struct.buffer*, %struct.buffer** %2
%16 = load i32, i32* %3
%17 = call i32 @buffer_data_at(%struct.buffer* %15, i32 %16)
%18 = call i64 @is_utf8(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
store i32 %22, i32* %5
br label %23
23: ; preds = %20, %14
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %10
26: ; preds = %10
%27 = load i32, i32* %5
ret i32 %27
}

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