#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*/ assert(int);
char *realloc(char *, int const);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int const);
void set_string_buf(char **buf, const char *src) {
  if (*buf == src) {
    return;
  }
  const int len = strlen(src) + 1;
  *buf = realloc(*buf, len);
  assert(*buf && "Failed to realloc");
  strncpy(*buf, src, len);
}


x86 assembly (clang, -O0):
<set_string_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    1166 <set_string_buf+0x26>
jmp    11cb <set_string_buf+0x8b>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmpq   $0x0,(%rcx)
mov    %al,-0x15(%rbp)
je     11ab <set_string_buf+0x6b>
mov    $0x1,%al
mov    %al,-0x15(%rbp)
jmp    11ab <set_string_buf+0x6b>
mov    -0x15(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_string_buf>:
push   %rbp
push   %r14
push   %rbx
cmp    %rsi,(%rdi)
je     1186 <set_string_buf+0x46>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%ebp
mov    (%rbx),%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,(%rbx)
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    (%rbx),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <strncpy@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Failed to realloc\00"
define void @set_string_buf(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
store i8** %0, i8*** %3
store i8* %1, i8** %4
%6 = load i8**, i8*** %3
%7 = load i8*, i8** %6
%8 = load i8*, i8** %4
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %11
10: ; preds = %2
br label %33
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 1
store i32 %14, i32* %5
%15 = load i8**, i8*** %3
%16 = load i8*, i8** %15
%17 = load i32, i32* %5
%18 = call i8* @realloc(i8* %16, i32 %17)
%19 = load i8**, i8*** %3
store i8* %18, i8** %19
%20 = load i8**, i8*** %3
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %11
br label %24
24: ; preds = %23, %11
%25 = phi i1 [ false, %11 ], [ true, %23 ]
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i8**, i8*** %3
%29 = load i8*, i8** %28
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = call i32 @strncpy(i8* %29, i8* %30, i32 %31)
br label %33
33: ; preds = %24, %10
ret void
}

LLVM IR (-O2):
define void @set_string_buf(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, %1
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %6, 1
%8 = load i8*, i8** %0
%9 = tail call i8* @realloc(i8* %8, i32 %7)
store i8* %9, i8** %0
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = load i8*, i8** %0
%14 = tail call i32 @strncpy(i8* %13, i8* %1, i32 %7)
br label %15
15: ; preds = %2, %5
ret void
}