#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*/ background();
int console_read(char *, int);
void console_readnum(int *num, char *ch) {
  int total = 0;
  for (;;) {
    total = (total * 10) + (*ch - '0');
    while (console_read(ch, 1) != 1) {
      background();
    }
    if (!((*ch >= '0') && (*ch <= '9')))
      break;
  }
  *num = total;
}


x86 assembly (clang, -O0):
<console_readnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
imul   $0xa,-0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
sub    $0x30,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1040 <console_read@plt>
cmp    $0x1,%eax
je     116d <console_readnum+0x4d>
mov    $0x0,%al
call   1030 <background@plt>
jmp    114a <console_readnum+0x2a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     118d <console_readnum+0x6d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    1192 <console_readnum+0x72>
jmp    1197 <console_readnum+0x77>
jmp    1137 <console_readnum+0x17>
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_readnum>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%r15b
xor    %r12d,%r12d
jmp    1147 <console_readnum+0x27>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <background@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <console_read@plt>
cmp    $0x1,%eax
jne    1140 <console_readnum+0x20>
lea    (%r12,%r12,4),%eax
movsbl %r15b,%ecx
lea    (%rcx,%rax,2),%r12d
add    $0xffffffd0,%r12d
movzbl (%rbx),%r15d
lea    -0x30(%r15),%eax
cmp    $0xa,%al
jb     1147 <console_readnum+0x27>
mov    %r12d,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @console_readnum(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %6
6: ; preds = %31, %2
%7 = load i32, i32* %5
%8 = mul nsw i32 %7, 10
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = sub nsw i32 %11, 48
%13 = add nsw i32 %8, %12
store i32 %13, i32* %5
br label %14
14: ; preds = %18, %6
%15 = load i8*, i8** %4
%16 = call i32 @console_read(i8* %15, i32 1)
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (...) @background()
br label %14
20: ; preds = %14
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp sge i32 %23, 48
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp sle i32 %28, 57
br i1 %29, label %31, label %30
30: ; preds = %25, %20
br label %32
31: ; preds = %25
br label %6
32: ; preds = %30
%33 = load i32, i32* %5
%34 = load i32*, i32** %3
store i32 %33, i32* %34
ret void
}

LLVM IR (-O2):
define void @console_readnum(i32* nocapture writeonly %0, i8* %1) {
%3 = load i8, i8* %1
br label %4
4: ; preds = %17, %2
%5 = phi i8 [ %3, %2 ], [ %18, %17 ]
%6 = phi i32 [ 0, %2 ], [ %10, %17 ]
%7 = mul nsw i32 %6, 10
%8 = sext i8 %5 to i32
%9 = add i32 %7, -48
%10 = add i32 %9, %8
%11 = tail call i32 @console_read(i8* %1, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %17, label %13
13: ; preds = %4, %13
%14 = tail call i32 (...) @background()
%15 = tail call i32 @console_read(i8* %1, i32 1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %13
17: ; preds = %13, %4
%18 = load i8, i8* %1
%19 = add i8 %18, -48
%20 = icmp ult i8 %19, 10
br i1 %20, label %4, label %21
21: ; preds = %17
store i32 %10, i32* %0
ret void
}