#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 wchar_t;

/* Variables and functions */
int MB_CUR_MAX;
int mblen(char *, int);
int mbstowcs(char *, char *, int);
int mbtowc(char *, char *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int wcstombs(char *, char *, int);
int wctomb(char *, char);
int main(int argc, char *argv[]) {
  wchar_t w[10];
  char c[10];
  int i;
  int lose = 0;
  i = mbstowcs(w, "bar", 4);
  if (!(i == 3 && w[1] == 'a')) {
    puts("mbstowcs FAILED!");
    lose = 1;
  }
  mbstowcs(w, "blah", 5);
  i = wcstombs(c, w, 10);
  if (i != 4) {
    puts("wcstombs FAILED!");
    lose = 1;
  }
  if (mblen("foobar", 7) != 1) {
    puts("mblen 1 FAILED!");
    lose = 1;
  }
  if (mblen("", 1) != 0) {
    puts("mblen 2 FAILED!");
    lose = 1;
  }
  {
    int r;
    char c = 'x';
    wchar_t wc;
    char mbc[MB_CUR_MAX];
    if ((r = mbtowc(&wc, &c, MB_CUR_MAX)) <= 0) {
      printf("conversion to wide failed, result: %d\n", r);
      lose = 1;
    } else {
      printf("wide value: 0x%04lx\n", (unsigned long)wc);
      mbc[0] = '\0';
      if ((r = wctomb(mbc, wc)) <= 0) {
        printf("conversion to multibyte failed, result: %d\n", r);
        lose = 1;
      }
    }
  }
  puts(lose ? "Test FAILED!" : "Test succeeded.");
  return lose;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x1a(%rbp),%rdi
lea    0xe92(%rip),%rsi
mov    $0x4,%edx
call   1050 <mbstowcs@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x3,-0x28(%rbp)
jne    11bc <main+0x4c>
movsbl -0x19(%rbp),%eax
cmp    $0x61,%eax
je     11cf <main+0x5f>
lea    0xe3d(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x2c(%rbp)
lea    -0x1a(%rbp),%rdi
lea    0xe37(%rip),%rsi
mov    $0x5,%edx
call   1050 <mbstowcs@plt>
lea    -0x24(%rbp),%rdi
lea    -0x1a(%rbp),%rsi
mov    $0xa,%edx
call   1090 <wcstombs@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
je     1216 <main+0xa6>
lea    0xe0c(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x2c(%rbp)
lea    0xe0a(%rip),%rdi
mov    $0x7,%esi
call   1030 <mblen@plt>
cmp    $0x1,%eax
je     1243 <main+0xd3>
lea    0xdf7(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x2c(%rbp)
lea    0xe2a(%rip),%rdi
mov    $0x1,%esi
call   1030 <mblen@plt>
cmp    $0x0,%eax
je     1270 <main+0x100>
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x2c(%rbp)
movb   $0x78,-0x31(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
lea    -0x32(%rbp),%rdi
lea    -0x31(%rbp),%rsi
call   1070 <mbtowc@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jg     12dd <main+0x16d>
mov    -0x30(%rbp),%esi
lea    0xd82(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x2c(%rbp)
jmp    1327 <main+0x1b7>
movsbq -0x32(%rbp),%rax
mov    %eax,%esi
lea    0xd8a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x50(%rbp),%rdi
movb   $0x0,(%rdi)
movsbl -0x32(%rbp),%esi
call   1080 <wctomb@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jg     1322 <main+0x1b2>
mov    -0x30(%rbp),%esi
lea    0xd74(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x2c(%rbp)
jmp    1327 <main+0x1b7>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x2c(%rbp),%ecx
lea    0xd8b(%rip),%rdi
lea    0xd77(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdi
call   1040 <puts@plt>
mov    -0x2c(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xea6(%rip),%rsi
lea    -0x24(%rbp),%rdi
mov    $0x4,%edx
call   1050 <mbstowcs@plt>
cmp    $0x3,%eax
jne    11a0 <main+0x30>
xor    %r14d,%r14d
cmpb   $0x61,-0x23(%rbp)
je     11b2 <main+0x42>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
lea    0xe58(%rip),%rsi
lea    -0x24(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x5,%edx
call   1050 <mbstowcs@plt>
lea    -0x2e(%rbp),%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1090 <wcstombs@plt>
cmp    $0x4,%eax
je     11f2 <main+0x82>
lea    0xe2f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
lea    0xe2e(%rip),%rdi
mov    $0x7,%esi
call   1030 <mblen@plt>
cmp    $0x1,%eax
je     121a <main+0xaa>
lea    0xe1f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
lea    0xe53(%rip),%rdi
mov    $0x1,%esi
call   1030 <mblen@plt>
test   %eax,%eax
je     1241 <main+0xd1>
lea    0xe08(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %rsp,%r15
movb   $0x78,-0x1a(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rax),%edx
lea    -0x19(%rbp),%rdi
lea    -0x1a(%rbp),%rsi
call   1070 <mbtowc@plt>
test   %eax,%eax
jle    12b2 <main+0x142>
movsbl -0x19(%rbp),%esi
lea    0xdf5(%rip),%rdi
call   1060 <printf@plt>
movb   $0x0,(%rbx)
movsbl -0x19(%rbp),%esi
mov    %rbx,%rdi
call   1080 <wctomb@plt>
test   %eax,%eax
jle    12bb <main+0x14b>
mov    %r15,%rsp
test   %r14d,%r14d
lea    0xe1e(%rip),%rax
lea    0xe0a(%rip),%rdi
cmove  %rax,%rdi
jmp    12d9 <main+0x169>
lea    0xd95(%rip),%rdi
jmp    12c2 <main+0x152>
lea    0xdc8(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %r15,%rsp
lea    0xde3(%rip),%rdi
mov    $0x1,%r14d
call   1040 <puts@plt>
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"bar\00"
@.str.1 = [17 x i8] c"mbstowcs FAILED!\00"
@.str.2 = [5 x i8] c"blah\00"
@.str.3 = [17 x i8] c"wcstombs FAILED!\00"
@.str.4 = [7 x i8] c"foobar\00"
@.str.5 = [16 x i8] c"mblen 1 FAILED!\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [16 x i8] c"mblen 2 FAILED!\00"
@MB_CUR_MAX = global i32 0
@.str.8 = [39 x i8] c"conversion to wide failed, result: %d\0A\00"
@.str.9 = [21 x i8] c"wide value: 0x%04lx\0A\00"
@.str.10 = [44 x i8] c"conversion to multibyte failed, result: %d\0A\00"
@.str.11 = [13 x i8] c"Test FAILED!\00"
@.str.12 = [16 x i8] c"Test succeeded.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10 x i8]
%7 = alloca [10 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i8
%13 = alloca i8*
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%16 = call i32 @mbstowcs(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 4)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, 3
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 97
br i1 %23, label %26, label %24
24: ; preds = %19, %2
%25 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %9
br label %26
26: ; preds = %24, %19
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%28 = call i32 @mbstowcs(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 5)
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%31 = call i32 @wcstombs(i8* %29, i8* %30, i32 10)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 4
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %9
br label %36
36: ; preds = %34, %26
%37 = call i32 @mblen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 7)
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %9
br label %41
41: ; preds = %39, %36
%42 = call i32 @mblen(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i32 1)
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %9
br label %46
46: ; preds = %44, %41
store i8 120, i8* %11
%47 = load i32, i32* @MB_CUR_MAX
%48 = zext i32 %47 to i64
%49 = call i8* @llvm.stacksave()
store i8* %49, i8** %13
%50 = alloca i8, i64 %48
store i64 %48, i64* %14
%51 = load i32, i32* @MB_CUR_MAX
%52 = call i32 @mbtowc(i8* %12, i8* %11, i32 %51)
store i32 %52, i32* %10
%53 = icmp sle i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = load i32, i32* %10
%56 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %55)
store i32 1, i32* %9
br label %70
57: ; preds = %46
%58 = load i8, i8* %12
%59 = sext i8 %58 to i64
%60 = trunc i64 %59 to i32
%61 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds i8, i8* %50, i64 0
store i8 0, i8* %62
%63 = load i8, i8* %12
%64 = call i32 @wctomb(i8* %50, i8 signext %63)
store i32 %64, i32* %10
%65 = icmp sle i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %57
%67 = load i32, i32* %10
%68 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.10, i64 0, i64 0), i32 %67)
store i32 1, i32* %9
br label %69
69: ; preds = %66, %57
br label %70
70: ; preds = %69, %54
%71 = load i8*, i8** %13
%72 = load i32, i32* %9
%73 = icmp ne i32 %72, 0
%74 = zext i1 %73 to i64
%75 = select i1 %73, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.12, i64 0, i64 0)
%76 = call i32 @puts(i8* %75)
%77 = load i32, i32* %9
ret i32 %77
}

LLVM IR (-O2):
@.str = [4 x i8] c"bar\00"
@.str.1 = [17 x i8] c"mbstowcs FAILED!\00"
@.str.2 = [5 x i8] c"blah\00"
@.str.3 = [17 x i8] c"wcstombs FAILED!\00"
@.str.4 = [7 x i8] c"foobar\00"
@.str.5 = [16 x i8] c"mblen 1 FAILED!\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [16 x i8] c"mblen 2 FAILED!\00"
@MB_CUR_MAX = global i32 0
@.str.8 = [39 x i8] c"conversion to wide failed, result: %d\0A\00"
@.str.9 = [21 x i8] c"wide value: 0x%04lx\0A\00"
@.str.10 = [44 x i8] c"conversion to multibyte failed, result: %d\0A\00"
@.str.11 = [13 x i8] c"Test FAILED!\00"
@.str.12 = [16 x i8] c"Test succeeded.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i8]
%4 = alloca [10 x i8]
%5 = alloca i8
%6 = alloca i8
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%9 = call i32 @mbstowcs(i8* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 4)
%10 = icmp eq i32 %9, 3
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 97
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %2, %15
%18 = phi i32 [ 0, %2 ], [ 1, %15 ]
%19 = call i32 @mbstowcs(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 5)
%20 = call i32 @wcstombs(i8* %8, i8* %7, i32 10)
%21 = icmp eq i32 %20, 4
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = phi i32 [ 1, %22 ], [ %18, %17 ]
%26 = call i32 @mblen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 7)
%27 = icmp eq i32 %26, 1
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = phi i32 [ 1, %28 ], [ %25, %24 ]
%32 = call i32 @mblen(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i32 1)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = phi i32 [ 1, %34 ], [ %31, %30 ]
store i8 120, i8* %5
%38 = load i32, i32* @MB_CUR_MAX
%39 = zext i32 %38 to i64
%40 = call i8* @llvm.stacksave()
%41 = alloca i8, i64 %39
%42 = load i32, i32* @MB_CUR_MAX
%43 = call i32 @mbtowc(i8* %6, i8* %5, i32 %42)
%44 = icmp slt i32 %43, 1
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %43)
br label %56
47: ; preds = %36
%48 = load i8, i8* %6
%49 = sext i8 %48 to i32
%50 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %49)
store i8 0, i8* %41
%51 = load i8, i8* %6
%52 = call i32 @wctomb(i8* %41, i8 signext %51)
%53 = icmp slt i32 %52, 1
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.10, i64 0, i64 0), i32 %52)
br label %56
56: ; preds = %45, %54
br label %62
57: ; preds = %47
%58 = icmp eq i32 %37, 0
%59 = xor i1 %58, true
%60 = zext i1 %59 to i32
%61 = select i1 %58, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0)
br label %62
62: ; preds = %57, %56
%63 = phi i32 [ 1, %56 ], [ %60, %57 ]
%64 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0), %56 ], [ %61, %57 ]
%65 = call i32 @puts(i8* dereferenceable(1) %64)
ret i32 %63
}