#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(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
void string_update_real(char **old, const char *new) {
  if (*old != NULL)
    free(*old);
  if (new == NULL) {
    *old = NULL;
  } else {
    size_t length = strlen(new);
    *old = (char *)malloc(length + 1);
    memcpy(*old, new, length);
    (*old)[length] = '\0';
  }
}


x86 assembly (clang, -O0):
<string_update_real>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     116a <string_update_real+0x2a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1185 <string_update_real+0x45>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
jmp    11cc <string_update_real+0x8c>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_update_real>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1158 <string_update_real+0x18>
call   1030 <free@plt>
test   %r14,%r14
je     118b <string_update_real+0x4b>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    (%r15),%rax
movb   $0x0,(%rax,%rbx,1)
jmp    1192 <string_update_real+0x52>
movq   $0x0,(%r15)
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @string_update_real(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%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 = icmp eq i8* %1, null
br i1 %8, label %9, label %10
9: ; preds = %7
store i8* null, i8** %0
br label %18
10: ; preds = %7
%11 = tail call i64 @strlen(i8* %1)
%12 = add i64 %11, 1
%13 = tail call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %0
%15 = tail call i32 @memcpy(i8* %14, i8* %1, i64 %11)
%16 = load i8*, i8** %0
%17 = getelementptr inbounds i8, i8* %16, i64 %11
store i8 0, i8* %17
br label %18
18: ; preds = %10, %9
ret void
}