#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 */
typedef scalar_t__ uint64_t;
typedef int /*<<< orphan*/ mpz_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *mpz_get_str(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
uint64_t digitalSum(mpz_t i) {
  uint64_t sum = 0;
  char *tmp = NULL;
  tmp = mpz_get_str(NULL, 10, i);
  for (char *str = tmp; *str != '\0'; sum += (uint64_t)(*str++ - '0'))
    ;
  free(tmp);
  return sum;
}


x86 assembly (clang, -O0):
<digitalSum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xa,%esi
call   1040 <mpz_get_str@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1191 <digitalSum+0x71>
jmp    116d <digitalSum+0x4d>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
sub    $0x30,%eax
cltq
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1158 <digitalSum+0x38>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<digitalSum>:
push   %rbx
mov    %edi,%edx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0xa,%esi
call   1040 <mpz_get_str@plt>
mov    (%rax),%cl
test   %cl,%cl
je     1156 <digitalSum+0x36>
mov    %rax,%rdx
add    $0x1,%rdx
xor    %ebx,%ebx
movsbq %cl,%rcx
add    %rcx,%rbx
add    $0xffffffffffffffd0,%rbx
movzbl (%rdx),%ecx
add    $0x1,%rdx
test   %cl,%cl
jne    1140 <digitalSum+0x20>
mov    %rax,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i64 @digitalSum(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %2
store i64 0, i64* %3
store i8* null, i8** %4
%6 = load i32, i32* %2
%7 = call i8* @mpz_get_str(i32* null, i32 10, i32 %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
store i8* %8, i8** %5
br label %9
9: ; preds = %15, %1
%10 = load i8*, i8** %5
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %9
br label %15
15: ; preds = %14
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %5
%18 = load i8, i8* %16
%19 = sext i8 %18 to i32
%20 = sub nsw i32 %19, 48
%21 = sext i32 %20 to i64
%22 = load i64, i64* %3
%23 = add nsw i64 %22, %21
store i64 %23, i64* %3
br label %9
24: ; preds = %9
%25 = load i8*, i8** %4
%26 = call i32 @free(i8* %25)
%27 = load i64, i64* %3
ret i64 %27
}

LLVM IR (-O2):
define i64 @digitalSum(i32 %0) {
%2 = tail call i8* @mpz_get_str(i32* null, i32 10, i32 %0)
%3 = load i8, i8* %2
%4 = icmp eq i8 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %8, %1
%6 = phi i64 [ 0, %1 ], [ %15, %8 ]
%7 = tail call i32 @free(i8* %2)
ret i64 %6
8: ; preds = %1, %8
%9 = phi i8 [ %16, %8 ], [ %3, %1 ]
%10 = phi i8* [ %13, %8 ], [ %2, %1 ]
%11 = phi i64 [ %15, %8 ], [ 0, %1 ]
%12 = sext i8 %9 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = add i64 %11, -48
%15 = add i64 %14, %12
%16 = load i8, i8* %13
%17 = icmp eq i8 %16, 0
br i1 %17, label %5, label %8
}