#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int const);
void *snprintf(char *, int const, char *, long);
int /*<<< orphan*/ stderr;
char *long2string(long value) {
  char *retval;
  int nchars;
  const int n = snprintf(NULL, 0, "%li", value);
  retval = (char *)malloc((n + 1) * sizeof(char));
  if (retval == NULL) {
    fprintf(stderr, "Error during allocation in long2string!\n");
    return NULL;
  }
  nchars = snprintf(retval, n + 1, "%li", value);
  if (nchars != n) {
    free(retval);
    fprintf(stderr, "Error during string format in long2string!\n");
    return NULL;
  }
  return retval;
}


x86 assembly (clang, -O0):
<long2string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xea3(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a9 <long2string+0x69>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1205 <long2string+0xc5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rcx
lea    0xe42(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     11fd <long2string+0xbd>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1205 <long2string+0xc5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<long2string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xeaf(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    %rax,%r15
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     119c <long2string+0x5c>
mov    %rax,%rbx
lea    0xe87(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
cmp    %r15d,%eax
je     11b3 <long2string+0x73>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe93(%rip),%rsi
jmp    11a3 <long2string+0x63>
lea    0xe61(%rip),%rsi
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%li\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Error during allocation in long2string!\0A\00"
@.str.2 = [44 x i8] c"Error during string format in long2string!\0A\00"
define i8* @long2string(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = call i8* @snprintf(i8* null, i32 0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %7)
%9 = ptrtoint i8* %8 to i32
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 1
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %39
22: ; preds = %1
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = load i64, i64* %3
%27 = call i8* @snprintf(i8* %23, i32 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %26)
%28 = ptrtoint i8* %27 to i32
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = load i32, i32* %6
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %37
32: ; preds = %22
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %39
37: ; preds = %22
%38 = load i8*, i8** %4
store i8* %38, i8** %2
br label %39
39: ; preds = %37, %32, %19
%40 = load i8*, i8** %2
ret i8* %40
}

LLVM IR (-O2):
@.str = [4 x i8] c"%li\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Error during allocation in long2string!\0A\00"
@.str.2 = [44 x i8] c"Error during string format in long2string!\0A\00"
define i8* @long2string(i64 %0) {
%2 = tail call i8* @snprintf(i8* null, i32 0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %0)
%3 = ptrtoint i8* %2 to i64
%4 = trunc i64 %3 to i32
%5 = add nsw i32 %4, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %16, label %9
9: ; preds = %1
%10 = tail call i8* @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %0)
%11 = ptrtoint i8* %10 to i64
%12 = trunc i64 %11 to i32
%13 = icmp eq i32 %12, %4
br i1 %13, label %20, label %14
14: ; preds = %9
%15 = tail call i32 @free(i8* %7)
br label %16
16: ; preds = %1, %14
%17 = phi i8* [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), %14 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), %1 ]
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* %17)
br label %20
20: ; preds = %16, %9
%21 = phi i8* [ %7, %9 ], [ null, %16 ]
ret i8* %21
}