#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 INT_MAX;
char getchar();
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, int *);
int main(void) {
  int boundary;
  printf("pls input a integer: ");
  int ret1, ret2;
  while ((ret1 = scanf("%d", &boundary)) != 1 || (ret2 = getchar()) != '\n' ||
         (boundary < 0 || boundary > INT_MAX - 1)) {
    if (ret1 != 1 || ret2 != '\n')
      while (getchar() != '\n')
        ;
    printf("input invalid!\n");
    printf("pls input a integer: ");
  }
  int sum;
  sum = boundary * (1 + boundary) / 2;
  if (sum < 0) {
    printf("summary from 1 to %d is out of range.\n", boundary);
    return -2;
  }
  if (boundary > 2)
    printf("1 + ... + %d = %d\n", boundary, sum);
  else if (boundary == 2)
    printf("1 + 2 = %d\n", sum);
  else
    printf("answer: %d\n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec2(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x15(%rbp)
jne    11b7 <main+0x87>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%ecx
mov    %ecx,-0x10(%rbp)
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x15(%rbp)
jne    11b7 <main+0x87>
mov    $0x1,%al
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x16(%rbp)
jl     11b1 <main+0x81>
mov    -0x8(%rbp),%eax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setg   %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11c7 <main+0x97>
jmp    121e <main+0xee>
cmpl   $0x1,-0xc(%rbp)
jne    11db <main+0xab>
cmpl   $0xa,-0x10(%rbp)
je     11fd <main+0xcd>
jmp    11e0 <main+0xb0>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     11f8 <main+0xc8>
jmp    11e0 <main+0xb0>
jmp    11fd <main+0xcd>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    114d <main+0x1d>
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    $0x1,%ecx
imul   %ecx,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125c <main+0x12c>
mov    -0x8(%rbp),%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12bc <main+0x18c>
cmpl   $0x2,-0x8(%rbp)
jle    127f <main+0x14f>
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b5 <main+0x185>
cmpl   $0x2,-0x8(%rbp)
jne    129f <main+0x16f>
mov    -0x14(%rbp),%esi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b0 <main+0x180>
mov    -0x14(%rbp),%esi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b5 <main+0x185>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xeac(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb1(%rip),%r15
lea    0xc(%rsp),%r12
mov    0x2e77(%rip),%r13
lea    0xef3(%rip),%rbx
jmp    1192 <main+0x52>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    11c0 <main+0x80>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
jne    11c0 <main+0x80>
mov    0xc(%rsp),%esi
test   %esi,%esi
js     11c7 <main+0x87>
cmp    0x0(%r13),%esi
jge    11c7 <main+0x87>
jmp    11cd <main+0x8d>
nopl   (%rax)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
jne    11c0 <main+0x80>
jmp    1180 <main+0x40>
lea    0x1(%rsi),%eax
imul   %esi,%eax
cmp    $0xfffffffe,%eax
jg     11ed <main+0xad>
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xfffffffe,%ebx
jmp    122d <main+0xed>
mov    %eax,%edx
shr    $0x1f,%edx
add    %eax,%edx
sar    %edx
cmp    $0x3,%esi
jb     120d <main+0xcd>
lea    0xe3e(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    122d <main+0xed>
cmp    $0x2,%esi
jne    121b <main+0xdb>
lea    0xe3a(%rip),%rdi
jmp    1222 <main+0xe2>
lea    0xe3d(%rip),%rdi
xor    %ebx,%ebx
mov    %edx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"pls input a integer: \00"
@.str.1 = [3 x i8] c"%d\00"
@INT_MAX = global i32 0
@.str.2 = [16 x i8] c"input invalid!\0A\00"
@.str.3 = [39 x i8] c"summary from 1 to %d is out of range.\0A\00"
@.str.4 = [19 x i8] c"1 + ... + %d = %d\0A\00"
@.str.5 = [12 x i8] c"1 + 2 = %d\0A\00"
@.str.6 = [12 x i8] c"answer: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %39, %0
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
store i32 %8, i32* %3
%9 = icmp ne i32 %8, 1
br i1 %9, label %24, label %10
10: ; preds = %7
%11 = call signext i8 (...) @getchar()
%12 = sext i8 %11 to i32
store i32 %12, i32* %4
%13 = icmp ne i32 %12, 10
br i1 %13, label %24, label %14
14: ; preds = %10
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i32, i32* %2
%19 = load i32, i32* @INT_MAX
%20 = sub nsw i32 %19, 1
%21 = icmp sgt i32 %18, %20
br label %22
22: ; preds = %17, %14
%23 = phi i1 [ true, %14 ], [ %21, %17 ]
br label %24
24: ; preds = %22, %10, %7
%25 = phi i1 [ true, %10 ], [ true, %7 ], [ %23, %22 ]
br i1 %25, label %26, label %42
26: ; preds = %24
%27 = load i32, i32* %3
%28 = icmp ne i32 %27, 1
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, 10
br i1 %31, label %32, label %39
32: ; preds = %29, %26
br label %33
33: ; preds = %37, %32
%34 = call signext i8 (...) @getchar()
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 10
br i1 %36, label %37, label %38
37: ; preds = %33
br label %33
38: ; preds = %33
br label %39
39: ; preds = %38, %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %7
42: ; preds = %24
%43 = load i32, i32* %2
%44 = load i32, i32* %2
%45 = add nsw i32 1, %44
%46 = mul nsw i32 %43, %45
%47 = sdiv i32 %46, 2
store i32 %47, i32* %5
%48 = load i32, i32* %5
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = load i32, i32* %2
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %51)
store i32 -2, i32* %1
br label %71
53: ; preds = %42
%54 = load i32, i32* %2
%55 = icmp sgt i32 %54, 2
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i32, i32* %2
%58 = load i32, i32* %5
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %57, i32 %58)
br label %70
60: ; preds = %53
%61 = load i32, i32* %2
%62 = icmp eq i32 %61, 2
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* %5
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %64)
br label %69
66: ; preds = %60
%67 = load i32, i32* %5
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %67)
br label %69
69: ; preds = %66, %63
br label %70
70: ; preds = %69, %56
store i32 0, i32* %1
br label %71
71: ; preds = %70, %50
%72 = load i32, i32* %1
ret i32 %72
}

