#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*/ memcpy(char *, char const *, size_t);
scalar_t__ ralloc_array_size(void const *, int, size_t);
size_t strlen(char const *);
char *ralloc_strdup(const void *ctx, const char *str) {
  size_t n;
  char *ptr;
  if ((str == NULL))
    return NULL;
  n = strlen(str);
  ptr = ((char *)ralloc_array_size(ctx, sizeof(char), n + 1));
  memcpy(ptr, str, n);
  ptr[n] = '\0';
  return ptr;
}


x86 assembly (clang, -O0):
<ralloc_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1158 <ralloc_strdup+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11a4 <ralloc_strdup+0x74>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
mov    $0x1,%esi
call   1030 <ralloc_array_size@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ralloc_strdup>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1174 <ralloc_strdup+0x44>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <ralloc_array_size@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    1176 <ralloc_strdup+0x46>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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