#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 fgetc(int /*<<< orphan*/);
scalar_t__ hexdigit(char);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ stdin;
size_t strtoul(char *, int /*<<< orphan*/ *, int);
int readnum(size_t *n) {
  char buf[9];
  int i = 0;
  char ch;
  memset(buf, 0, 9);
  while (0x20 == (ch = fgetc(stdin)))
    printf("%c", ch);
  while (hexdigit(ch) && (i <= 8)) {
    printf("%c", ch);
    buf[i++] = ch;
    ch = fgetc(stdin);
  }
  if ((0x20 != ch) && (0x0a != ch) && (0x0d != ch)) {
    printf("%x\n", ch);
    return -1;
  }
  printf("%c", ch);
  *n = strtoul(buf, NULL, 0x10);
  return 0;
}


x86 assembly (clang, -O0):
<readnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x19(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1040 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
jne    11a9 <readnum+0x59>
lea    0xe65(%rip),%rdi
movsbl -0x21(%rbp),%esi
call   1030 <printf@plt>
jmp    1173 <readnum+0x23>
jmp    11ae <readnum+0x5e>
movsbl -0x21(%rbp),%edi
call   1060 <hexdigit@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x22(%rbp)
je     11d3 <readnum+0x83>
cmpl   $0x8,-0x20(%rbp)
setle  %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11e3 <readnum+0x93>
jmp    121d <readnum+0xcd>
lea    0xe16(%rip),%rdi
movsbl -0x21(%rbp),%esi
call   1030 <printf@plt>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x20(%rbp)
cltq
mov    %cl,-0x19(%rbp,%rax,1)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %al,-0x21(%rbp)
jmp    11ae <readnum+0x5e>
movsbl -0x21(%rbp),%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
je     126c <readnum+0x11c>
movsbl -0x21(%rbp),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
je     126c <readnum+0x11c>
movsbl -0x21(%rbp),%ecx
mov    $0xd,%eax
cmp    %ecx,%eax
je     126c <readnum+0x11c>
lea    0xdac(%rip),%rdi
movsbl -0x21(%rbp),%esi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <readnum+0x14f>
lea    0xd8d(%rip),%rdi
movsbl -0x21(%rbp),%esi
call   1030 <printf@plt>
lea    -0x19(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readnum>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x7(%rsp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1040 <memset@plt>
mov    0x2e70(%rip),%r12
mov    (%r12),%edi
call   1050 <fgetc@plt>
mov    %eax,%ebx
cmp    $0x20,%al
jne    11ac <readnum+0x5c>
lea    0xe72(%rip),%r15
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0x20,%esi
call   1030 <printf@plt>
mov    (%r12),%edi
call   1050 <fgetc@plt>
cmp    $0x20,%al
je     1190 <readnum+0x40>
mov    %eax,%ebx
movsbl %bl,%edi
call   1060 <hexdigit@plt>
test   %rax,%rax
je     1207 <readnum+0xb7>
xor    %r13d,%r13d
lea    0xe3d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %bl,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %bl,0x7(%rsp,%r13,1)
mov    (%r12),%edi
call   1050 <fgetc@plt>
mov    %eax,%ebx
movsbl %al,%edi
call   1060 <hexdigit@plt>
test   %rax,%rax
je     1207 <readnum+0xb7>
lea    0x1(%r13),%rax
cmp    $0x8,%r13
mov    %rax,%r13
jb     11d0 <readnum+0x80>
cmp    $0x20,%bl
ja     1246 <readnum+0xf6>
movzbl %bl,%eax
movabs $0x100002400,%rcx
bt     %rax,%rcx
jae    1246 <readnum+0xf6>
movsbl %bl,%esi
lea    0xdd7(%rip),%rdi
call   1030 <printf@plt>
xor    %ebx,%ebx
lea    0x7(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,(%r14)
jmp    125a <readnum+0x10a>
movsbl %bl,%esi
lea    0xdb3(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [3 x i8] c"%c\00"
@.str.1 = [4 x i8] c"%x\0A\00"
define i32 @readnum(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca [9 x i8]
%5 = alloca i32
%6 = alloca i8
store i64* %0, i64** %3
store i32 0, i32* %5
%7 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 9)
br label %9
9: ; preds = %14, %1
%10 = load i32, i32* @stdin
%11 = call signext i8 @fgetc(i32 %10)
store i8 %11, i8* %6
%12 = sext i8 %11 to i32
%13 = icmp eq i32 32, %12
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i8, i8* %6
%16 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %15)
br label %9
17: ; preds = %9
br label %18
18: ; preds = %27, %17
%19 = load i8, i8* %6
%20 = call i64 @hexdigit(i8 signext %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %5
%24 = icmp sle i32 %23, 8
br label %25
25: ; preds = %22, %18
%26 = phi i1 [ false, %18 ], [ %24, %22 ]
br i1 %26, label %27, label %37
27: ; preds = %25
%28 = load i8, i8* %6
%29 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %28)
%30 = load i8, i8* %6
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 %33
store i8 %30, i8* %34
%35 = load i32, i32* @stdin
%36 = call signext i8 @fgetc(i32 %35)
store i8 %36, i8* %6
br label %18
37: ; preds = %25
%38 = load i8, i8* %6
%39 = sext i8 %38 to i32
%40 = icmp ne i32 32, %39
br i1 %40, label %41, label %52
41: ; preds = %37
%42 = load i8, i8* %6
%43 = sext i8 %42 to i32
%44 = icmp ne i32 10, %43
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = load i8, i8* %6
%47 = sext i8 %46 to i32
%48 = icmp ne i32 13, %47
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i8, i8* %6
%51 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8 signext %50)
store i32 -1, i32* %2
br label %58
52: ; preds = %45, %41, %37
%53 = load i8, i8* %6
%54 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %53)
%55 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%56 = call i64 @strtoul(i8* %55, i32* null, i32 16)
%57 = load i64*, i64** %3
store i64 %56, i64* %57
store i32 0, i32* %2
br label %58
58: ; preds = %52, %49
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [3 x i8] c"%c\00"
@.str.1 = [4 x i8] c"%x\0A\00"
define i32 @readnum(i64* nocapture writeonly %0) {
%2 = alloca [9 x i8]
%3 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 9)
%5 = load i32, i32* @stdin
%6 = call signext i8 @fgetc(i32 %5)
%7 = icmp eq i8 %6, 32
br i1 %7, label %12, label %8
8: ; preds = %12, %1
%9 = phi i8 [ %6, %1 ], [ %15, %12 ]
%10 = call i64 @hexdigit(i8 signext %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %29, label %17
12: ; preds = %1, %12
%13 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext 32)
%14 = load i32, i32* @stdin
%15 = call signext i8 @fgetc(i32 %14)
%16 = icmp eq i8 %15, 32
br i1 %16, label %12, label %8
17: ; preds = %8, %17
%18 = phi i64 [ %21, %17 ], [ 0, %8 ]
%19 = phi i8 [ %24, %17 ], [ %9, %8 ]
%20 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %19)
%21 = add nuw nsw i64 %18, 1
%22 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 %18
store i8 %19, i8* %22
%23 = load i32, i32* @stdin
%24 = call signext i8 @fgetc(i32 %23)
%25 = call i64 @hexdigit(i8 signext %24)
%26 = icmp ne i64 %25, 0
%27 = icmp ult i64 %18, 8
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %17, label %29
29: ; preds = %17, %8
%30 = phi i8 [ %9, %8 ], [ %24, %17 ]
switch i8 %30, label %31 [
i8 32, label %33
i8 13, label %33
i8 10, label %33
]
31: ; preds = %29
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8 signext %30)
br label %36
33: ; preds = %29, %29, %29
%34 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %30)
%35 = call i64 @strtoul(i8* nocapture %3, i32* null, i32 16)
store i64 %35, i64* %0
br label %36
36: ; preds = %33, %31
%37 = phi i32 [ -1, %31 ], [ 0, %33 ]
ret i32 %37
}