#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 */
char *utf8_next_char(char const *);
int utf8_pointer_to_offset(char const *str, char const *const pos) {
  int ret = 0;
  if (pos < str) {
    ret = -utf8_pointer_to_offset(pos, str);
  } else {
    for (; str < pos; ret++) {
      str = utf8_next_char(str);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<utf8_pointer_to_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1160 <utf8_pointer_to_offset+0x40>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <utf8_pointer_to_offset@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1193 <utf8_pointer_to_offset+0x73>
jmp    1165 <utf8_pointer_to_offset+0x45>
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    118e <utf8_pointer_to_offset+0x6e>
mov    -0x8(%rbp),%rdi
call   1040 <utf8_next_char@plt>
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1165 <utf8_pointer_to_offset+0x45>
jmp    1193 <utf8_pointer_to_offset+0x73>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_pointer_to_offset>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rax
cmp    %rdi,%rsi
jae    113f <utf8_pointer_to_offset+0x1f>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <utf8_pointer_to_offset@plt>
mov    %eax,%ebp
neg    %ebp
jmp    1160 <utf8_pointer_to_offset+0x40>
xor    %ebp,%ebp
cmp    %rbx,%rax
jae    1160 <utf8_pointer_to_offset+0x40>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   1040 <utf8_next_char@plt>
add    $0x1,%ebp
cmp    %rbx,%rax
jb     1150 <utf8_pointer_to_offset+0x30>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @utf8_pointer_to_offset(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
%6 = load i8*, i8** %4
%7 = load i8*, i8** %3
%8 = icmp ult i8* %6, %7
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = load i8*, i8** %3
%12 = call i32 @utf8_pointer_to_offset(i8* %10, i8* %11)
%13 = sub nsw i32 0, %12
store i32 %13, i32* %5
br label %26
14: ; preds = %2
br label %15
15: ; preds = %22, %14
%16 = load i8*, i8** %3
%17 = load i8*, i8** %4
%18 = icmp ult i8* %16, %17
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = call i8* @utf8_next_char(i8* %20)
store i8* %21, i8** %3
br label %22
22: ; preds = %19
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %15
25: ; preds = %15
br label %26
26: ; preds = %25, %9
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
define i32 @utf8_pointer_to_offset(i8* %0, i8* %1) {
%3 = icmp ult i8* %1, %0
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = icmp ult i8* %0, %1
br i1 %5, label %11, label %6
6: ; preds = %11, %4, %8
%7 = phi i32 [ %10, %8 ], [ 0, %4 ], [ %15, %11 ]
ret i32 %7
8: ; preds = %2
%9 = tail call i32 @utf8_pointer_to_offset(i8* %1, i8* %0)
%10 = sub nsw i32 0, %9
br label %6
11: ; preds = %4, %11
%12 = phi i32 [ %15, %11 ], [ 0, %4 ]
%13 = phi i8* [ %14, %11 ], [ %0, %4 ]
%14 = tail call i8* @utf8_next_char(i8* %13)
%15 = add nuw nsw i32 %12, 1
%16 = icmp ult i8* %14, %1
br i1 %16, label %11, label %6
}