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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ WEOF;
int mbrtowc(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
wint_t btowc(int c) {
  mbstate_t mbs;
  char cc;
  wchar_t wc;
  if (c == EOF)
    return (WEOF);
  memset(&mbs, 0, sizeof(mbs));
  cc = (char)c;
  if (mbrtowc(&wc, &cc, 1, &mbs) > 1)
    return (WEOF);
  return (wc);
}


x86 assembly (clang, -O0):
<btowc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2eb3(%rip),%rcx
cmp    (%rcx),%eax
jne    114e <btowc+0x2e>
mov    0x2e94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119a <btowc+0x7a>
lea    -0xc(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %al,-0xd(%rbp)
lea    -0x14(%rbp),%rdi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
lea    -0xc(%rbp),%rcx
call   1030 <mbrtowc@plt>
cmp    $0x1,%eax
jle    1194 <btowc+0x74>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119a <btowc+0x7a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<btowc>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2eba(%rip),%rax
cmp    %edi,(%rax)
je     116b <btowc+0x4b>
mov    %edi,%ebx
lea    0x14(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %bl,0xf(%rsp)
lea    0x10(%rsp),%rbx
lea    0xf(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <mbrtowc@plt>
cmp    $0x2,%eax
jl     1172 <btowc+0x52>
mov    0x2e66(%rip),%rbx
mov    (%rbx),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EOF = global i32 0
@WEOF = global i32 0
define i32 @btowc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* @WEOF
store i32 %11, i32* %2
br label %22
12: ; preds = %1
%13 = call i32 @memset(i32* %4, i32 0, i32 4)
%14 = load i32, i32* %3
%15 = trunc i32 %14 to i8
store i8 %15, i8* %5
%16 = call i32 @mbrtowc(i32* %6, i8* %5, i32 1, i32* %4)
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = load i32, i32* @WEOF
store i32 %19, i32* %2
br label %22
20: ; preds = %12
%21 = load i32, i32* %6
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %18, %10
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@EOF = global i32 0
@WEOF = global i32 0
define i32 @btowc(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %7, %0
br i1 %8, label %15, label %9
9: ; preds = %1
%10 = call i32 @memset(i32* %2, i32 0, i32 4)
%11 = trunc i32 %0 to i8
store i8 %11, i8* %3
%12 = call i32 @mbrtowc(i32* %4, i8* %3, i32 1, i32* %2)
%13 = icmp sgt i32 %12, 1
%14 = select i1 %13, i32* @WEOF, i32* %4
br label %15
15: ; preds = %9, %1
%16 = phi i32* [ @WEOF, %1 ], [ %14, %9 ]
%17 = load i32, i32* %16
ret i32 %17
}