#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 /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char const *);
char *json_strdup(const char *str) {
  int len;
  char *copy = NULL;
  len = strlen(str) + 1;
  copy = (char *)malloc(len);
  if (NULL == copy) {
    return NULL;
  }
  memset(copy, 0, len * sizeof(char));
  memcpy(copy, str, len);
  return copy;
}


x86 assembly (clang, -O0):
<json_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1188 <json_strdup+0x48>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <json_strdup+0x75>
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_strdup>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     117b <json_strdup+0x3b>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
jmp    117d <json_strdup+0x3d>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @json_strdup(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 %10, label %7
7: ; preds = %1
%8 = tail call i32 @memset(i8* %5, i32 0, i32 %3)
%9 = tail call i32 @memcpy(i8* %5, i8* %0, i32 %3)
br label %10
10: ; preds = %1, %7
%11 = phi i8* [ %5, %7 ], [ null, %1 ]
ret i8* %11
}