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

/* Variables and functions */
int /*<<< orphan*/ wcrtomb(char *, scalar_t__ const, int /*<<< orphan*/ *);
size_t wcsrtombs(char *__restrict s, const wchar_t **__restrict pwcs, size_t n,
                 mbstate_t *__restrict ps) {
  char *d;
  const wchar_t *p;
  size_t count;
  for (p = *pwcs, d = s, count = 0; count <= n && *p != 0; count++, d++, p++) {
    wcrtomb(d, *p, ps);
  }
  *d = 0;
  return count;
}


x86 assembly (clang, -O0):
<wcsrtombs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
xor    %eax,%eax
cmp    -0x18(%rbp),%rcx
mov    %al,-0x39(%rbp)
ja     1164 <wcsrtombs+0x54>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1174 <wcsrtombs+0x64>
jmp    11b1 <wcsrtombs+0xa1>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <wcrtomb@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
jmp    1143 <wcsrtombs+0x33>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsrtombs>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rdi,%r14
mov    (%rsi),%r13
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%rdi
mov    0x0(%r13,%rbx,8),%rsi
test   %rsi,%rsi
je     1155 <wcsrtombs+0x45>
mov    %r15,%rdx
call   1030 <wcrtomb@plt>
add    $0x1,%rbx
cmp    %r12,%rbx
jbe    1130 <wcsrtombs+0x20>
add    %rbx,%r14
mov    %r14,%rdi
movb   $0x0,(%rdi)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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