#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(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *dupe_string(const char *str, int *str_len) {
  if (*str_len == -1)
    *str_len = strlen(str);
  char *out = malloc(*str_len + 1);
  if (out) {
    memcpy(out, str, *str_len);
    out[*str_len] = 0;
  }
  return out;
}


x86 assembly (clang, -O0):
<dupe_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    115e <dupe_string+0x2e>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119d <dupe_string+0x6d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupe_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%r12d
cmp    $0xffffffff,%r12d
jne    1155 <dupe_string+0x25>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
mov    %eax,(%r15)
lea    0x1(%r12),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117c <dupe_string+0x4c>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
movslq (%r15),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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