#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 */
typedef int wchar_t;

/* Variables and functions */
int mbtowc(int *, char const *, size_t);
int str_next(const char *str, size_t n, size_t *step) {
  if (!n) {
    *step = 0;
    return 0;
  }
  if (str[0] >= 128U) {
    wchar_t wc;
    int k = mbtowc(&wc, str, n);
    if (k < 0) {
      *step = 1;
      return -1;
    }
    *step = k;
    return wc;
  }
  *step = 1;
  return str[0];
}


x86 assembly (clang, -O0):
<str_next>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1146 <str_next+0x36>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11b8 <str_next+0xa8>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x80,%eax
jb     11a3 <str_next+0x93>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x24(%rbp),%rdi
call   1030 <mbtowc@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    118d <str_next+0x7d>
mov    -0x20(%rbp),%rax
movq   $0x1,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <str_next+0xa8>
movslq -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b8 <str_next+0xa8>
mov    -0x20(%rbp),%rax
movq   $0x1,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_next>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%rdx
test   %rsi,%rsi
je     1134 <str_next+0x24>
mov    %rdi,%rsi
cmpb   $0x0,(%rdi)
js     113f <str_next+0x2f>
movq   $0x1,(%rbx)
movsbl (%rsi),%eax
jmp    1164 <str_next+0x54>
movq   $0x0,(%rbx)
xor    %eax,%eax
jmp    1164 <str_next+0x54>
lea    0xc(%rsp),%rdi
call   1030 <mbtowc@plt>
test   %eax,%eax
js     1158 <str_next+0x48>
mov    %eax,%eax
mov    %rax,(%rbx)
mov    0xc(%rsp),%eax
jmp    1164 <str_next+0x54>
movq   $0x1,(%rbx)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @str_next(i8* %0, i64 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = load i64*, i64** %7
store i64 0, i64* %13
store i32 0, i32* %4
br label %39
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp uge i32 %18, 128
br i1 %19, label %20, label %33
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i32 @mbtowc(i32* %8, i8* %21, i64 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i64*, i64** %7
store i64 1, i64* %27
store i32 -1, i32* %4
br label %39
28: ; preds = %20
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = load i64*, i64** %7
store i64 %30, i64* %31
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %39
33: ; preds = %14
%34 = load i64*, i64** %7
store i64 1, i64* %34
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
store i32 %38, i32* %4
br label %39
39: ; preds = %33, %28, %26, %12
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
define i32 @str_next(i8* %0, i64 %1, i64* nocapture writeonly %2) {
%4 = alloca i32
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %7
6: ; preds = %3
store i64 0, i64* %2
br label %23
7: ; preds = %3
%8 = load i8, i8* %0
%9 = icmp slt i8 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = bitcast i32* %4 to i8*
%12 = call i32 @mbtowc(i32* %4, i8* %0, i64 %1)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i64 1, i64* %2
br label %18
15: ; preds = %10
%16 = zext i32 %12 to i64
store i64 %16, i64* %2
%17 = load i32, i32* %4
br label %18
18: ; preds = %15, %14
%19 = phi i32 [ -1, %14 ], [ %17, %15 ]
br label %23
20: ; preds = %7
store i64 1, i64* %2
%21 = load i8, i8* %0
%22 = sext i8 %21 to i32
br label %23
23: ; preds = %20, %18, %6
%24 = phi i32 [ %19, %18 ], [ %22, %20 ], [ 0, %6 ]
ret i32 %24
}