#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*/ ENOMEM;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                         char const *const);
int /*<<< orphan*/ free(char *);
char *strdup(char const *const);
void p_strdup(char **const dst, const char *const src) {
  free(*dst);
  if (src == NULL) {
    *dst = NULL;
    return;
  }
  *dst = strdup(src);
  if (*dst == NULL) {
    error(EXIT_FAILURE, ENOMEM, "strdup(s=[%s])", src);
  }
}


x86 assembly (clang, -O0):
<p_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1167 <p_strdup+0x37>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
jmp    11aa <p_strdup+0x7a>
mov    -0x10(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11aa <p_strdup+0x7a>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
lea    0xe5b(%rip),%rdx
call   1040 <error@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p_strdup>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1030 <free@plt>
test   %r14,%r14
je     117f <p_strdup+0x4f>
mov    %r14,%rdi
call   1050 <strdup@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    1186 <p_strdup+0x56>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdx
mov    %r14,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <error@plt>
movq   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@ENOMEM = global i32 0
@.str = [15 x i8] c"strdup(s=[%s])\00"
define void @p_strdup(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%5 = load i8**, i8*** %3
%6 = load i8*, i8** %5
%7 = call i32 @free(i8* %6)
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i8**, i8*** %3
store i8* null, i8** %11
br label %24
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
%15 = load i8**, i8*** %3
store i8* %14, i8** %15
%16 = load i8**, i8*** %3
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i32, i32* @EXIT_FAILURE
%21 = load i32, i32* @ENOMEM
%22 = load i8*, i8** %4
%23 = call i32 @error(i32 %20, i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %10, %19, %12
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@ENOMEM = global i32 0
@.str = [15 x i8] c"strdup(s=[%s])\00"
define void @p_strdup(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = tail call i32 @free(i8* %3)
%5 = icmp eq i8* %1, null
br i1 %5, label %6, label %7
6: ; preds = %2
store i8* null, i8** %0
br label %14
7: ; preds = %2
%8 = tail call i8* @strdup(i8* %1)
store i8* %8, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i32, i32* @EXIT_FAILURE
%12 = load i32, i32* @ENOMEM
%13 = tail call i32 @error(i32 %11, i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %14
14: ; preds = %6, %10, %7
ret void
}