#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(int);
scalar_t__ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ logaroo(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
char *growstring(char *string, int newlength) {
  char *newstring;
  char logString[8192];
  0 && fprintf(stderr, "Growing string to %d\n", newlength);
  sprintf(logString, "Growing string to %d", newlength);
  logaroo(logString);
  newstring = (char *)malloc(newlength * sizeof(char));
  if (newstring == NULL) {
    fprintf(stderr, "HALP! FATAIL ERRORRS! MALLOC NO GOOD \n");
    exit(1);
  }
  memset(newstring, 0, newlength * sizeof(char));
  strncpy(newstring, string, newlength - 1);
  free(string);
  return (newstring);
}


x86 assembly (clang, -O0):
<growstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x2020(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe5d(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x2020(%rbp),%rdi
call   1030 <logaroo@plt>
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f3 <growstring+0x73>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1050 <strncpy@plt>
mov    -0x8(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<growstring>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe69(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <logaroo@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11ee <growstring+0x6e>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
add    $0xffffffff,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <strncpy@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Growing string to %d\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"HALP! FATAIL ERRORRS! MALLOC NO GOOD \0A\00"
define i8* @growstring(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [8192 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* %4
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%11 = call i32 @logaroo(i8* %10)
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 1
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i64 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i8*, i8** %5
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i32 @memset(i8* %25, i32 0, i32 %29)
%31 = load i8*, i8** %5
%32 = load i8*, i8** %3
%33 = load i32, i32* %4
%34 = sub nsw i32 %33, 1
%35 = call i32 @strncpy(i8* %31, i8* %32, i32 %34)
%36 = load i8*, i8** %3
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %5
ret i8* %38
}

LLVM IR (-O2):
@.str = [21 x i8] c"Growing string to %d\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"HALP! FATAIL ERRORRS! MALLOC NO GOOD \0A\00"
define i8* @growstring(i8* %0, i32 %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = call i32 @logaroo(i8* %4)
%7 = call i64 @malloc(i32 %1)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i64 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i32 @memset(i8* %8, i32 0, i32 %1)
%16 = add nsw i32 %1, -1
%17 = call i32 @strncpy(i8* %8, i8* %0, i32 %16)
%18 = call i32 @free(i8* %0)
ret i8* %8
}