#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 *dstrpos(char const *, size_t);
int /*<<< orphan*/ strlen(char *);
scalar_t__ utf8_decode(char *, int /*<<< orphan*/, unsigned long *);
unsigned long dstridx(const char *str, size_t pos) {
  unsigned long val;
  char *ptr = dstrpos(str, pos);
  if (ptr == NULL)
    return -1;
  val = 0;
  if (utf8_decode(ptr, strlen(ptr), &val) <= 0)
    return -1;
  return val;
}


x86 assembly (clang, -O0):
<dstridx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <dstrpos@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1169 <dstridx+0x39>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b0 <dstridx+0x80>
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
call   1050 <utf8_decode@plt>
cmp    $0x0,%rax
jg     11a8 <dstridx+0x78>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b0 <dstridx+0x80>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dstridx>:
push   %rbx
sub    $0x10,%rsp
call   1040 <dstrpos@plt>
test   %rax,%rax
je     1178 <dstridx+0x48>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
mov    %rax,%rdi
call   1030 <strlen@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <utf8_decode@plt>
mov    %rax,%rcx
mov    $0xffffffffffffffff,%rax
test   %rcx,%rcx
jle    117f <dstridx+0x4f>
mov    0x8(%rsp),%rax
jmp    117f <dstridx+0x4f>
mov    $0xffffffffffffffff,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i64 @dstridx(i8* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = load i64, i64* %5
%10 = call i8* @dstrpos(i8* %8, i64 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i64 -1, i64* %3
br label %23
14: ; preds = %2
store i64 0, i64* %6
%15 = load i8*, i8** %7
%16 = load i8*, i8** %7
%17 = call i32 @strlen(i8* %16)
%18 = call i64 @utf8_decode(i8* %15, i32 %17, i64* %6)
%19 = icmp sle i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
store i64 -1, i64* %3
br label %23
21: ; preds = %14
%22 = load i64, i64* %6
store i64 %22, i64* %3
br label %23
23: ; preds = %21, %20, %13
%24 = load i64, i64* %3
ret i64 %24
}

LLVM IR (-O2):
define i64 @dstridx(i8* %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i8* @dstrpos(i8* %0, i64 %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %13, label %7
7: ; preds = %2
store i64 0, i64* %3
%8 = tail call i32 @strlen(i8* %5)
%9 = call i64 @utf8_decode(i8* %5, i32 %8, i64* %3)
%10 = icmp slt i64 %9, 1
%11 = load i64, i64* %3
%12 = select i1 %10, i64 -1, i64 %11
br label %13
13: ; preds = %7, %2
%14 = phi i64 [ -1, %2 ], [ %12, %7 ]
ret i64 %14
}