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

/* Variables and functions */
int wcwidth(int);
int wcswidth(const wchar_t *pwcs, size_t n) {
  wchar_t wc;
  int len, l;
  len = 0;
  while (n-- > 0 && (wc = *pwcs++) != L'\0') {
    if ((l = wcwidth(wc)) < 0)
      return (-1);
    len += l;
  }
  return (len);
}


x86 assembly (clang, -O0):
<wcswidth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x25(%rbp)
jbe    1162 <wcswidth+0x52>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1172 <wcswidth+0x62>
jmp    11a0 <wcswidth+0x90>
mov    -0x1c(%rbp),%edi
call   1030 <wcwidth@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1192 <wcswidth+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <wcswidth+0x96>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1127 <wcswidth+0x17>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcswidth>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     114e <wcswidth+0x3e>
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%edi
test   %edi,%edi
je     1157 <wcswidth+0x47>
call   1030 <wcwidth@plt>
test   %eax,%eax
js     1152 <wcswidth+0x42>
add    %eax,%ebp
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1130 <wcswidth+0x20>
jmp    1157 <wcswidth+0x47>
xor    %ebp,%ebp
jmp    1157 <wcswidth+0x47>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @wcswidth(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32 0, i32* %7
br label %9
9: ; preds = %25, %2
%10 = load i64, i64* %5
%11 = add i64 %10, -1
store i64 %11, i64* %5
%12 = icmp ugt i64 %10, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i32*, i32** %4
%15 = getelementptr inbounds i32, i32* %14, i32 1
store i32* %15, i32** %4
%16 = load i32, i32* %14
store i32 %16, i32* %6
%17 = icmp ne i32 %16, 0
br label %18
18: ; preds = %13, %9
%19 = phi i1 [ false, %9 ], [ %17, %13 ]
br i1 %19, label %20, label %29
20: ; preds = %18
%21 = load i32, i32* %6
%22 = call i32 @wcwidth(i32 %21)
store i32 %22, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 -1, i32* %3
br label %31
25: ; preds = %20
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = add nsw i32 %27, %26
store i32 %28, i32* %7
br label %9
29: ; preds = %18
%30 = load i32, i32* %7
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %24
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
define i32 @wcswidth(i32* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %18, label %7
4: ; preds = %13
%5 = getelementptr inbounds i32, i32* %10, i64 1
%6 = icmp eq i64 %14, 0
br i1 %6, label %18, label %7
7: ; preds = %2, %4
%8 = phi i64 [ %14, %4 ], [ %1, %2 ]
%9 = phi i32 [ %17, %4 ], [ 0, %2 ]
%10 = phi i32* [ %5, %4 ], [ %0, %2 ]
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = add i64 %8, -1
%15 = tail call i32 @wcwidth(i32 %11)
%16 = icmp slt i32 %15, 0
%17 = add nsw i32 %15, %9
br i1 %16, label %18, label %4
18: ; preds = %13, %4, %7, %2
%19 = phi i32 [ 0, %2 ], [ %9, %7 ], [ %17, %4 ], [ -1, %13 ]
ret i32 %19
}