#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*/ assert(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ refc_alloc(int);
int strlen(char *);
char *refc_str(char *buff) {
  int len = strlen(buff);
  char *alloc = (char *)refc_alloc(len + 1);
  assert(alloc);
  if (alloc) {
    memcpy(alloc, buff, len);
    alloc[len] = '\0';
    return alloc;
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<refc_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <refc_alloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
je     11a4 <refc_str+0x64>
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)
jmp    11ac <refc_str+0x6c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<refc_str>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1060 <refc_alloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <assert@plt>
test   %rbx,%rbx
je     1180 <refc_str+0x40>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1182 <refc_str+0x42>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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