#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);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int snprintf(char *, int, char *, char *);
int main() {
  int bufSize = 10;
  char *mystr = "This is my string!";
  char *buf = malloc(bufSize);
  if (snprintf(buf, bufSize, "%s", mystr) >= bufSize) {
    bufSize *= 2;
    printf("Not enough space. Trying %d bytes\n", bufSize);
    free(buf);
    buf = malloc(bufSize);
    if (snprintf(buf, bufSize, "%s", mystr) >= bufSize) {
      printf("Still not enough space. Aborting\n");
      exit(1);
    }
  }
  printf("There was enough space!\n");
  printf("buf: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xa,-0x8(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xe84(%rip),%rdx
call   1050 <snprintf@plt>
cmp    -0x8(%rbp),%eax
jl     1209 <main+0xb9>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xe35(%rip),%rdx
call   1050 <snprintf@plt>
cmp    -0x8(%rbp),%eax
jl     1204 <main+0xb4>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1209 <main+0xb9>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0xa,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe9e(%rip),%rdx
lea    0xe84(%rip),%rcx
mov    %rax,%rdi
mov    $0xa,%esi
call   1060 <snprintf@plt>
cmp    $0xa,%eax
jl     11d6 <main+0x76>
lea    0xe81(%rip),%rdi
mov    $0x14,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe56(%rip),%rdx
lea    0xe3c(%rip),%rcx
mov    %rax,%rdi
mov    $0x14,%esi
call   1060 <snprintf@plt>
cmp    $0x14,%eax
jge    11f7 <main+0x97>
lea    0xe65(%rip),%rdi
call   1040 <puts@plt>
lea    0xe50(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe5c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"This is my string!\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [35 x i8] c"Not enough space. Trying %d bytes\0A\00"
@.str.3 = [34 x i8] c"Still not enough space. Aborting\0A\00"
@.str.4 = [25 x i8] c"There was enough space!\0A\00"
@.str.5 = [9 x i8] c"buf: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
store i32 10, i32* %2
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %3
%5 = load i32, i32* %2
%6 = call i8* @malloc(i32 %5)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = load i32, i32* %2
%9 = load i8*, i8** %3
%10 = call i32 @snprintf(i8* %7, i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i32, i32* %2
%12 = icmp sge i32 %10, %11
br i1 %12, label %13, label %32
13: ; preds = %0
%14 = load i32, i32* %2
%15 = mul nsw i32 %14, 2
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i8*, i8** %4
%19 = call i32 @free(i8* %18)
%20 = load i32, i32* %2
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = load i32, i32* %2
%24 = load i8*, i8** %3
%25 = call i32 @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %2
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %13
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %13
br label %32
32: ; preds = %31, %0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%34 = load i8*, i8** %4
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"This is my string!\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [35 x i8] c"Not enough space. Trying %d bytes\0A\00"
@.str.5 = [9 x i8] c"buf: %s\0A\00"
@str = [24 x i8] c"There was enough space!\00"
@str.6 = [33 x i8] c"Still not enough space. Aborting\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%2 = tail call i32 @snprintf(i8* %1, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%3 = icmp sgt i32 %2, 9
br i1 %3, label %4, label %13
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 20)
%6 = tail call i32 @free(i8* %1)
%7 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%8 = tail call i32 @snprintf(i8* %7, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = icmp sgt i32 %8, 19
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %4, %0
%14 = phi i8* [ %7, %4 ], [ %1, %0 ]
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %14)
ret i32 0
}