#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 */
scalar_t__ count(int, int);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int scanf(char *, int *, int *, int *);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  int a, b, k, t;
  freopen("input.txt", "r", stdin);
  while (scanf("%d%d%d", &a, &b, &k), a) {
    if (count(a, k) == count(b, k))
      t = -1;
    else
      t = a + b;
    printf("%d\n", t);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
lea    0xe9a(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1050 <freopen@plt>
lea    0xe93(%rip),%rdi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1060 <scanf@plt>
cmpl   $0x0,-0x14(%rbp)
je     11e7 <main+0xa7>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <count@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <count@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jne    11ca <main+0x8a>
movl   $0xffffffff,-0x20(%rbp)
jmp    11d3 <main+0x93>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
jmp    1172 <main+0x32>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
lea    0xea2(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1050 <freopen@plt>
lea    0xe9b(%rip),%rdi
lea    0xc(%rsp),%rsi
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
call   1060 <scanf@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
je     11fd <main+0xbd>
lea    0xe7f(%rip),%r14
lea    0xe71(%rip),%r15
lea    0xc(%rsp),%r12
lea    0x14(%rsp),%r13
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    0x10(%rsp),%esi
call   1030 <count@plt>
mov    %rax,%rbp
mov    0x14(%rsp),%edi
mov    0x10(%rsp),%esi
call   1030 <count@plt>
mov    0x14(%rsp),%esi
add    0xc(%rsp),%esi
cmp    %rax,%rbp
mov    $0xffffffff,%eax
cmove  %eax,%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1060 <scanf@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
jne    11b0 <main+0x70>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"input.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [7 x i8] c"%d%d%d\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @stdin
%11 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %29, %2
%13 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %6, i32* %7, i32* %8)
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %12
%17 = load i32, i32* %6
%18 = load i32, i32* %8
%19 = call i64 @count(i32 %17, i32 %18)
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = call i64 @count(i32 %20, i32 %21)
%23 = icmp eq i64 %19, %22
br i1 %23, label %24, label %25
24: ; preds = %16
store i32 -1, i32* %9
br label %29
25: ; preds = %16
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = add nsw i32 %26, %27
store i32 %28, i32* %9
br label %29
29: ; preds = %25, %24
%30 = load i32, i32* %9
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %12
32: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"input.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [7 x i8] c"%d%d%d\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i32, i32* @stdin
%10 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %30, label %14
14: ; preds = %2, %14
%15 = phi i32 [ %28, %14 ], [ %12, %2 ]
%16 = load i32, i32* %5
%17 = call i64 @count(i32 %15, i32 %16)
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = call i64 @count(i32 %18, i32 %19)
%21 = icmp eq i64 %17, %20
%22 = load i32, i32* %3
%23 = load i32, i32* %4
%24 = add nsw i32 %23, %22
%25 = select i1 %21, i32 -1, i32 %24
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%28 = load i32, i32* %3
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %14
30: ; preds = %14, %2
ret i32 0
}