#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(void) {
  char *str = (char *)malloc(sizeof(char) * 3);
  strncpy(str, "hi", 3);
  printf("%s\n", str);
  char *str2 = realloc(str, sizeof(char) * 6);
  if (str2 == NULL) {
    free(str);
    printf("realloc failed.\n");
    exit(EXIT_FAILURE);
  }
  strncpy(str2, "hello", 6);
  printf("%s\n", str2);
  free(str2);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe78(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x6,%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e6 <main+0x86>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe27(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe88(%rip),%rsi
mov    %rax,%rdi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    %r14,%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
mov    $0x6,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     11d7 <main+0x77>
mov    %rax,%rbx
lea    0xe5a(%rip),%rsi
mov    %rax,%rdi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xe23(%rip),%rdi
call   1050 <puts@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"hi\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [17 x i8] c"realloc failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [6 x i8] c"hello\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i64 @malloc(i32 3)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strncpy(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 3)
%8 = load i8*, i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %2
%11 = call i8* @realloc(i8* %10, i32 6)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = load i8*, i8** %2
%16 = call i32 @free(i8* %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %0
%21 = load i8*, i8** %3
%22 = call i32 @strncpy(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 6)
%23 = load i8*, i8** %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
%27 = load i32, i32* @EXIT_SUCCESS
ret i32 %27
}

LLVM IR (-O2):
@.str = [3 x i8] c"hi\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [6 x i8] c"hello\00"
@EXIT_SUCCESS = global i32 0
@str = [16 x i8] c"realloc failed.\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 3)
%2 = inttoptr i64 %1 to i8*
%3 = tail call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 3)
%4 = tail call i32 @puts(i8* dereferenceable(1) %2)
%5 = tail call i8* @realloc(i8* %2, i32 6)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = tail call i32 @free(i8* %2)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = tail call i32 @strncpy(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 6)
%14 = tail call i32 @puts(i8* %5)
%15 = tail call i32 @free(i8* %5)
%16 = load i32, i32* @EXIT_SUCCESS
ret i32 %16
}