#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 strlen(char *);
int lcd_getprevcharacterpos(char *str, int curpos) {
  if (curpos == 0)
    curpos = strlen(str) - 1;
  else
    curpos--;
  while (curpos > 0 && (str[curpos] & '\xC0') == '\x80')
    curpos--;
  return curpos;
}


x86 assembly (clang, -O0):
<lcd_getprevcharacterpos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    113d <lcd_getprevcharacterpos+0x2d>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1146 <lcd_getprevcharacterpos+0x36>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    114b <lcd_getprevcharacterpos+0x3b>
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jle    1172 <lcd_getprevcharacterpos+0x62>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xffffffc0,%eax
cmp    $0xffffff80,%eax
sete   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1182 <lcd_getprevcharacterpos+0x72>
jmp    1190 <lcd_getprevcharacterpos+0x80>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    114b <lcd_getprevcharacterpos+0x3b>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lcd_getprevcharacterpos>:
push   %rbx
mov    %rdi,%rbx
test   %esi,%esi
jne    1122 <lcd_getprevcharacterpos+0x12>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%esi
mov    %esi,%ecx
test   %esi,%esi
mov    $0x1,%eax
cmovle %esi,%eax
add    $0xffffffff,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x2,%ecx
jl     1159 <lcd_getprevcharacterpos+0x49>
add    $0xffffffffffffffff,%rcx
mov    %ecx,%edx
movzbl (%rbx,%rdx,1),%edx
and    $0xc0,%dl
cmp    $0x80,%dl
je     1140 <lcd_getprevcharacterpos+0x30>
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @lcd_getprevcharacterpos(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = sub nsw i32 %9, 1
store i32 %10, i32* %4
br label %14
11: ; preds = %2
%12 = load i32, i32* %4
%13 = add nsw i32 %12, -1
store i32 %13, i32* %4
br label %14
14: ; preds = %11, %7
br label %15
15: ; preds = %29, %14
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = and i32 %24, -64
%26 = icmp eq i32 %25, -128
br label %27
27: ; preds = %18, %15
%28 = phi i1 [ false, %15 ], [ %26, %18 ]
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = load i32, i32* %4
%31 = add nsw i32 %30, -1
store i32 %31, i32* %4
br label %15
32: ; preds = %27
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
define i32 @lcd_getprevcharacterpos(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %0)
br label %6
6: ; preds = %2, %4
%7 = phi i32 [ %5, %4 ], [ %1, %2 ]
%8 = zext i32 %7 to i64
%9 = call i32 @llvm.smin.i32(i32 %7, i32 1)
%10 = add i32 %9, -1
br label %11
11: ; preds = %16, %6
%12 = phi i64 [ %13, %16 ], [ %8, %6 ]
%13 = add nsw i64 %12, -1
%14 = trunc i64 %12 to i32
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = and i64 %13, 4294967295
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = and i8 %19, -64
%21 = icmp eq i8 %20, -128
br i1 %21, label %11, label %22
22: ; preds = %16
%23 = trunc i64 %13 to i32
br label %24
24: ; preds = %11, %22
%25 = phi i32 [ %23, %22 ], [ %10, %11 ]
ret i32 %25
}