#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 int /*<<< orphan*/ digit;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(void) {
  int i, sum, temp;
  char digit[1002];
  while (1) {
    fgets(digit, sizeof(digit), stdin);
    sscanf(digit, "%d\n", &temp);
    if (temp == 0)
      break;
    sum = 0;
    for (i = 0; i < strlen(digit) - 1; i++)
      sum += digit[i] - '0';
    printf("%d\n", sum);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x400(%rbp),%rdi
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
mov    $0x3ea,%esi
call   1050 <fgets@plt>
lea    -0x400(%rbp),%rdi
lea    0xe86(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1060 <sscanf@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1192 <main+0x52>
jmp    11ff <main+0xbf>
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x404(%rbp)
lea    -0x400(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x404(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11eb <main+0xab>
movslq -0x8(%rbp),%rax
movsbl -0x400(%rbp,%rax,1),%eax
ff
sub    $0x30,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a0 <main+0x60>
mov    -0xc(%rbp),%esi
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
jmp    1152 <main+0x12>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    0x2e90(%rip),%r13
mov    0x0(%r13),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3ea,%esi
call   1050 <fgets@plt>
lea    0xe8b(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <sscanf@plt>
cmpl   $0x0,0xc(%rsp)
je     1212 <main+0xd2>
lea    0x10(%rsp),%r12
lea    0xe67(%rip),%r14
lea    0xc(%rsp),%r15
jmp    11d0 <main+0x90>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    0x0(%r13),%edx
mov    %r12,%rdi
mov    $0x3ea,%esi
call   1050 <fgets@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <sscanf@plt>
cmpl   $0x0,0xc(%rsp)
je     1212 <main+0xd2>
mov    %r12,%rdi
call   1030 <strlen@plt>
xor    %ebx,%ebx
cmp    $0x2,%eax
jl     11a0 <main+0x60>
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x10(%rsp,%rbp,1),%eax
add    %eax,%ebx
add    $0xffffffd0,%ebx
add    $0x1,%rbp
mov    %r12,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%eax
cltq
cmp    %rax,%rbp
jl     11f0 <main+0xb0>
jmp    11a0 <main+0x60>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1002 x i8]
store i32 0, i32* %1
br label %6
6: ; preds = %0, %34
%7 = getelementptr inbounds [1002 x i8], [1002 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %7, i32 1002, i32 %8)
%10 = getelementptr inbounds [1002 x i8], [1002 x i8]* %5, i64 0, i64 0
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %6
br label %37
15: ; preds = %6
store i32 0, i32* %3
store i32 0, i32* %2
br label %16
16: ; preds = %31, %15
%17 = load i32, i32* %2
%18 = getelementptr inbounds [1002 x i8], [1002 x i8]* %5, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
%21 = icmp slt i32 %17, %20
br i1 %21, label %22, label %34
22: ; preds = %16
%23 = load i32, i32* %2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [1002 x i8], [1002 x i8]* %5, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = sub nsw i32 %27, 48
%29 = load i32, i32* %3
%30 = add nsw i32 %29, %28
store i32 %30, i32* %3
br label %31
31: ; preds = %22
%32 = load i32, i32* %2
%33 = add nsw i32 %32, 1
store i32 %33, i32* %2
br label %16
34: ; preds = %16
%35 = load i32, i32* %3
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %6
37: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1002 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [1002 x i8], [1002 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %4, i32 1002, i32 %5)
%7 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %34, label %10
10: ; preds = %0, %26
%11 = call i32 @strlen(i8* %4)
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %26
13: ; preds = %10, %13
%14 = phi i64 [ %21, %13 ], [ 0, %10 ]
%15 = phi i32 [ %20, %13 ], [ 0, %10 ]
%16 = getelementptr inbounds [1002 x i8], [1002 x i8]* %2, i64 0, i64 %14
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = add i32 %15, -48
%20 = add i32 %19, %18
%21 = add nuw nsw i64 %14, 1
%22 = call i32 @strlen(i8* %4)
%23 = add nsw i32 %22, -1
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %21, %24
br i1 %25, label %13, label %26
26: ; preds = %13, %10
%27 = phi i32 [ 0, %10 ], [ %20, %13 ]
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @stdin
%30 = call i32 @fgets(i8* %4, i32 1002, i32 %29)
%31 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %1)
%32 = load i32, i32* %1
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %10
34: ; preds = %26, %0
ret i32 0
}