#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*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  const int coins[] = {500, 100, 50, 10, 5, 1, 0};
  int paid, i, j;
  while (1) {
    int v = 1000;
    scanf("%d", &paid);
    if (paid == 0)
      break;
    v -= paid;
    for (i = j = 0; v > 0; i++) {
      while (v < coins[j])
        j++;
      v -= coins[j];
    }
    printf("%d\n", i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeca(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xec7(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xec4(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xec2(%rip),%eax
mov    %eax,-0x8(%rbp)
movl   $0x3e8,-0x30(%rbp)
lea    0xeb5(%rip),%rdi
lea    -0x24(%rbp),%rsi
call   1040 <scanf@plt>
cmpl   $0x0,-0x24(%rbp)
jne    117f <main+0x5f>
jmp    11f8 <main+0xd8>
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    11e4 <main+0xc4>
jmp    11a7 <main+0x87>
mov    -0x30(%rbp),%eax
movslq -0x2c(%rbp),%rcx
cmp    -0x20(%rbp,%rcx,4),%eax
jge    11c6 <main+0xa6>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a7 <main+0x87>
movslq -0x2c(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1198 <main+0x78>
mov    -0x28(%rbp),%esi
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
jmp    1159 <main+0x39>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeed(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
je     11b7 <main+0x97>
lea    0xeb8(%rip),%rbx
lea    0xed0(%rip),%r14
lea    0xec6(%rip),%r15
lea    0x4(%rsp),%r12
jmp    117b <main+0x5b>
nopl   (%rax)
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <scanf@plt>
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
je     11b7 <main+0x97>
xor    %esi,%esi
cmp    $0x3e7,%ecx
jg     1160 <main+0x40>
mov    $0x3e8,%eax
sub    %ecx,%eax
xor    %ecx,%ecx
xor    %esi,%esi
movslq %ecx,%rcx
lea    (%rbx,%rcx,4),%rdx
add    $0xffffffffffffffff,%rcx
nopl   0x0(%rax,%rax,1)
mov    (%rdx),%edi
add    $0x1,%rcx
add    $0x4,%rdx
cmp    %edi,%eax
jl     11a0 <main+0x80>
add    $0x1,%esi
sub    %edi,%eax
jg     1190 <main+0x70>
jmp    1160 <main+0x40>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.coins = [7 x i32] [i32 500, i32 100, i32 50, i32 10, i32 5, i32 1, i32 0]
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [7 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [7 x i32]* %2 to i8*
br label %8
8: ; preds = %0, %41
store i32 1000, i32* %6
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
br label %44
13: ; preds = %8
%14 = load i32, i32* %3
%15 = load i32, i32* %6
%16 = sub nsw i32 %15, %14
store i32 %16, i32* %6
store i32 0, i32* %5
store i32 0, i32* %4
br label %17
17: ; preds = %38, %13
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %41
20: ; preds = %17
br label %21
21: ; preds = %28, %20
%22 = load i32, i32* %6
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [7 x i32], [7 x i32]* %2, i64 0, i64 %24
%26 = load i32, i32* %25
%27 = icmp slt i32 %22, %26
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %21
31: ; preds = %21
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [7 x i32], [7 x i32]* %2, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %6
br label %38
38: ; preds = %31
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %17
41: ; preds = %17
%42 = load i32, i32* %4
%43 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %42)
br label %8
44: ; preds = %12
ret i32 0
}

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