#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 */
int /*<<< orphan*/ abort();
scalar_t__ calloc(int, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ perror(char *);
char *mem_str(const char *str, int len) {
  char *str_new;
  if (!str)
    return 0;
  {
    if (!((str_new) = (char *)calloc((len + 1), sizeof(char)))) {
      perror("malloc failure");
      abort();
    }
  };
  memcpy(str_new, str, len);
  *(str_new + len) = 0;
  return str_new;
}


x86 assembly (clang, -O0):
<mem_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1167 <mem_str+0x27>
movq   $0x0,-0x8(%rbp)
jmp    11bc <mem_str+0x7c>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1198 <mem_str+0x58>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
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):
<mem_str>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     117d <mem_str+0x3d>
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(%r14),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     1188 <mem_str+0x48>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    117f <mem_str+0x3f>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"malloc failure\00"
define i8* @mem_str(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 i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i8* null, i8** %3
br label %29
10: ; preds = %2
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
%13 = call i64 @calloc(i32 %12, i32 1)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %10
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = call i32 @memcpy(i8* %20, i8* %21, i32 %22)
%24 = load i8*, i8** %6
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %6
store i8* %28, i8** %3
br label %29
29: ; preds = %19, %9
%30 = load i8*, i8** %3
ret i8* %30
}

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