#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int snprintf(char *, int, char *, int);
char *givemeastring(int i) {
  char *answer = malloc(16);
  int r = snprintf(answer, 16, "%d", i);
  if ((r >= 0) && (r < 16)) {
    return answer;
  } else {
    printf("bad");
    free(answer);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<givemeastring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x10,%esi
lea    0xe94(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1195 <givemeastring+0x55>
cmpl   $0x10,-0x1c(%rbp)
jge    1195 <givemeastring+0x55>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <givemeastring+0x72>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<givemeastring>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xea7(%rip),%rdx
mov    %rax,%rdi
mov    $0x10,%esi
mov    %ebp,%ecx
call   1050 <snprintf@plt>
cmp    $0x10,%eax
jb     1183 <givemeastring+0x43>
lea    0xe8f(%rip),%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"bad\00"
define i8* @givemeastring(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i8* @malloc(i32 16)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 16
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i8*, i8** %4
store i8* %16, i8** %2
br label %21
17: ; preds = %12, %1
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
store i8* null, i8** %2
br label %21
21: ; preds = %17, %15
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"bad\00"
define i8* @givemeastring(i32 %0) {
%2 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%3 = tail call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = icmp ult i32 %3, 16
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @free(i8* %2)
br label %8
8: ; preds = %1, %5
%9 = phi i8* [ null, %5 ], [ %2, %1 ]
ret i8* %9
}