#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*/ free(char *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ safeStrCpy(char *, char *, scalar_t__);
scalar_t__ strlen(char *);
char *StrSavePtr(char *s, char **savePtr) {
  if (*savePtr) {
    free(*savePtr);
  }
  if ((*savePtr = (char *)malloc(strlen(s) + 1))) {
    safeStrCpy(*savePtr, s, strlen(s) + 1);
  }
  return (*savePtr);
}


x86 assembly (clang, -O0):
<StrSavePtr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     116a <StrSavePtr+0x2a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
je     11c0 <StrSavePtr+0x80>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1060 <safeStrCpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrSavePtr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%rdi
test   %rdi,%rdi
je     1158 <StrSavePtr+0x18>
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
test   %rax,%rax
je     118e <StrSavePtr+0x4e>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <safeStrCpy@plt>
mov    (%r15),%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @StrSavePtr(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
store i8* %0, i8** %3
store i8** %1, i8*** %4
%5 = load i8**, i8*** %4
%6 = load i8*, i8** %5
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8**, i8*** %4
%10 = load i8*, i8** %9
%11 = call i32 @free(i8* %10)
br label %12
12: ; preds = %8, %2
%13 = load i8*, i8** %3
%14 = call i64 @strlen(i8* %13)
%15 = add nsw i64 %14, 1
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
%18 = load i8**, i8*** %4
store i8* %17, i8** %18
%19 = icmp ne i8* %17, null
br i1 %19, label %20, label %28
20: ; preds = %12
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = load i8*, i8** %3
%24 = load i8*, i8** %3
%25 = call i64 @strlen(i8* %24)
%26 = add nsw i64 %25, 1
%27 = call i32 @safeStrCpy(i8* %22, i8* %23, i64 %26)
br label %28
28: ; preds = %20, %12
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
ret i8* %30
}

LLVM IR (-O2):
define i8* @StrSavePtr(i8* %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @free(i8* %3)
br label %7
7: ; preds = %5, %2
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = add nsw i64 %8, 1
%10 = tail call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %1
%12 = icmp eq i64 %10, 0
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%15 = add nsw i64 %14, 1
%16 = tail call i32 @safeStrCpy(i8* %11, i8* %0, i64 %15)
%17 = load i8*, i8** %1
br label %18
18: ; preds = %13, %7
%19 = phi i8* [ %17, %13 ], [ %11, %7 ]
ret i8* %19
}