#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 char const *wint_t;
typedef int wchar_t;
typedef int /*<<< orphan*/ state;
typedef int /*<<< orphan*/ out;
typedef int mbstate_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
size_t mbsrtowcs(int *, char const **, int, int *);
int /*<<< orphan*/ memset(int *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  const unsigned char buf[] = {'a', 'b', '\0', 'c', 'd', '\0', 'e'};
  wchar_t out[sizeof(buf)];
  memset(out, '\xff', sizeof(out));
  mbstate_t state;
  memset(&state, '\0', sizeof(state));
  const char *in = (const char *)buf;
  size_t n = mbsrtowcs(out, &in, sizeof(out) / sizeof(wchar_t), &state);
  int result = 0;
  if (n != 2) {
    printf("n = %zu, expected 2\n", n);
    result = 1;
  }
  if (in != NULL) {
    printf("in = %p, expected NULL\n", in);
    result = 1;
  }
  if (out[0] != L'a') {
    printf("out[0] = L'%lc', expected L'a'\n", (wint_t)out[0]);
    result = 1;
  }
  if (out[1] != L'b') {
    printf("out[1] = L'%lc', expected L'b'\n", (wint_t)out[1]);
    result = 1;
  }
  if (out[2] != L'\0') {
    printf("out[2] = L'%lc', expected L'\\0'\n", (wint_t)out[2]);
    result = 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xebb(%rip),%eax
mov    %eax,-0xb(%rbp)
mov    0xeb5(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xead(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0xffffffff,%esi
mov    $0x1c,%edx
call   1040 <memset@plt>
lea    -0x34(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    -0xb(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x7,%edx
lea    -0x34(%rbp),%rcx
call   1050 <mbsrtowcs@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpq   $0x2,-0x48(%rbp)
je     11cc <main+0x9c>
mov    -0x48(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4c(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11f0 <main+0xc0>
mov    -0x40(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4c(%rbp)
cmpl   $0x61,-0x30(%rbp)
je     1213 <main+0xe3>
movslq -0x30(%rbp),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4c(%rbp)
cmpl   $0x62,-0x2c(%rbp)
je     1236 <main+0x106>
movslq -0x2c(%rbp),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4c(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1259 <main+0x129>
movslq -0x28(%rbp),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
movl   $0x65006463,0xb(%rsp)
65
movl   $0x63006261,0x8(%rsp)
63
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0xffffffff,%esi
mov    $0x1c,%edx
call   1040 <memset@plt>
lea    0x1c(%rsp),%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    $0x7,%edx
mov    %r15,%rcx
call   1050 <mbsrtowcs@plt>
cmp    $0x2,%rax
je     11b1 <main+0x81>
lea    0xe65(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebx
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     11ce <main+0x9e>
lea    0xe5a(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebx
movslq 0x20(%rsp),%rsi
cmp    $0x61,%rsi
je     11ec <main+0xbc>
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebx
movslq 0x24(%rsp),%rsi
cmp    $0x62,%rsi
je     120a <main+0xda>
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebx
movslq 0x28(%rsp),%rsi
test   %rsi,%rsi
je     1227 <main+0xf7>
lea    0xe59(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.buf = [7 x i8] c"ab\00cd\00e"
@.str = [21 x i8] c"n = %zu, expected 2\0A\00"
@.str.1 = [24 x i8] c"in = %p, expected NULL\0A\00"
@.str.2 = [32 x i8] c"out[0] = L'%lc', expected L'a'\0A\00"
@.str.3 = [32 x i8] c"out[1] = L'%lc', expected L'b'\0A\00"
@.str.4 = [33 x i8] c"out[2] = L'%lc', expected L'\\0'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [7 x i8]
%3 = alloca [7 x i32]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [7 x i8]* %2 to i8*
%9 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 0
%10 = call i32 @memset(i32* %9, i8 signext -1, i32 28)
%11 = call i32 @memset(i32* %4, i8 signext 0, i32 4)
%12 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
store i8* %12, i8** %5
%13 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 0
%14 = call i64 @mbsrtowcs(i32* %13, i8** %5, i32 7, i32* %4)
store i64 %14, i64* %6
store i32 0, i32* %7
%15 = load i64, i64* %6
%16 = icmp ne i64 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = load i64, i64* %6
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %18)
store i32 1, i32* %7
br label %20
20: ; preds = %17, %0
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 1, i32* %7
br label %26
26: ; preds = %23, %20
%27 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 0
%28 = load i32, i32* %27
%29 = icmp ne i32 %28, 97
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 0
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = inttoptr i64 %33 to i8*
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 1, i32* %7
br label %36
36: ; preds = %30, %26
%37 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 1
%38 = load i32, i32* %37
%39 = icmp ne i32 %38, 98
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 1
%42 = load i32, i32* %41
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i8*
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %44)
store i32 1, i32* %7
br label %46
46: ; preds = %40, %36
%47 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 2
%48 = load i32, i32* %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 2
%52 = load i32, i32* %51
%53 = sext i32 %52 to i64
%54 = inttoptr i64 %53 to i8*
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %54)
store i32 1, i32* %7
br label %56
56: ; preds = %50, %46
%57 = load i32, i32* %7
ret i32 %57
}

LLVM IR (-O2):
@__const.main.buf = [7 x i8] c"ab\00cd\00e"
@.str = [21 x i8] c"n = %zu, expected 2\0A\00"
@.str.1 = [24 x i8] c"in = %p, expected NULL\0A\00"
@.str.2 = [32 x i8] c"out[0] = L'%lc', expected L'a'\0A\00"
@.str.3 = [32 x i8] c"out[1] = L'%lc', expected L'b'\0A\00"
@.str.4 = [33 x i8] c"out[2] = L'%lc', expected L'\\0'\0A\00"
define i32 @main() {
%1 = alloca [7 x i8]
%2 = alloca [7 x i32]
%3 = alloca i32
%4 = alloca i8*
%5 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%6 = bitcast [7 x i32]* %2 to i8*
%7 = getelementptr inbounds [7 x i32], [7 x i32]* %2, i64 0, i64 0
%8 = call i32 @memset(i32* %7, i8 signext -1, i32 28)
%9 = bitcast i32* %3 to i8*
%10 = call i32 @memset(i32* %3, i8 signext 0, i32 4)
%11 = bitcast i8** %4 to i8*
store i8* %5, i8** %4
%12 = call i64 @mbsrtowcs(i32* %7, i8** %4, i32 7, i32* %3)
%13 = icmp eq i64 %12, 2
br i1 %13, label %16, label %14
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %12)
br label %16
16: ; preds = %14, %0
%17 = phi i32 [ 1, %14 ], [ 0, %0 ]
%18 = load i8*, i8** %4
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %22
22: ; preds = %20, %16
%23 = phi i32 [ 1, %20 ], [ %17, %16 ]
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 97
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = sext i32 %24 to i64
%28 = inttoptr i64 %27 to i8*
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %26, %22
%31 = phi i32 [ 1, %26 ], [ %23, %22 ]
%32 = getelementptr inbounds [7 x i32], [7 x i32]* %2, i64 0, i64 1
%33 = load i32, i32* %32
%34 = icmp eq i32 %33, 98
br i1 %34, label %39, label %35
35: ; preds = %30
%36 = sext i32 %33 to i64
%37 = inttoptr i64 %36 to i8*
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35, %30
%40 = phi i32 [ 1, %35 ], [ %31, %30 ]
%41 = getelementptr inbounds [7 x i32], [7 x i32]* %2, i64 0, i64 2
%42 = load i32, i32* %41
%43 = icmp eq i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %39
%45 = sext i32 %42 to i64
%46 = inttoptr i64 %45 to i8*
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %44, %39
%49 = phi i32 [ 1, %44 ], [ %40, %39 ]
ret i32 %49
}