#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*/ fatal(int, char *);
char *realloc(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void realloc_strcpy(char **dest, char *src) {
  int len = strlen(src);
  *dest = realloc(*dest, len + 1);
  if (!*dest)
    fatal(2, "Memory allocation failed\n");
  strcpy(*dest, src);
}


x86 assembly (clang, -O0):
<realloc_strcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1197 <realloc_strcpy+0x57>
mov    $0x2,%edi
lea    0xe6e(%rip),%rsi
call   1060 <fatal@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc_strcpy>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    (%rbx),%rdi
lea    0x1(%rax),%esi
call   1050 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    1179 <realloc_strcpy+0x39>
lea    0xe94(%rip),%rsi
mov    $0x2,%edi
call   1060 <fatal@plt>
mov    (%rbx),%rax
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strcpy@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Memory allocation failed\0A\00"
define void @realloc_strcpy(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** %4
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %5
%8 = load i8**, i8*** %3
%9 = load i8*, i8** %8
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 1
%12 = call i8* @realloc(i8* %9, i32 %11)
%13 = load i8**, i8*** %3
store i8* %12, i8** %13
%14 = load i8**, i8*** %3
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 @fatal(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load i8**, i8*** %3
%21 = load i8*, i8** %20
%22 = load i8*, i8** %4
%23 = call i32 @strcpy(i8* %21, i8* %22)
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Memory allocation failed\0A\00"
define void @realloc_strcpy(i8** nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = load i8*, i8** %0
%5 = add nsw i32 %3, 1
%6 = tail call i8* @realloc(i8* %4, i32 %5)
store i8* %6, i8** %0
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @fatal(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %0
br label %11
11: ; preds = %8, %2
%12 = phi i8* [ %10, %8 ], [ %6, %2 ]
%13 = tail call i32 @strcpy(i8* %12, i8* %1)
ret void
}