#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 scalar_t__ wchar_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ WEOF;
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
scalar_t__ fgetwc(int /*<<< orphan*/ *);
int fgetnwc(wchar_t *wbuff, int n, FILE *infile) {
  assert(infile);
  int i = 0;
  wchar_t winp = (wchar_t)fgetwc(infile);
  for (i; winp != WEOF && (n ? i < n : 1); i++) {
    *(wbuff + i) = winp;
    winp = (wchar_t)fgetwc(infile);
  }
  return i;
}


x86 assembly (clang, -O0):
<fgetnwc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <assert@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fgetwc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    0x2e83(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x29(%rbp)
je     11a2 <fgetnwc+0x82>
cmpl   $0x0,-0xc(%rbp)
je     1189 <fgetnwc+0x69>
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x30(%rbp)
jmp    1196 <fgetnwc+0x76>
mov    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1196 <fgetnwc+0x76>
mov    -0x30(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11b2 <fgetnwc+0x92>
jmp    11dd <fgetnwc+0xbd>
mov    -0x28(%rbp),%rdx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rdi
call   1030 <fgetwc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1150 <fgetnwc+0x30>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetnwc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rdx,%rdi
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <fgetwc@plt>
mov    0x2e95(%rip),%r12
xor    %ebp,%ebp
cmp    (%r12),%rax
je     1180 <fgetnwc+0x60>
movslq %r15d,%r13
xor    %ebp,%ebp
jmp    1176 <fgetnwc+0x56>
nopw   0x0(%rax,%rax,1)
mov    %rax,(%r14,%rbp,8)
mov    %rbx,%rdi
call   1030 <fgetwc@plt>
add    $0x1,%rbp
cmp    (%r12),%rax
je     1180 <fgetnwc+0x60>
test   %r15d,%r15d
je     1160 <fgetnwc+0x40>
cmp    %r13,%rbp
jl     1160 <fgetnwc+0x40>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@WEOF = global i64 0
define i32 @fgetnwc(i64* %0, i32 %1, i32* %2) {
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
store i64* %0, i64** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%9 = load i32*, i32** %6
%10 = call i32 @assert(i32* %9)
store i32 0, i32* %7
%11 = load i32*, i32** %6
%12 = call i64 @fgetwc(i32* %11)
store i64 %12, i64* %8
%13 = load i32, i32* %7
br label %14
14: ; preds = %40, %3
%15 = load i64, i64* %8
%16 = load i64, i64* @WEOF
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
%25 = zext i1 %24 to i32
br label %27
26: ; preds = %18
br label %27
27: ; preds = %26, %21
%28 = phi i32 [ %25, %21 ], [ 1, %26 ]
%29 = icmp ne i32 %28, 0
br label %30
30: ; preds = %27, %14
%31 = phi i1 [ false, %14 ], [ %29, %27 ]
br i1 %31, label %32, label %43
32: ; preds = %30
%33 = load i64, i64* %8
%34 = load i64*, i64** %4
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i64, i64* %34, i64 %36
store i64 %33, i64* %37
%38 = load i32*, i32** %6
%39 = call i64 @fgetwc(i32* %38)
store i64 %39, i64* %8
br label %40
40: ; preds = %32
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %14
43: ; preds = %30
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
@WEOF = global i64 0
define i32 @fgetnwc(i64* nocapture writeonly %0, i32 %1, i32* %2) {
%4 = tail call i32 @assert(i32* %2)
%5 = tail call i64 @fgetwc(i32* %2)
%6 = load i64, i64* @WEOF
%7 = icmp eq i64 %5, %6
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = icmp eq i32 %1, 0
%10 = sext i32 %1 to i64
br label %11
11: ; preds = %8, %16
%12 = phi i64 [ 0, %8 ], [ %19, %16 ]
%13 = phi i64 [ %5, %8 ], [ %18, %16 ]
%14 = icmp slt i64 %12, %10
%15 = select i1 %9, i1 true, i1 %14
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = getelementptr inbounds i64, i64* %0, i64 %12
store i64 %13, i64* %17
%18 = tail call i64 @fgetwc(i32* %2)
%19 = add nuw i64 %12, 1
%20 = load i64, i64* @WEOF
%21 = icmp eq i64 %18, %20
br i1 %21, label %22, label %11
22: ; preds = %16, %11
%23 = phi i64 [ %12, %11 ], [ %19, %16 ]
%24 = trunc i64 %23 to i32
br label %25
25: ; preds = %22, %3
%26 = phi i32 [ 0, %3 ], [ %24, %22 ]
ret i32 %26
}