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

/* Variables and functions */
int mbtowc(int /*<<< orphan*/ *, char const *, int);
int num_invalid(const char *s, int len) {
  int retval = 0;
  int i = 0;
  int num_inv = 0;
  wchar_t wchar;
  const char *t;
  t = s;
  for (i = 0; i < len; t++, i++) {
    retval = mbtowc(&wchar, t, len - i);
    if (retval == -1)
      num_inv++;
    else
      return -1;
  }
  return num_inv;
}


x86 assembly (clang, -O0):
<num_invalid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a8 <num_invalid+0x98>
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
lea    -0x24(%rbp),%rdi
call   1030 <mbtowc@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    117d <num_invalid+0x6d>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1189 <num_invalid+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <num_invalid+0x9e>
jmp    118e <num_invalid+0x7e>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1143 <num_invalid+0x33>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_invalid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    114d <num_invalid+0x3d>
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x4(%rsp),%r15
mov    %esi,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <mbtowc@plt>
cmp    $0xffffffff,%eax
jne    1152 <num_invalid+0x42>
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    1130 <num_invalid+0x20>
jmp    1158 <num_invalid+0x48>
xor    %r14d,%r14d
jmp    1158 <num_invalid+0x48>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @num_invalid(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %16
6: ; preds = %2, %12
%7 = phi i8* [ %14, %12 ], [ %0, %2 ]
%8 = phi i32 [ %13, %12 ], [ 0, %2 ]
%9 = sub nsw i32 %1, %8
%10 = call i32 @mbtowc(i32* %3, i8* %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = add nuw nsw i32 %8, 1
%14 = getelementptr inbounds i8, i8* %7, i64 1
%15 = icmp eq i32 %13, %1
br i1 %15, label %16, label %6
16: ; preds = %6, %12, %2
%17 = phi i32 [ 0, %2 ], [ %1, %12 ], [ -1, %6 ]
ret i32 %17
}