#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
char *json_strdup(const char *str) {
  int nBytes = strlen(str) + 1;
  char *ret = (char *)malloc(strlen(str) + 1);
  if (ret == NULL)
    do {
      fprintf(stderr, "Out of memory.\n");
      exit(EXIT_FAILURE);
    } while (0);
  snprintf(ret, nBytes, "%s", str);
  return ret;
}


x86 assembly (clang, -O0):
<json_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b5 <json_strdup+0x65>
jmp    1192 <json_strdup+0x42>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rcx
lea    0xe49(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%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
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1195 <json_strdup+0x45>
mov    %rax,%rbx
add    $0x1,%ebp
lea    0xe90(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Out of memory.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i8* @json_strdup(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)
%7 = add nsw i32 %6, 1
store i32 %7, i32* %3
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %22
15: ; preds = %1
br label %16
16: ; preds = %15
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; No predecessors!
br label %22
22: ; preds = %21, %1
%23 = load i8*, i8** %4
%24 = load i32, i32* %3
%25 = load i8*, i8** %2
%26 = call i32 @snprintf(i8* %23, i32 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"Out of memory.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i8* @json_strdup(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = add nsw i32 %2, 1
%15 = tail call i32 @snprintf(i8* %6, i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
ret i8* %6
}