#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 */
scalar_t__ malloc(int);
char *memcpy(char *, char *, int);
char *__strasp_xmalloc(char *str, int len) {
  char *ptr;
  if (len <= 0)
    return NULL;
  ptr = (char *)malloc(sizeof(char) * (len + 1));
  if (ptr == NULL)
    return NULL;
  ptr = memcpy(ptr, str, len);
  ptr[len] = '\0';
  return ptr;
}


x86 assembly (clang, -O0):
<__strasp_xmalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1146 <__strasp_xmalloc+0x26>
movq   $0x0,-0x8(%rbp)
jmp    119d <__strasp_xmalloc+0x7d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1175 <__strasp_xmalloc+0x55>
movq   $0x0,-0x8(%rbp)
jmp    119d <__strasp_xmalloc+0x7d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%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):
<__strasp_xmalloc>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    114f <__strasp_xmalloc+0x2f>
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x1(%rbx),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     114f <__strasp_xmalloc+0x2f>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    %ebx,%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    1151 <__strasp_xmalloc+0x31>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @__strasp_xmalloc(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp sle i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
store i8* null, i8** %3
br label %31
10: ; preds = %2
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %10
store i8* null, i8** %3
br label %31
21: ; preds = %10
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = call i8* @memcpy(i8* %22, i8* %23, i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %6
store i8* %30, i8** %3
br label %31
31: ; preds = %21, %20, %9
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
define i8* @__strasp_xmalloc(i8* %0, i32 %1) {
%3 = icmp slt i32 %1, 1
br i1 %3, label %13, label %4
4: ; preds = %2
%5 = add nuw nsw i32 %1, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = inttoptr i64 %6 to i8*
%10 = tail call i8* @memcpy(i8* %9, i8* %0, i32 %1)
%11 = zext i32 %1 to i64
%12 = getelementptr inbounds i8, i8* %10, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %4, %2, %8
%14 = phi i8* [ %10, %8 ], [ null, %2 ], [ null, %4 ]
ret i8* %14
}