#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 arrayToInt(int *, int);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int nDigits(int);
int reverse(int number, int *digits) {
  int i;
  int res;
  int *rev;
  *digits = nDigits(number);
  rev = (int *)malloc(*digits * sizeof(int));
  i = 0;
  while (number > 0) {
    rev[i] = number % 10;
    number = number / 10;
    i++;
  }
  res = arrayToInt(rev, i);
  free(rev);
  return res;
}


x86 assembly (clang, -O0):
<reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <nDigits@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    11b8 <reverse+0x78>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117c <reverse+0x3c>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <arrayToInt@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <nDigits@plt>
mov    %eax,(%rbx)
lea    0x0(,%rax,4),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
xor    %esi,%esi
test   %ebp,%ebp
jle    1192 <reverse+0x52>
mov    $0xcccccccd,%eax
nopl   0x0(%rax)
mov    %ebp,%ecx
imul   %rax,%rcx
shr    $0x23,%rcx
lea    (%rcx,%rcx,1),%edx
lea    (%rdx,%rdx,4),%edx
mov    %ebp,%edi
sub    %edx,%edi
mov    %edi,(%rbx,%rsi,4)
add    $0x1,%rsi
cmp    $0x9,%ebp
mov    %ecx,%ebp
ja     1170 <reverse+0x30>
mov    %rbx,%rdi
call   1060 <arrayToInt@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @reverse(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %3
store i32* %1, i32** %4
%8 = load i32, i32* %3
%9 = call i32 @nDigits(i32 %8)
%10 = load i32*, i32** %4
store i32 %9, i32* %10
%11 = load i32*, i32** %4
%12 = load i32, i32* %11
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 4
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %7
store i32 0, i32* %5
br label %18
18: ; preds = %21, %2
%19 = load i32, i32* %3
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = load i32, i32* %3
%23 = srem i32 %22, 10
%24 = load i32*, i32** %7
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
store i32 %23, i32* %27
%28 = load i32, i32* %3
%29 = sdiv i32 %28, 10
store i32 %29, i32* %3
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %18
32: ; preds = %18
%33 = load i32*, i32** %7
%34 = load i32, i32* %5
%35 = call i32 @arrayToInt(i32* %33, i32 %34)
store i32 %35, i32* %6
%36 = load i32*, i32** %7
%37 = call i32 @free(i32* %36)
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
define i32 @reverse(i32 %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @nDigits(i32 %0)
store i32 %3, i32* %1
%4 = shl i32 %3, 2
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i32*
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %18
8: ; preds = %2, %8
%9 = phi i64 [ %14, %8 ], [ 0, %2 ]
%10 = phi i32 [ %13, %8 ], [ %0, %2 ]
%11 = urem i32 %10, 10
%12 = getelementptr inbounds i32, i32* %6, i64 %9
store i32 %11, i32* %12
%13 = udiv i32 %10, 10
%14 = add nuw i64 %9, 1
%15 = icmp ult i32 %10, 10
br i1 %15, label %16, label %8
16: ; preds = %8
%17 = trunc i64 %14 to i32
br label %18
18: ; preds = %16, %2
%19 = phi i32 [ 0, %2 ], [ %17, %16 ]
%20 = tail call i32 @arrayToInt(i32* %6, i32 %19)
%21 = tail call i32 @free(i32* %6)
ret i32 %20
}