#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;
typedef int /*<<< orphan*/ mbstate_t;
typedef int /*<<< orphan*/ mbs;

/* Variables and functions */
int /*<<< orphan*/ MB_LEN_MAX;
int /*<<< orphan*/ iswprint(int /*<<< orphan*/);
size_t mbrtowc(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/,
               int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ putchar(unsigned char);
scalar_t__ wcwidth(int /*<<< orphan*/);
int prn_printable(const char *s) {
  mbstate_t mbs;
  wchar_t wc;
  int i, n;
  size_t clen;
  memset(&mbs, 0, sizeof(mbs));
  n = 0;
  while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
    if (clen == (size_t)-1) {
      putchar('?');
      s++;
      n++;
      memset(&mbs, 0, sizeof(mbs));
      continue;
    }
    if (clen == (size_t)-2) {
      putchar('?');
      n++;
      break;
    }
    if (!iswprint(wc)) {
      putchar('?');
      s += clen;
      n++;
      continue;
    }
    for (i = 0; i < (int)clen; i++)
      putchar((unsigned char)s[i]);
    s += clen;
    n += wcwidth(wc);
  }
  return (n);
}


x86 assembly (clang, -O0):
<prn_printable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rdi
lea    -0xc(%rbp),%rcx
call   1040 <mbrtowc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     128d <prn_printable+0x13d>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11da <prn_printable+0x8a>
mov    $0x3f,%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
lea    -0xc(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
jmp    1173 <prn_printable+0x23>
cmpq   $0xfffffffffffffffe,-0x20(%rbp)
jne    11fd <prn_printable+0xad>
mov    $0x3f,%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    128d <prn_printable+0x13d>
mov    -0x10(%rbp),%edi
call   1070 <iswprint@plt>
cmp    $0x0,%eax
jne    1232 <prn_printable+0xe2>
mov    $0x3f,%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1173 <prn_printable+0x23>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
jge    1267 <prn_printable+0x117>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1239 <prn_printable+0xe9>
mov    -0x20(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <wcwidth@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    1173 <prn_printable+0x23>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prn_printable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x4(%rsp),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e64(%rip),%r15
jmp    1193 <prn_printable+0x43>
xchg   %ax,%ax
add    %r13,%rbx
mov    (%rsp),%edi
call   1060 <wcwidth@plt>
add    %eax,%r14d
lea    0x4(%rsp),%rbp
mov    %rsp,%r12
jmp    11c1 <prn_printable+0x71>
nopl   0x0(%rax,%rax,1)
mov    $0x3f,%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
add    $0x1,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    (%r15),%edx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rbp,%rcx
call   1040 <mbrtowc@plt>
cmp    $0xffffffffffffffff,%rax
je     11a0 <prn_printable+0x50>
mov    %rax,%r13
cmp    $0xfffffffffffffffe,%rax
je     1238 <prn_printable+0xe8>
test   %r13,%r13
je     1246 <prn_printable+0xf6>
mov    (%rsp),%edi
call   1070 <iswprint@plt>
test   %eax,%eax
jne    1210 <prn_printable+0xc0>
mov    $0x3f,%edi
call   1030 <putchar@plt>
add    %r13,%rbx
add    $0x1,%r14d
jmp    11c1 <prn_printable+0x71>
cs nopw 0x0(%rax,%rax,1)
nop
test   %r13d,%r13d
jle    1180 <prn_printable+0x30>
mov    %r13d,%ebp
xor    %r12d,%r12d
nop
movzbl (%rbx,%r12,1),%edi
call   1030 <putchar@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    1220 <prn_printable+0xd0>
jmp    1180 <prn_printable+0x30>
mov    $0x3f,%edi
call   1030 <putchar@plt>
add    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MB_LEN_MAX = global i32 0
define i32 @prn_printable(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %2
%8 = call i32 @memset(i32* %3, i32 0, i32 4)
store i32 0, i32* %6
br label %9
9: ; preds = %58, %35, %17, %1
%10 = load i8*, i8** %2
%11 = load i32, i32* @MB_LEN_MAX
%12 = call i64 @mbrtowc(i32* %4, i8* %10, i32 %11, i32* %3)
store i64 %12, i64* %7
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %68
14: ; preds = %9
%15 = load i64, i64* %7
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = call i32 @putchar(i8 zeroext 63)
%19 = load i8*, i8** %2
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %2
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
%23 = call i32 @memset(i32* %3, i32 0, i32 4)
br label %9
24: ; preds = %14
%25 = load i64, i64* %7
%26 = icmp eq i64 %25, -2
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i32 @putchar(i8 zeroext 63)
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %68
31: ; preds = %24
%32 = load i32, i32* %4
%33 = call i32 @iswprint(i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %42, label %35
35: ; preds = %31
%36 = call i32 @putchar(i8 zeroext 63)
%37 = load i64, i64* %7
%38 = load i8*, i8** %2
%39 = getelementptr inbounds i8, i8* %38, i64 %37
store i8* %39, i8** %2
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %9
42: ; preds = %31
store i32 0, i32* %5
br label %43
43: ; preds = %55, %42
%44 = load i32, i32* %5
%45 = load i64, i64* %7
%46 = trunc i64 %45 to i32
%47 = icmp slt i32 %44, %46
br i1 %47, label %48, label %58
48: ; preds = %43
%49 = load i8*, i8** %2
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = call i32 @putchar(i8 zeroext %53)
br label %55
55: ; preds = %48
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 1
store i32 %57, i32* %5
br label %43
58: ; preds = %43
%59 = load i64, i64* %7
%60 = load i8*, i8** %2
%61 = getelementptr inbounds i8, i8* %60, i64 %59
store i8* %61, i8** %2
%62 = load i32, i32* %4
%63 = call i64 @wcwidth(i32 %62)
%64 = load i32, i32* %6
%65 = sext i32 %64 to i64
%66 = add nsw i64 %65, %63
%67 = trunc i64 %66 to i32
store i32 %67, i32* %6
br label %9
68: ; preds = %27, %9
%69 = load i32, i32* %6
ret i32 %69
}

LLVM IR (-O2):
@MB_LEN_MAX = global i32 0
define i32 @prn_printable(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @memset(i32* %2, i32 0, i32 4)
br label %7
7: ; preds = %17, %1
%8 = phi i32 [ 0, %1 ], [ %18, %17 ]
%9 = phi i8* [ %0, %1 ], [ %19, %17 ]
%10 = load i32, i32* @MB_LEN_MAX
%11 = call i64 @mbrtowc(i32* %3, i8* %9, i32 %10, i32* %2)
switch i64 %11, label %23 [
i64 0, label %49
i64 -1, label %12
i64 -2, label %20
]
12: ; preds = %7
%13 = call i32 @putchar(i8 zeroext 63)
%14 = getelementptr inbounds i8, i8* %9, i64 1
%15 = add nsw i32 %8, 1
%16 = call i32 @memset(i32* %2, i32 0, i32 4)
br label %17
17: ; preds = %12, %32, %43
%18 = phi i32 [ %15, %12 ], [ %48, %43 ], [ %35, %32 ]
%19 = phi i8* [ %14, %12 ], [ %44, %43 ], [ %34, %32 ]
br label %7
20: ; preds = %7
%21 = call i32 @putchar(i8 zeroext 63)
%22 = add nsw i32 %8, 1
br label %49
23: ; preds = %7
%24 = load i32, i32* %3
%25 = call i32 @iswprint(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = trunc i64 %11 to i32
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %27
%31 = and i64 %11, 4294967295
br label %36
32: ; preds = %23
%33 = call i32 @putchar(i8 zeroext 63)
%34 = getelementptr inbounds i8, i8* %9, i64 %11
%35 = add nsw i32 %8, 1
br label %17
36: ; preds = %30, %36
%37 = phi i64 [ 0, %30 ], [ %41, %36 ]
%38 = getelementptr inbounds i8, i8* %9, i64 %37
%39 = load i8, i8* %38
%40 = call i32 @putchar(i8 zeroext %39)
%41 = add nuw nsw i64 %37, 1
%42 = icmp eq i64 %41, %31
br i1 %42, label %43, label %36
43: ; preds = %36, %27
%44 = getelementptr inbounds i8, i8* %9, i64 %11
%45 = load i32, i32* %3
%46 = call i64 @wcwidth(i32 %45)
%47 = trunc i64 %46 to i32
%48 = add i32 %8, %47
br label %17
49: ; preds = %7, %20
%50 = phi i32 [ %22, %20 ], [ %8, %7 ]
ret i32 %50
}