#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);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
int /*<<< orphan*/ syserr(char *, char *, char *);
char *strclone(const char *si) {
  int ni;
  char *so;
  ni = strlen(si);
  so = (char *)malloc(sizeof(char) * (ni + 1));
  if (!so)
    syserr("strclone", "malloc for string ", "failed");
  memcpy(so, si, ni);
  so[ni] = '\0';
  return so;
}


x86 assembly (clang, -O0):
<strclone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <strclone+0x54>
lea    0xe7f(%rip),%rdi
lea    0xe81(%rip),%rsi
lea    0xe8d(%rip),%rdx
call   1040 <syserr@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strclone>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    117b <strclone+0x3b>
lea    0xe98(%rip),%rdi
lea    0xe9a(%rip),%rsi
lea    0xea6(%rip),%rdx
call   1040 <syserr@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"strclone\00"
@.str.1 = [19 x i8] c"malloc for string \00"
@.str.2 = [7 x i8] c"failed\00"
define i8* @strclone(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = mul i64 1, %9
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %1
%17 = call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %1
%19 = load i8*, i8** %4
%20 = load i8*, i8** %2
%21 = load i32, i32* %3
%22 = call i32 @memcpy(i8* %19, i8* %20, i32 %21)
%23 = load i8*, i8** %4
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
@.str = [9 x i8] c"strclone\00"
@.str.1 = [19 x i8] c"malloc for string \00"
@.str.2 = [7 x i8] c"failed\00"
define i8* @strclone(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = tail call i32 @memcpy(i8* %5, i8* %0, i32 %2)
%11 = sext i32 %2 to i64
%12 = getelementptr inbounds i8, i8* %5, i64 %11
store i8 0, i8* %12
ret i8* %5
}