#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 */
scalar_t__ EILSEQ;
int /*<<< orphan*/ LC_CTYPE;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
size_t mbtowc(int *, char *, int);
int /*<<< orphan*/ setlocale(int /*<<< orphan*/, char *);
size_t strlen(char *);
int main() {
  wchar_t ws;
  char *c = "fn\xc3\xb6rd";
  size_t n = strlen(c);
  setlocale(LC_CTYPE, "de_DE.UTF8");
  ws = 0;
  assert(mbtowc(&ws, c, 6) == 1 && ws == 102);
  assert(mbtowc(&ws, c + 1, 5) == 1 && ws == 110);
  assert(mbtowc(&ws, c + 2, 4) == 2 && ws == 246);
  assert(mbtowc(&ws, c + 4, 2) == 1 && ws == 114);
  assert(mbtowc(&ws, c + 5, 1) == 1 && ws == 100);
  assert(mbtowc(&ws, c + 6, 1) == 0);
  errno = 0;
  c = "fnörd";
  assert(mbtowc(&ws, c, 6) == 1 && ws == 102);
  assert(mbtowc(&ws, c + 1, 5) == 1 && ws == 110);
  assert(mbtowc(&ws, c + 2, 4) == (size_t)-1 && errno == EILSEQ);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1060 <setlocale@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x8(%rbp),%rdi
mov    $0x6,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x1,%rcx
mov    %al,-0x19(%rbp)
jne    11b1 <main+0x71>
cmpl   $0x66,-0x8(%rbp)
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
lea    -0x8(%rbp),%rdi
mov    $0x5,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x1,%rcx
mov    %al,-0x1a(%rbp)
jne    11f0 <main+0xb0>
cmpl   $0x6e,-0x8(%rbp)
sete   %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
lea    -0x8(%rbp),%rdi
mov    $0x4,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x2,%rcx
mov    %al,-0x1b(%rbp)
jne    1232 <main+0xf2>
cmpl   $0xf6,-0x8(%rbp)
sete   %al
mov    %al,-0x1b(%rbp)
mov    -0x1b(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
lea    -0x8(%rbp),%rdi
mov    $0x2,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x1,%rcx
mov    %al,-0x1c(%rbp)
jne    1271 <main+0x131>
cmpl   $0x72,-0x8(%rbp)
sete   %al
mov    %al,-0x1c(%rbp)
mov    -0x1c(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
add    $0x5,%rsi
lea    -0x8(%rbp),%rdi
mov    $0x1,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x1,%rcx
mov    %al,-0x1d(%rbp)
jne    12b0 <main+0x170>
cmpl   $0x64,-0x8(%rbp)
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
add    $0x6,%rsi
lea    -0x8(%rbp),%rdi
mov    $0x1,%edx
call   1050 <mbtowc@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2ce5(%rip),%rax
movq   $0x0,(%rax)
lea    0xd07(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x8(%rbp),%rdi
mov    $0x6,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x1,%rcx
mov    %al,-0x1e(%rbp)
jne    132b <main+0x1eb>
cmpl   $0x66,-0x8(%rbp)
sete   %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
lea    -0x8(%rbp),%rdi
mov    $0x5,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x1,%rcx
mov    %al,-0x1f(%rbp)
jne    136a <main+0x22a>
cmpl   $0x6e,-0x8(%rbp)
sete   %al
mov    %al,-0x1f(%rbp)
mov    -0x1f(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
lea    -0x8(%rbp),%rdi
mov    $0x4,%edx
call   1050 <mbtowc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0xffffffffffffffff,%rcx
mov    %al,-0x20(%rbp)
jne    13b9 <main+0x279>
mov    0x2c2a(%rip),%rax
mov    (%rax),%rax
mov    0x2c40(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x20(%rbp)
mov    -0x20(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
lea    0xeba(%rip),%rsi
call   1050 <setlocale@plt>
movl   $0x0,0xc(%rsp)
lea    0xe9f(%rip),%r14
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x6,%edx
call   1040 <mbtowc@plt>
cmp    $0x1,%rax
sete   %al
cmpl   $0x66,0xc(%rsp)
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
lea    0xe6b(%rip),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x5,%edx
call   1040 <mbtowc@plt>
cmp    $0x1,%rax
sete   %al
cmpl   $0x6e,0xc(%rsp)
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
lea    0xe3c(%rip),%r12
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <mbtowc@plt>
cmp    $0x2,%rax
sete   %al
cmpl   $0xf6,0xc(%rsp)
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
lea    0xe0b(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1040 <mbtowc@plt>
cmp    $0x1,%rax
sete   %al
cmpl   $0x72,0xc(%rsp)
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
lea    0xddf(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1040 <mbtowc@plt>
cmp    $0x1,%rax
sete   %al
cmpl   $0x64,0xc(%rsp)
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
lea    0xdb3(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1040 <mbtowc@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1030 <assert@plt>
mov    0x2d5b(%rip),%r13
movq   $0x0,0x0(%r13)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x6,%edx
call   1040 <mbtowc@plt>
cmp    $0x1,%rax
sete   %al
cmpl   $0x66,0xc(%rsp)
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x5,%edx
call   1040 <mbtowc@plt>
cmp    $0x1,%rax
sete   %al
cmpl   $0x6e,0xc(%rsp)
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <mbtowc@plt>
mov    0x0(%r13),%rcx
mov    0x2d06(%rip),%rdx
xor    (%rdx),%rcx
not    %rax
xor    %edi,%edi
or     %rax,%rcx
sete   %dil
call   1030 <assert@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"fn\C3\B6rd\00"
@LC_CTYPE = global i32 0
@.str.1 = [11 x i8] c"de_DE.UTF8\00"
@errno = global i64 0
@EILSEQ = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @strlen(i8* %5)
store i64 %6, i64* %4
%7 = load i32, i32* @LC_CTYPE
%8 = call i32 @setlocale(i32 %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
%9 = load i8*, i8** %3
%10 = call i64 @mbtowc(i32* %2, i8* %9, i32 6)
%11 = icmp eq i64 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 102
br label %15
15: ; preds = %12, %0
%16 = phi i1 [ false, %0 ], [ %14, %12 ]
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = call i64 @mbtowc(i32* %2, i8* %20, i32 5)
%22 = icmp eq i64 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32, i32* %2
%25 = icmp eq i32 %24, 110
br label %26
26: ; preds = %23, %15
%27 = phi i1 [ false, %15 ], [ %25, %23 ]
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 2
%32 = call i64 @mbtowc(i32* %2, i8* %31, i32 4)
%33 = icmp eq i64 %32, 2
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i32, i32* %2
%36 = icmp eq i32 %35, 246
br label %37
37: ; preds = %34, %26
%38 = phi i1 [ false, %26 ], [ %36, %34 ]
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i64 4
%43 = call i64 @mbtowc(i32* %2, i8* %42, i32 2)
%44 = icmp eq i64 %43, 1
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i32, i32* %2
%47 = icmp eq i32 %46, 114
br label %48
48: ; preds = %45, %37
%49 = phi i1 [ false, %37 ], [ %47, %45 ]
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i8*, i8** %3
%53 = getelementptr inbounds i8, i8* %52, i64 5
%54 = call i64 @mbtowc(i32* %2, i8* %53, i32 1)
%55 = icmp eq i64 %54, 1
br i1 %55, label %56, label %59
56: ; preds = %48
%57 = load i32, i32* %2
%58 = icmp eq i32 %57, 100
br label %59
59: ; preds = %56, %48
%60 = phi i1 [ false, %48 ], [ %58, %56 ]
%61 = zext i1 %60 to i32
%62 = call i32 @assert(i32 %61)
%63 = load i8*, i8** %3
%64 = getelementptr inbounds i8, i8* %63, i64 6
%65 = call i64 @mbtowc(i32* %2, i8* %64, i32 1)
%66 = icmp eq i64 %65, 0
%67 = zext i1 %66 to i32
%68 = call i32 @assert(i32 %67)
store i64 0, i64* @errno
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %3
%69 = load i8*, i8** %3
%70 = call i64 @mbtowc(i32* %2, i8* %69, i32 6)
%71 = icmp eq i64 %70, 1
br i1 %71, label %72, label %75
72: ; preds = %59
%73 = load i32, i32* %2
%74 = icmp eq i32 %73, 102
br label %75
75: ; preds = %72, %59
%76 = phi i1 [ false, %59 ], [ %74, %72 ]
%77 = zext i1 %76 to i32
%78 = call i32 @assert(i32 %77)
%79 = load i8*, i8** %3
%80 = getelementptr inbounds i8, i8* %79, i64 1
%81 = call i64 @mbtowc(i32* %2, i8* %80, i32 5)
%82 = icmp eq i64 %81, 1
br i1 %82, label %83, label %86
83: ; preds = %75
%84 = load i32, i32* %2
%85 = icmp eq i32 %84, 110
br label %86
86: ; preds = %83, %75
%87 = phi i1 [ false, %75 ], [ %85, %83 ]
%88 = zext i1 %87 to i32
%89 = call i32 @assert(i32 %88)
%90 = load i8*, i8** %3
%91 = getelementptr inbounds i8, i8* %90, i64 2
%92 = call i64 @mbtowc(i32* %2, i8* %91, i32 4)
%93 = icmp eq i64 %92, -1
br i1 %93, label %94, label %98
94: ; preds = %86
%95 = load i64, i64* @errno
%96 = load i64, i64* @EILSEQ
%97 = icmp eq i64 %95, %96
br label %98
98: ; preds = %94, %86
%99 = phi i1 [ false, %86 ], [ %97, %94 ]
%100 = zext i1 %99 to i32
%101 = call i32 @assert(i32 %100)
%102 = load i32, i32* %1
ret i32 %102
}

LLVM IR (-O2):
@.str = [7 x i8] c"fn\C3\B6rd\00"
@LC_CTYPE = global i32 0
@.str.1 = [11 x i8] c"de_DE.UTF8\00"
@errno = global i64 0
@EILSEQ = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @LC_CTYPE
%4 = tail call i32 @setlocale(i32 %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
%5 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%6 = icmp eq i64 %5, 1
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, 102
%9 = select i1 %6, i1 %8, i1 false
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 1), i32 5)
%13 = icmp eq i64 %12, 1
%14 = load i32, i32* %1
%15 = icmp eq i32 %14, 110
%16 = select i1 %13, i1 %15, i1 false
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 2), i32 4)
%20 = icmp eq i64 %19, 2
%21 = load i32, i32* %1
%22 = icmp eq i32 %21, 246
%23 = select i1 %20, i1 %22, i1 false
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 4), i32 2)
%27 = icmp eq i64 %26, 1
%28 = load i32, i32* %1
%29 = icmp eq i32 %28, 114
%30 = select i1 %27, i1 %29, i1 false
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 5), i32 1)
%34 = icmp eq i64 %33, 1
%35 = load i32, i32* %1
%36 = icmp eq i32 %35, 100
%37 = select i1 %34, i1 %36, i1 false
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 6), i32 1)
%41 = icmp eq i64 %40, 0
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
store i64 0, i64* @errno
%44 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%45 = icmp eq i64 %44, 1
%46 = load i32, i32* %1
%47 = icmp eq i32 %46, 102
%48 = select i1 %45, i1 %47, i1 false
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 1), i32 5)
%52 = icmp eq i64 %51, 1
%53 = load i32, i32* %1
%54 = icmp eq i32 %53, 110
%55 = select i1 %52, i1 %54, i1 false
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
%58 = call i64 @mbtowc(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 2), i32 4)
%59 = icmp eq i64 %58, -1
%60 = load i64, i64* @errno
%61 = load i64, i64* @EILSEQ
%62 = icmp eq i64 %60, %61
%63 = select i1 %59, i1 %62, i1 false
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
ret i32 0
}