#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ realloc(char *, int);
int snprintf(char *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ strlen(char *);
int string_length(int number) {
  char *buf;
  buf = (char *)malloc(1);
  memset(buf, 0, 1);
  int currSize = 1;
  while (1) {
    int nxtSize = snprintf(buf, strlen(buf), "%d");
    if (nxtSize == -1) {
      return currSize;
    } else {
      currSize += nxtSize;
      buf = (char *)realloc(buf, currSize);
      memset(buf, 0, currSize);
    }
  }
  return currSize;
}


x86 assembly (clang, -O0):
<string_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
lea    0xe62(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b9 <string_length+0x69>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
jmp    1180 <string_length+0x30>

x86 assembly (clang, -O2):
<string_length>:
push   %r15
push   %r14
push   %rbx
mov    $0x1,%ebx
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0xe7b(%rip),%rdx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <snprintf@plt>
cmp    $0xffffffff,%eax
je     11d5 <string_length+0x85>
mov    $0x1,%ebx
lea    0xe60(%rip),%r15
add    %eax,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %r15,%rdx
call   1040 <snprintf@plt>
cmp    $0xffffffff,%eax
jne    11a0 <string_length+0x50>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i32 @string_length(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i64 @malloc(i32 1)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @memset(i8* %8, i32 0, i32 1)
store i32 1, i32* %4
br label %10
10: ; preds = %1, %30
%11 = load i8*, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = call i32 @snprintf(i8* %11, i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = load i32, i32* %4
ret i32 %18
19: ; preds = %10
%20 = load i32, i32* %5
%21 = load i32, i32* %4
%22 = add nsw i32 %21, %20
store i32 %22, i32* %4
%23 = load i8*, i8** %3
%24 = load i32, i32* %4
%25 = call i64 @realloc(i8* %23, i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %3
%27 = load i8*, i8** %3
%28 = load i32, i32* %4
%29 = call i32 @memset(i8* %27, i32 0, i32 %28)
br label %30
30: ; preds = %19
br label %10
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32 @string_length(i32 %0) {
%2 = tail call i64 @malloc(i32 1)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @memset(i8* %3, i32 0, i32 1)
%5 = tail call i32 @strlen(i8* %3)
%6 = tail call i32 @snprintf(i8* %3, i32 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32 %6, -1
br i1 %7, label %19, label %8
8: ; preds = %1, %8
%9 = phi i32 [ %17, %8 ], [ %6, %1 ]
%10 = phi i32 [ %12, %8 ], [ 1, %1 ]
%11 = phi i8* [ %14, %8 ], [ %3, %1 ]
%12 = add nsw i32 %9, %10
%13 = tail call i64 @realloc(i8* %11, i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @memset(i8* %14, i32 0, i32 %12)
%16 = tail call i32 @strlen(i8* %14)
%17 = tail call i32 @snprintf(i8* %14, i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %8
19: ; preds = %8, %1
%20 = phi i32 [ 1, %1 ], [ %12, %8 ]
ret i32 %20
}