#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 *brain_alloc_(long, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
long strlen(char const *);
char *brain_strdup(char const *in_string) {
  if (!in_string)
    in_string = "";
  long length = strlen(in_string);
  char *result = brain_alloc_(length + 1, 0);
  if (!result)
    return NULL;
  strcpy(result, in_string);
  return result;
}


x86 assembly (clang, -O0):
<brain_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1152 <brain_strdup+0x22>
lea    0xeb2(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
xor    %esi,%esi
call   1050 <brain_alloc_@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118a <brain_strdup+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    119f <brain_strdup+0x6f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brain_strdup>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
lea    0xec2(%rip),%r14
cmovne %rdi,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
xor    %esi,%esi
call   1050 <brain_alloc_@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1168 <brain_strdup+0x38>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @brain_strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %3
br label %9
9: ; preds = %8, %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = add nsw i64 %12, 1
%14 = call i8* @brain_alloc_(i64 %13, i32 0)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %9
store i8* null, i8** %2
br label %23
18: ; preds = %9
%19 = load i8*, i8** %5
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %5
store i8* %22, i8** %2
br label %23
23: ; preds = %18, %17
%24 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @brain_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = add nsw i64 %4, 1
%6 = tail call i8* @brain_alloc_(i64 %5, i32 0)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @strcpy(i8* %6, i8* %3)
br label %10
10: ; preds = %1, %8
ret i8* %6
}