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

/* Variables and functions */
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ perror(char *);
char *util_ncopy(const char *s, size_t n) {
  char *p;
  if (s == NULL || n == 0)
    return NULL;
  p = malloc(n);
  if (p == NULL) {
    perror("malloc");
    return NULL;
  }
  memcpy(p, s, n);
  p[n - 1] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<util_ncopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <util_ncopy+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <util_ncopy+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11bd <util_ncopy+0x8d>
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1194 <util_ncopy+0x64>
lea    0xe7e(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11bd <util_ncopy+0x8d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_ncopy>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     116b <util_ncopy+0x3b>
mov    %rsi,%r14
test   %rsi,%rsi
je     116b <util_ncopy+0x3b>
mov    %rdi,%r15
mov    %r14,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1174 <util_ncopy+0x44>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <memcpy@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe85(%rip),%rdi
call   1050 <perror@plt>
xor    %ebx,%ebx
jmp    116b <util_ncopy+0x3b>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
define i8* @util_ncopy(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i64, i64* %5
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i8* null, i8** %3
br label %30
13: ; preds = %9
%14 = load i64, i64* %5
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %30
20: ; preds = %13
%21 = load i8*, i8** %6
%22 = load i8*, i8** %4
%23 = load i64, i64* %5
%24 = call i32 @memcpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %6
%26 = load i64, i64* %5
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %6
store i8* %29, i8** %3
br label %30
30: ; preds = %20, %18, %12
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
define i8* @util_ncopy(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i64 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = tail call i8* @malloc(i64 %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %15
11: ; preds = %6
%12 = tail call i32 @memcpy(i8* %7, i8* %0, i64 %1)
%13 = add i64 %1, -1
%14 = getelementptr inbounds i8, i8* %7, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %2, %11, %9
%16 = phi i8* [ null, %9 ], [ %7, %11 ], [ null, %2 ]
ret i8* %16
}