LLVM IR (-O2):
@.str = [22 x i8] c"pls input a integer: \00"
@.str.1 = [3 x i8] c"%d\00"
@INT_MAX = global i32 0
@.str.3 = [39 x i8] c"summary from 1 to %d is out of range.\0A\00"
@.str.4 = [19 x i8] c"1 + ... + %d = %d\0A\00"
@.str.5 = [12 x i8] c"1 + 2 = %d\0A\00"
@.str.6 = [12 x i8] c"answer: %d\0A\00"
@str = [15 x i8] c"input invalid!\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %4
4: ; preds = %22, %0
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %18
7: ; preds = %4
%8 = call signext i8 (...) @getchar()
%9 = icmp eq i8 %8, 10
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 0
%13 = load i32, i32* @INT_MAX
%14 = icmp sge i32 %11, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %25
16: ; preds = %10
%17 = icmp eq i8 %8, 10
br i1 %17, label %22, label %18
18: ; preds = %4, %16, %7
br label %19
19: ; preds = %18, %19
%20 = call signext i8 (...) @getchar()
%21 = icmp eq i8 %20, 10
br i1 %21, label %22, label %19
22: ; preds = %19, %16
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %4
25: ; preds = %10
%26 = add nuw nsw i32 %11, 1
%27 = mul nsw i32 %26, %11
%28 = sdiv i32 %27, 2
%29 = icmp slt i32 %27, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %11)
br label %42
32: ; preds = %25
%33 = icmp ugt i32 %11, 2
br i1 %33, label %34, label %36
34: ; preds = %32
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %11, i32 %28)
br label %42
36: ; preds = %32
%37 = icmp eq i32 %11, 2
br i1 %37, label %38, label %40
38: ; preds = %36
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %28)
br label %42
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %28)
br label %42
42: ; preds = %34, %40, %38, %30
%43 = phi i32 [ -2, %30 ], [ 0, %38 ], [ 0, %40 ], [ 0, %34 ]
ret i32 %43
}