#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*/ sprintf(char *, char *, int);
int cddb_sum(int n) {
  char buf[12], *p;
  int ret = 0;
  sprintf(buf, "%u", n);
  for (p = buf; *p != '\0'; p++)
    ret += (*p - '0');
  return (ret);
}


x86 assembly (clang, -O0):
<cddb_sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xed0(%rip),%rsi
call   1030 <sprintf@plt>
lea    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116e <cddb_sum+0x5e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    113d <cddb_sum+0x2d>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cddb_sum>:
sub    $0x18,%rsp
mov    %edi,%edx
lea    0xee3(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1030 <sprintf@plt>
mov    0xc(%rsp),%dl
test   %dl,%dl
je     1158 <cddb_sum+0x48>
lea    0xd(%rsp),%rcx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
movsbl %dl,%edx
add    %edx,%eax
add    $0xffffffd0,%eax
movzbl (%rcx),%edx
add    $0x1,%rcx
test   %dl,%dl
jne    1140 <cddb_sum+0x30>
add    $0x18,%rsp
ret
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%u\00"
define i32 @cddb_sum(i32 %0) {
%2 = alloca i32
%3 = alloca [12 x i8]
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
%6 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
store i8* %9, i8** %4
br label %10
10: ; preds = %22, %1
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = load i8*, i8** %4
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = sub nsw i32 %18, 48
%20 = load i32, i32* %5
%21 = add nsw i32 %20, %19
store i32 %21, i32* %5
br label %22
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
br label %10
25: ; preds = %10
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"%u\00"
define i32 @cddb_sum(i32 %0) {
%2 = alloca [12 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i8, i8* %3
%6 = icmp eq i8 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %1, %7
%8 = phi i8 [ %15, %7 ], [ %5, %1 ]
%9 = phi i32 [ %13, %7 ], [ 0, %1 ]
%10 = phi i8* [ %14, %7 ], [ %3, %1 ]
%11 = sext i8 %8 to i32
%12 = add i32 %9, -48
%13 = add i32 %12, %11
%14 = getelementptr inbounds i8, i8* %10, i64 1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %7, %1
%18 = phi i32 [ 0, %1 ], [ %13, %7 ]
ret i32 %18
}