#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, ...);
int main() {
  int n, m, x, r, my_turn;
  printf("How many stones? ");
  scanf("%d", &n);
  printf("How many stones to pick up at once? ");
  scanf("%d", &m);
  if (n < 1 || m < 1)
    return EXIT_FAILURE;
  for (my_turn = 1; n != 0; my_turn ^= 1) {
    if (my_turn) {
      x = (n - 1) % (m + 1);
      if (x == 0)
        x = 1;
      printf("I'll take %d stones.\n", x);
    } else
      do {
        printf("How many stones will you take?");
        r = scanf("%d", &x);
        scanf("%*[^\n]");
      } while (r != 1 || x <= 0 || x > m || x > n);
    n -= x;
    printf("You have %d stones remaining.\n", n);
  }
  if (my_turn)
    printf("Heh, you lost... Loser.\n");
  else
    printf("Nooo, I lost. Looks like you won!\n");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xece(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
lea    0xebf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeae(%rip),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
cmpl   $0x1,-0x8(%rbp)
jl     1183 <main+0x63>
cmpl   $0x1,-0xc(%rbp)
jge    1194 <main+0x74>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ca <main+0x1aa>
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1293 <main+0x173>
cmpl   $0x0,-0x18(%rbp)
je     11e8 <main+0xc8>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    11d2 <main+0xb2>
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1269 <main+0x149>
jmp    11ed <main+0xcd>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe10(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    %eax,-0x14(%rbp)
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    $0x1,%al
cmpl   $0x1,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jne    1259 <main+0x139>
mov    $0x1,%al
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x19(%rbp)
jle    1259 <main+0x139>
mov    -0x10(%rbp),%ecx
mov    $0x1,%al
cmp    -0xc(%rbp),%ecx
mov    %al,-0x19(%rbp)
jg     1259 <main+0x139>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11ed <main+0xcd>
jmp    1269 <main+0x149>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
xor    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119b <main+0x7b>
cmpl   $0x0,-0x18(%rbp)
je     12b0 <main+0x190>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12be <main+0x19e>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xebb(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xebf(%rip),%rbx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <scanf@plt>
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x14(%rsp),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0xc(%rsp),%eax
mov    0x2e56(%rip),%rcx
test   %eax,%eax
jle    1281 <main+0x151>
cmpl   $0x0,0x14(%rsp)
jle    1281 <main+0x151>
lea    0xeac(%rip),%r14
lea    0xe67(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xeb8(%rip),%rbx
mov    $0x1,%r13d
jmp    1213 <main+0xe3>
nop
add    $0xffffffff,%eax
mov    0x14(%rsp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
test   %edx,%edx
mov    $0x1,%eax
cmove  %eax,%edx
mov    %edx,0x10(%rsp)
lea    0xe58(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rsp),%eax
mov    0xc(%rsp),%esi
sub    %eax,%esi
mov    %esi,0xc(%rsp)
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    $0x1,%r13d
mov    0xc(%rsp),%eax
test   %eax,%eax
je     1260 <main+0x130>
test   %r13d,%r13d
jne    11c0 <main+0x90>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <scanf@plt>
cmp    $0x1,%ebp
jne    1220 <main+0xf0>
mov    0x10(%rsp),%eax
test   %eax,%eax
jle    1220 <main+0xf0>
cmp    0x14(%rsp),%eax
jg     1220 <main+0xf0>
mov    0xc(%rsp),%esi
cmp    %esi,%eax
jg     1220 <main+0xf0>
jmp    11f3 <main+0xc3>
test   %r13d,%r13d
lea    0xe2b(%rip),%rax
lea    0xe46(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    0x2d57(%rip),%rcx
mov    (%rcx),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"How many stones? \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [37 x i8] c"How many stones to pick up at once? \00"
@EXIT_FAILURE = global i32 0
@.str.3 = [22 x i8] c"I'll take %d stones.\0A\00"
@.str.4 = [31 x i8] c"How many stones will you take?\00"
@.str.5 = [7 x i8] c"%*[^\0A]\00"
@.str.6 = [31 x i8] c"You have %d stones remaining.\0A\00"
@.str.7 = [25 x i8] c"Heh, you lost... Loser.\0A\00"
@.str.8 = [35 x i8] c"Nooo, I lost. Looks like you won!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %13, %0
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %1
br label %77
18: ; preds = %13
store i32 1, i32* %6
br label %19
19: ; preds = %65, %18
%20 = load i32, i32* %2
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %68
22: ; preds = %19
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %22
%26 = load i32, i32* %2
%27 = sub nsw i32 %26, 1
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
%30 = srem i32 %27, %29
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 1, i32* %4
br label %34
34: ; preds = %33, %25
%35 = load i32, i32* %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %59
37: ; preds = %22
br label %38
38: ; preds = %56, %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
store i32 %40, i32* %5
%41 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %38
%43 = load i32, i32* %5
%44 = icmp ne i32 %43, 1
br i1 %44, label %56, label %45
45: ; preds = %42
%46 = load i32, i32* %4
%47 = icmp sle i32 %46, 0
br i1 %47, label %56, label %48
48: ; preds = %45
%49 = load i32, i32* %4
%50 = load i32, i32* %3
%51 = icmp sgt i32 %49, %50
br i1 %51, label %56, label %52
52: ; preds = %48
%53 = load i32, i32* %4
%54 = load i32, i32* %2
%55 = icmp sgt i32 %53, %54
br label %56
56: ; preds = %52, %48, %45, %42
%57 = phi i1 [ true, %48 ], [ true, %45 ], [ true, %42 ], [ %55, %52 ]
br i1 %57, label %38, label %58
58: ; preds = %56
br label %59
59: ; preds = %58, %34
%60 = load i32, i32* %4
%61 = load i32, i32* %2
%62 = sub nsw i32 %61, %60
store i32 %62, i32* %2
%63 = load i32, i32* %2
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %59
%66 = load i32, i32* %6
%67 = xor i32 %66, 1
store i32 %67, i32* %6
br label %19
68: ; preds = %19
%69 = load i32, i32* %6
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
br label %75
73: ; preds = %68
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
br label %75
75: ; preds = %73, %71
%76 = load i32, i32* @EXIT_SUCCESS
store i32 %76, i32* %1
br label %77
77: ; preds = %75, %16
%78 = load i32, i32* %1
ret i32 %78
}

LLVM IR (-O2):
@.str = [18 x i8] c"How many stones? \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [37 x i8] c"How many stones to pick up at once? \00"
@EXIT_FAILURE = global i32 0
@.str.3 = [22 x i8] c"I'll take %d stones.\0A\00"
@.str.4 = [31 x i8] c"How many stones will you take?\00"
@.str.5 = [7 x i8] c"%*[^\0A]\00"
@.str.6 = [31 x i8] c"You have %d stones remaining.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [34 x i8] c"Nooo, I lost. Looks like you won!\00"
@str.9 = [24 x i8] c"Heh, you lost... Loser.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 1
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 1
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %56, label %16
16: ; preds = %0, %44
%17 = phi i32 [ %49, %44 ], [ 1, %0 ]
%18 = phi i32 [ %50, %44 ], [ %11, %0 ]
%19 = icmp eq i32 %17, 0
br i1 %19, label %30, label %20
20: ; preds = %16
%21 = add nsw i32 %18, -1
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
%24 = srem i32 %21, %23
%25 = icmp eq i32 %24, 0
%26 = select i1 %25, i32 1, i32 %24
store i32 %26, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %3
%29 = load i32, i32* %1
br label %44
30: ; preds = %16, %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%33 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%34 = icmp ne i32 %32, 1
%35 = load i32, i32* %3
%36 = icmp slt i32 %35, 1
%37 = select i1 %34, i1 true, i1 %36
%38 = load i32, i32* %2
%39 = icmp sgt i32 %35, %38
%40 = select i1 %37, i1 true, i1 %39
%41 = load i32, i32* %1
%42 = icmp sgt i32 %35, %41
%43 = select i1 %40, i1 true, i1 %42
br i1 %43, label %30, label %44
44: ; preds = %30, %20
%45 = phi i32 [ %29, %20 ], [ %41, %30 ]
%46 = phi i32 [ %28, %20 ], [ %35, %30 ]
%47 = sub nsw i32 %45, %46
store i32 %47, i32* %1
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %47)
%49 = xor i32 %17, 1
%50 = load i32, i32* %1
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %16
52: ; preds = %44
%53 = icmp eq i32 %49, 0
%54 = select i1 %53, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @str.9, i64 0, i64 0)
%55 = call i32 @puts(i8* dereferenceable(1) %54)
br label %56
56: ; preds = %52, %0
%57 = phi i32* [ @EXIT_FAILURE, %0 ], [ @EXIT_SUCCESS, %52 ]
%58 = load i32, i32* %57
ret i32 %58
}