#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__ ENOMEM;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ errno;
char *strdup(char *);
char *mx_strdup_forever(char *str) {
  char *dup;
  do {
    dup = strdup(str);
    assert(dup || (!dup && errno == ENOMEM));
  } while (!dup);
  return dup;
}


x86 assembly (clang, -O0):
<mx_strdup_forever>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jne    1179 <mx_strdup_forever+0x59>
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x12(%rbp)
jne    1173 <mx_strdup_forever+0x53>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    112c <mx_strdup_forever+0xc>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mx_strdup_forever>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2ea6(%rip),%r15
mov    0x2ea7(%rip),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
setne  %al
mov    (%r15),%rcx
cmp    (%r12),%rcx
sete   %cl
or     %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
test   %rbx,%rbx
je     1140 <mx_strdup_forever+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOMEM = global i64 0
define i8* @mx_strdup_forever(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
br label %4
4: ; preds = %22, %1
%5 = load i8*, i8** %2
%6 = call i8* @strdup(i8* %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %18, label %9
9: ; preds = %4
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOMEM
%15 = icmp eq i64 %13, %14
br label %16
16: ; preds = %12, %9
%17 = phi i1 [ false, %9 ], [ %15, %12 ]
br label %18
18: ; preds = %16, %4
%19 = phi i1 [ true, %4 ], [ %17, %16 ]
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
br label %22
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = icmp ne i8* %23, null
%25 = xor i1 %24, true
br i1 %25, label %4, label %26
26: ; preds = %22
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
@errno = global i64 0
@ENOMEM = global i64 0
define noalias i8* @mx_strdup_forever(i8* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = tail call i8* @strdup(i8* %0)
%4 = icmp eq i8* %3, null
%5 = load i64, i64* @errno
%6 = load i64, i64* @ENOMEM
%7 = icmp eq i64 %5, %6
%8 = xor i1 %4, true
%9 = select i1 %8, i1 true, i1 %7
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
br i1 %4, label %2, label %12
12: ; preds = %2
ret i8* %3
}