#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*/ uint32_t;
typedef int /*<<< orphan*/ chtype;
typedef int /*<<< orphan*/ WINDOW;

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int winchnstr(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int __darwin_winchnstr(WINDOW *w, uint32_t *c, int n) {
  chtype *nc = malloc(sizeof(chtype) * n);
  int rv = winchnstr(w, nc, n);
  for (int i = 0; i < n; i++)
    c[i] = nc[i];
  free(nc);
  return rv;
}


x86 assembly (clang, -O0):
<__darwin_winchnstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <winchnstr@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a0 <__darwin_winchnstr+0x70>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1170 <__darwin_winchnstr+0x40>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__darwin_winchnstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x0(,%rbx,4),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <winchnstr@plt>
mov    %eax,%ebp
test   %ebx,%ebx
jle    1182 <__darwin_winchnstr+0x42>
mov    %ebx,%edx
shl    $0x2,%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @__darwin_winchnstr(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = mul i64 4, %11
%13 = trunc i64 %12 to i32
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %4
%16 = load i32*, i32** %7
%17 = load i32, i32* %6
%18 = call i32 @winchnstr(i32* %15, i32* %16, i32 %17)
store i32 %18, i32* %8
store i32 0, i32* %9
br label %19
19: ; preds = %33, %3
%20 = load i32, i32* %9
%21 = load i32, i32* %6
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %19
%24 = load i32*, i32** %7
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = load i32*, i32** %5
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
store i32 %28, i32* %32
br label %33
33: ; preds = %23
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %19
36: ; preds = %19
%37 = load i32*, i32** %7
%38 = call i32 @free(i32* %37)
%39 = load i32, i32* %8
ret i32 %39
}

LLVM IR (-O2):
define i32 @__darwin_winchnstr(i32* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = shl i32 %2, 2
%5 = tail call i32* @malloc(i32 %4)
%6 = tail call i32 @winchnstr(i32* %0, i32* %5, i32 %2)
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %1 to i8*
%11 = zext i32 %2 to i64
%12 = shl nuw nsw i64 %11, 2
br label %13
13: ; preds = %8, %3
%14 = tail call i32 @free(i32* %5)
ret i32 %6
}