#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 */
char *Rmalloc(size_t, char const *);
int /*<<< orphan*/ Rmalloc_set_flags(char *, int, char *);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *Rstrdup(const char *s, const char *file) {
  size_t size;
  char *ret;
  if (s == NULL) {
    fprintf(stderr,
            "<MALLOC_DEBUG>\t"
            "Calling strdup(NULL) (in %s)\n",
            file);
    abort();
  }
  size = strlen(s) + 1;
  ret = Rmalloc(size, file);
  if (ret) {
    strcpy(ret, s);
    Rmalloc_set_flags(ret, (0x0001 << 1), "<by strdup>");
    return ret;
  } else {
    fprintf(stderr,
            "<MALLOC_DEBUG>\t"
            "WARNING: Out of memory! Returning NULL (in %s)\n",
            file);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<Rstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119b <Rstrdup+0x3b>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe71(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <Rmalloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f8 <Rstrdup+0x98>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    $0x2,%esi
lea    0xe47(%rip),%rdx
call   1080 <Rmalloc_set_flags@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1219 <Rstrdup+0xb9>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Rstrdup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   %rdi,%rdi
je     11cd <Rstrdup+0x6d>
mov    %rdi,%r15
call   1060 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdi
mov    %r14,%rsi
call   1040 <Rmalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ac <Rstrdup+0x4c>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
lea    0xe90(%rip),%rdx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1080 <Rmalloc_set_flags@plt>
jmp    11c4 <Rstrdup+0x64>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"<MALLOC_DEBUG>\09Calling strdup(NULL) (in %s)\0A\00"
@.str.1 = [12 x i8] c"<by strdup>\00"
@.str.2 = [63 x i8] c"<MALLOC_DEBUG>\09WARNING: Out of memory! Returning NULL (in %s)\0A\00"
define i8* @Rstrdup(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** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %5
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 (...) @abort()
unreachable
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = load i8*, i8** %5
%22 = call i8* @Rmalloc(i64 %20, i8* %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %32
25: ; preds = %15
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %7
%30 = call i32 @Rmalloc_set_flags(i8* %29, i32 2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%31 = load i8*, i8** %7
store i8* %31, i8** %3
br label %36
32: ; preds = %15
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %5
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i8* null, i8** %3
br label %36
36: ; preds = %32, %25
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"<MALLOC_DEBUG>\09Calling strdup(NULL) (in %s)\0A\00"
@.str.1 = [12 x i8] c"<by strdup>\00"
@.str.2 = [63 x i8] c"<MALLOC_DEBUG>\09WARNING: Out of memory! Returning NULL (in %s)\0A\00"
define i8* @Rstrdup(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %1)
%7 = tail call i32 (...) @abort()
unreachable
8: ; preds = %2
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = tail call i8* @Rmalloc(i64 %11, i8* %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = tail call i32 @strcpy(i8* %12, i8* %0)
%16 = tail call i32 @Rmalloc_set_flags(i8* %12, i32 2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %20
17: ; preds = %8
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %20
20: ; preds = %17, %14
ret i8* %12
}