#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*/ FILE;

/* Variables and functions */
int EILSEQ;
char EOF;
int /*<<< orphan*/ WEOF;
int errno;
char getc(int /*<<< orphan*/ *);
int mbtowc(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ ungetc(char, int /*<<< orphan*/ *);
wint_t my_getwc(FILE *s) {
  char i[16];
  int byte = 0;
  int convert;
  int x;
  wchar_t rval;
  while (1) {
    i[byte] = getc(s);
    if (i[byte] == EOF) {
      return WEOF;
    }
    byte++;
    errno = 0;
    mbtowc(NULL, NULL, 0);
    convert = mbtowc(&rval, i, byte);
    x = errno;
    if (convert > 0) {
      return rval;
    }
    if (byte == 16) {
      while (byte > 1) {
        ungetc(i[--byte], s);
      }
      errno = -EILSEQ;
      return WEOF;
    }
  }
}


x86 assembly (clang, -O0):
<my_getwc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <getc@plt>
mov    %al,%cl
movslq -0x24(%rbp),%rax
mov    %cl,-0x20(%rbp,%rax,1)
movslq -0x24(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%eax
mov    0x2e7a(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1182 <my_getwc+0x52>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    123d <my_getwc+0x10d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    0x2e3e(%rip),%rax
movl   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
mov    %rsi,%rdi
call   1030 <mbtowc@plt>
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    -0x30(%rbp),%rdi
call   1030 <mbtowc@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    11da <my_getwc+0xaa>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123d <my_getwc+0x10d>
cmpl   $0x10,-0x24(%rbp)
jne    1238 <my_getwc+0x108>
jmp    11e9 <my_getwc+0xb9>
cmpl   $0x1,-0x24(%rbp)
jle    1213 <my_getwc+0xe3>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cltq
mov    -0x20(%rbp,%rax,1),%al
mov    -0x10(%rbp),%rsi
movsbl %al,%edi
call   1040 <ungetc@plt>
jmp    11e9 <my_getwc+0xb9>
xor    %ecx,%ecx
mov    0x2dd4(%rip),%rax
sub    (%rax),%ecx
mov    0x2dab(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    123d <my_getwc+0x10d>
jmp    1143 <my_getwc+0x13>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getwc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
mov    0x2e96(%rip),%r13
mov    0x2e7f(%rip),%rbp
lea    0x14(%rsp),%r15
mov    %rsp,%r12
nopl   0x0(%rax)
mov    %r14,%rdi
call   1050 <getc@plt>
mov    %al,(%rsp,%rbx,1)
cmp    0x0(%r13),%al
je     1277 <my_getwc+0x147>
add    $0x1,%rbx
movl   $0x0,0x0(%rbp)
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <mbtowc@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <mbtowc@plt>
test   %eax,%eax
jg     127e <my_getwc+0x14e>
cmp    $0x10,%rbx
jne    1160 <my_getwc+0x30>
movsbl 0xf(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0xe(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0xd(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0xc(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0xb(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0xa(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x9(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x8(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x7(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x6(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x5(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x4(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x3(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x2(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
movsbl 0x1(%rsp),%edi
mov    %r14,%rsi
call   1040 <ungetc@plt>
mov    0x2d80(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    %ecx,0x0(%rbp)
mov    0x2d4a(%rip),%r15
mov    (%r15),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i8 0
@WEOF = global i32 0
@errno = global i32 0
@EILSEQ = global i32 0
define i32 @my_getwc(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [16 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 0, i32* %5
br label %9
9: ; preds = %1, %56
%10 = load i32*, i32** %3
%11 = call signext i8 @getc(i32* %10)
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %13
store i8 %11, i8* %14
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = load i8, i8* @EOF
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %9
%24 = load i32, i32* @WEOF
store i32 %24, i32* %2
br label %57
25: ; preds = %9
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
store i32 0, i32* @errno
%28 = call i32 @mbtowc(i32* null, i8* null, i32 0)
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%30 = load i32, i32* %5
%31 = call i32 @mbtowc(i32* %8, i8* %29, i32 %30)
store i32 %31, i32* %6
%32 = load i32, i32* @errno
store i32 %32, i32* %7
%33 = load i32, i32* %6
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = load i32, i32* %8
store i32 %36, i32* %2
br label %57
37: ; preds = %25
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, 16
br i1 %39, label %40, label %56
40: ; preds = %37
br label %41
41: ; preds = %44, %40
%42 = load i32, i32* %5
%43 = icmp sgt i32 %42, 1
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = load i32, i32* %5
%46 = add nsw i32 %45, -1
store i32 %46, i32* %5
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = load i32*, i32** %3
%51 = call i32 @ungetc(i8 signext %49, i32* %50)
br label %41
52: ; preds = %41
%53 = load i32, i32* @EILSEQ
%54 = sub nsw i32 0, %53
store i32 %54, i32* @errno
%55 = load i32, i32* @WEOF
store i32 %55, i32* %2
br label %57
56: ; preds = %37
br label %9
57: ; preds = %52, %35, %23
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@EOF = global i8 0
@WEOF = global i32 0
@errno = global i32 0
@EILSEQ = global i32 0
define i32 @my_getwc(i32* nocapture %0) {
%2 = alloca [16 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
br label %6
6: ; preds = %18, %1
%7 = phi i64 [ %13, %18 ], [ 0, %1 ]
%8 = call signext i8 @getc(i32* %0)
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 %7
store i8 %8, i8* %9
%10 = load i8, i8* @EOF
%11 = icmp eq i8 %8, %10
br i1 %11, label %68, label %12
12: ; preds = %6
%13 = add nuw nsw i64 %7, 1
store i32 0, i32* @errno
%14 = call i32 @mbtowc(i32* null, i8* null, i32 0)
%15 = trunc i64 %13 to i32
%16 = call i32 @mbtowc(i32* %3, i8* %4, i32 %15)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %68, label %18
18: ; preds = %12
%19 = icmp eq i64 %13, 16
br i1 %19, label %20, label %6
20: ; preds = %18
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 15
%22 = load i8, i8* %21
%23 = call i32 @ungetc(i8 signext %22, i32* %0)
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 14
%25 = load i8, i8* %24
%26 = call i32 @ungetc(i8 signext %25, i32* %0)
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 13
%28 = load i8, i8* %27
%29 = call i32 @ungetc(i8 signext %28, i32* %0)
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 12
%31 = load i8, i8* %30
%32 = call i32 @ungetc(i8 signext %31, i32* %0)
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 11
%34 = load i8, i8* %33
%35 = call i32 @ungetc(i8 signext %34, i32* %0)
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 10
%37 = load i8, i8* %36
%38 = call i32 @ungetc(i8 signext %37, i32* %0)
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 9
%40 = load i8, i8* %39
%41 = call i32 @ungetc(i8 signext %40, i32* %0)
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 8
%43 = load i8, i8* %42
%44 = call i32 @ungetc(i8 signext %43, i32* %0)
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 7
%46 = load i8, i8* %45
%47 = call i32 @ungetc(i8 signext %46, i32* %0)
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 6
%49 = load i8, i8* %48
%50 = call i32 @ungetc(i8 signext %49, i32* %0)
%51 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 5
%52 = load i8, i8* %51
%53 = call i32 @ungetc(i8 signext %52, i32* %0)
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 4
%55 = load i8, i8* %54
%56 = call i32 @ungetc(i8 signext %55, i32* %0)
%57 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 3
%58 = load i8, i8* %57
%59 = call i32 @ungetc(i8 signext %58, i32* %0)
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 2
%61 = load i8, i8* %60
%62 = call i32 @ungetc(i8 signext %61, i32* %0)
%63 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 1
%64 = load i8, i8* %63
%65 = call i32 @ungetc(i8 signext %64, i32* %0)
%66 = load i32, i32* @EILSEQ
%67 = sub nsw i32 0, %66
store i32 %67, i32* @errno
br label %68
68: ; preds = %12, %6, %20
%69 = phi i32* [ @WEOF, %20 ], [ %3, %12 ], [ @WEOF, %6 ]
%70 = load i32, i32* %69
ret i32 %70
}