#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 */
char getchar();
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, short *);
int main(void) {
  short ch;
  printf("pls input an ASCII value(0-127): ");
  int ret1, ret2;
  while ((ret1 = scanf("%hd", &ch)) != 1 || ch > 127 || ch < 0 ||
         (ret2 = getchar()) != '\n') {
    if (ret1 != 1 || ret2 != '\n')
      while (getchar() != '\n')
        ;
    printf("input invalid!\n");
    printf("pls input an ASCII value(0-127): ");
  }
  printf("the character of %d is '%c'\n", ch, (char)ch);
  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    0xece(%rip),%rdi
lea    -0x6(%rbp),%rsi
call   1050 <scanf@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0x1,%al
cmp    $0x1,%ecx
mov    %al,-0x11(%rbp)
jne    11aa <main+0x7a>
movswl -0x6(%rbp),%ecx
mov    $0x1,%al
cmp    $0x7f,%ecx
mov    %al,-0x11(%rbp)
jg     11aa <main+0x7a>
movswl -0x6(%rbp),%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
jl     11aa <main+0x7a>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%eax
mov    %eax,-0x10(%rbp)
cmp    $0xa,%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11ba <main+0x8a>
jmp    1211 <main+0xe1>
cmpl   $0x1,-0xc(%rbp)
jne    11ce <main+0x9e>
cmpl   $0xa,-0x10(%rbp)
je     11f0 <main+0xc0>
jmp    11d3 <main+0xa3>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     11eb <main+0xbb>
jmp    11d3 <main+0xa3>
jmp    11f0 <main+0xc0>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    114d <main+0x1d>
movswl -0x6(%rbp),%esi
mov    -0x6(%rbp),%ax
movsbl %al,%edx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeae(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xebf(%rip),%r15
lea    0x6(%rsp),%r13
lea    0xed4(%rip),%r12
jmp    1192 <main+0x52>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <scanf@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    11c2 <main+0x82>
movzwl 0x6(%rsp),%eax
cmp    $0x7f,%ax
jg     11c2 <main+0x82>
test   %ax,%ax
js     11c2 <main+0x82>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     11dd <main+0x9d>
movsbl %al,%ebx
cmp    $0x1,%ebp
jne    11d0 <main+0x90>
cmp    $0xa,%ebx
je     1180 <main+0x40>
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
jne    11d0 <main+0x90>
jmp    1180 <main+0x40>
movswl 0x6(%rsp),%esi
movsbl %sil,%edx
lea    0xe39(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"pls input an ASCII value(0-127): \00"
@.str.1 = [4 x i8] c"%hd\00"
@.str.2 = [16 x i8] c"input invalid!\0A\00"
@.str.3 = [29 x i8] c"the character of %d is '%c'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i16
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %36, %0
%7 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %2)
store i32 %7, i32* %3
%8 = icmp ne i32 %7, 1
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = load i16, i16* %2
%11 = sext i16 %10 to i32
%12 = icmp sgt i32 %11, 127
br i1 %12, label %21, label %13
13: ; preds = %9
%14 = load i16, i16* %2
%15 = sext i16 %14 to i32
%16 = icmp slt i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = call signext i8 (...) @getchar()
%19 = sext i8 %18 to i32
store i32 %19, i32* %4
%20 = icmp ne i32 %19, 10
br label %21
21: ; preds = %17, %13, %9, %6
%22 = phi i1 [ true, %13 ], [ true, %9 ], [ true, %6 ], [ %20, %17 ]
br i1 %22, label %23, label %39
23: ; preds = %21
%24 = load i32, i32* %3
%25 = icmp ne i32 %24, 1
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 10
br i1 %28, label %29, label %36
29: ; preds = %26, %23
br label %30
30: ; preds = %34, %29
%31 = call signext i8 (...) @getchar()
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 10
br i1 %33, label %34, label %35
34: ; preds = %30
br label %30
35: ; preds = %30
br label %36
36: ; preds = %35, %26
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %6
39: ; preds = %21
%40 = load i16, i16* %2
%41 = sext i16 %40 to i32
%42 = load i16, i16* %2
%43 = trunc i16 %42 to i8
%44 = sext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [34 x i8] c"pls input an ASCII value(0-127): \00"
@.str.1 = [4 x i8] c"%hd\00"
@.str.3 = [29 x i8] c"the character of %d is '%c'\0A\00"
@str = [15 x i8] c"input invalid!\00"
define i32 @main() {
%1 = alloca i16
%2 = bitcast i16* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %4
4: ; preds = %24, %0
%5 = phi i32 [ undef, %0 ], [ %18, %24 ]
%6 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %1)
%7 = icmp ne i32 %6, 1
%8 = load i16, i16* %1
%9 = icmp sgt i16 %8, 127
%10 = select i1 %7, i1 true, i1 %9
%11 = icmp slt i16 %8, 0
%12 = select i1 %10, i1 true, i1 %11
br i1 %12, label %17, label %13
13: ; preds = %4
%14 = call signext i8 (...) @getchar()
%15 = sext i8 %14 to i32
%16 = icmp eq i8 %14, 10
br i1 %16, label %27, label %17
17: ; preds = %4, %13
%18 = phi i32 [ %5, %4 ], [ %15, %13 ]
%19 = icmp ne i32 %18, 10
%20 = select i1 %7, i1 true, i1 %19
br i1 %20, label %21, label %24
21: ; preds = %17, %21
%22 = call signext i8 (...) @getchar()
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %21
24: ; preds = %21, %17
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %4
27: ; preds = %13
%28 = load i16, i16* %1
%29 = sext i16 %28 to i32
%30 = zext i16 %28 to i32
%31 = shl i32 %30, 24
%32 = ashr exact i32 %31, 24
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %29, i32 %32)
ret i32 0
}