#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_error(char *, char *, int, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *cstr_from_int(int val) {
  char buffer[1000];
  snprintf(buffer, 999, "%d", val);
  int size = strlen(buffer);
  char *str = (char *)malloc((sizeof(char)) * (size + 1));
  if (str == NULL)
    fatal_error(
        "cubestr.c", "cstr_from_int", 137,
        "Cannot allocate memory for return c-string while converting from int");
  strcpy(str, buffer);
  return str;
}


x86 assembly (clang, -O0):
<cstr_from_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x3f0(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x3e7,%esi
lea    0xe8c(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x3f0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3f4(%rbp)
mov    -0x3f4(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x400(%rbp)
cmpq   $0x0,-0x400(%rbp)
jne    11d5 <cstr_from_int+0x85>
lea    0xe46(%rip),%rdi
lea    0xe49(%rip),%rsi
mov    $0x89,%edx
lea    0xe4b(%rip),%rcx
call   1060 <fatal_error@plt>
mov    -0x400(%rbp),%rdi
lea    -0x3f0(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x400(%rbp),%rax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstr_from_int>:
push   %rbx
sub    $0x3f0,%rsp
mov    %edi,%ecx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x3e7,%esi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a8 <cstr_from_int+0x58>
lea    0xe73(%rip),%rdi
lea    0xe76(%rip),%rsi
lea    0xe7d(%rip),%rcx
mov    $0x89,%edx
call   1060 <fatal_error@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x3f0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [10 x i8] c"cubestr.c\00"
@.str.2 = [14 x i8] c"cstr_from_int\00"
@.str.3 = [69 x i8] c"Cannot allocate memory for return c-string while converting from int\00"
define i8* @cstr_from_int(i32 %0) {
%2 = alloca i32
%3 = alloca [1000 x i8]
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @snprintf(i8* %6, i32 999, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%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 %22
20: ; preds = %1
%21 = call i32 @fatal_error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 137, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %1
%23 = load i8*, i8** %5
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = load i8*, i8** %5
ret i8* %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [10 x i8] c"cubestr.c\00"
@.str.2 = [14 x i8] c"cstr_from_int\00"
@.str.3 = [69 x i8] c"Cannot allocate memory for return c-string while converting from int\00"
define i8* @cstr_from_int(i32 %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 999, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32 @strlen(i8* %3)
%6 = add nsw i32 %5, 1
%7 = call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @fatal_error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 137, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = call i32 @strcpy(i8* %8, i8* %3)
ret i8* %8
}