#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*/ die(char *);
int /*<<< orphan*/ release_pack_memory(scalar_t__, int);
char *strdup(char const *);
scalar_t__ strlen(char const *);
char *xstrdup(const char *str) {
  char *ret = strdup(str);
  if (!ret) {
    release_pack_memory(strlen(str) + 1, -1);
    ret = strdup(str);
    if (!ret)
      die("Out of memory, strdup failed");
  }
  return ret;
}


x86 assembly (clang, -O0):
<xstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <xstrdup+0x67>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
mov    $0xffffffff,%esi
call   1040 <release_pack_memory@plt>
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a2 <xstrdup+0x62>
lea    0xe63(%rip),%rdi
call   1050 <die@plt>
jmp    11a7 <xstrdup+0x67>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrdup>:
push   %rbx
mov    %rdi,%rbx
call   1060 <strdup@plt>
test   %rax,%rax
je     1150 <xstrdup+0x10>
pop    %rbx
ret
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
mov    $0xffffffff,%esi
call   1040 <release_pack_memory@plt>
mov    %rbx,%rdi
call   1060 <strdup@plt>
test   %rax,%rax
jne    114e <xstrdup+0xe>
lea    0xe86(%rip),%rdi
call   1050 <die@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Out of memory, strdup failed\00"
define i8* @xstrdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i8* @strdup(i8* %4)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %20, label %8
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call i64 @strlen(i8* %9)
%11 = add nsw i64 %10, 1
%12 = call i32 @release_pack_memory(i64 %11, i32 -1)
%13 = load i8*, i8** %2
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %8
%18 = call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %8
br label %20
20: ; preds = %19, %1
%21 = load i8*, i8** %3
ret i8* %21
}

LLVM IR (-O2):
@.str = [29 x i8] c"Out of memory, strdup failed\00"
define noalias i8* @xstrdup(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %12
4: ; preds = %1
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 1
%7 = tail call i32 @release_pack_memory(i64 %6, i32 -1)
%8 = tail call i8* @strdup(i8* %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %4, %10, %1
%13 = phi i8* [ %2, %1 ], [ %8, %4 ], [ null, %10 ]
ret i8* %13
}