#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 */
typedef scalar_t__ uint8_t;
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fgets(char *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ fseek(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ isxdigit(char);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sscanf(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
int intoi(char *buffer, uint8_t max, uint8_t hex, uint32_t *num) {
  uint8_t len;
  fgets(buffer, max + 1, stdin);
  len = strlen(buffer);
  if (hex != 0) {
    int i = 0;
    for (i = 0; i < len - 1; i++) {
      if (!isxdigit(buffer[i])) {
        printf("Enter a valid hex digit\n");
        fseek(stdin, 0, SEEK_SET);
        return -1;
      }
    }
  }
  sscanf(buffer, " %X", num);
}


x86 assembly (clang, -O0):
<intoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1230 <intoi+0xd0>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jge    122b <intoi+0xcb>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1080 <isxdigit@plt>
cmp    $0x0,%eax
jne    1218 <intoi+0xb8>
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <intoi+0xe4>
jmp    121d <intoi+0xbd>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11b8 <intoi+0x58>
jmp    1230 <intoi+0xd0>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdda(%rip),%rsi
call   1060 <sscanf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intoi>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rdi,%r15
add    $0x1,%rsi
mov    0x2e6b(%rip),%r12
mov    (%r12),%edx
call   1050 <fgets@plt>
test   %rbx,%rbx
je     11c7 <intoi+0x67>
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%rax
jl     11c7 <intoi+0x67>
mov    %rax,%r13
add    $0xffffffffffffffff,%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r15,%rbx,1),%edi
call   1080 <isxdigit@plt>
test   %eax,%eax
je     11e8 <intoi+0x88>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11b0 <intoi+0x50>
lea    0xe32(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1060 <sscanf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    (%r12),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
jmp    11d9 <intoi+0x79>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [25 x i8] c"Enter a valid hex digit\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c" %X\00"
define i32 @intoi(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = add nsw i64 %13, 1
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %12, i64 %14, i32 %15)
%17 = load i8*, i8** %6
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %10
%19 = load i64, i64* %8
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %46
21: ; preds = %4
store i32 0, i32* %11
store i32 0, i32* %11
br label %22
22: ; preds = %42, %21
%23 = load i32, i32* %11
%24 = sext i32 %23 to i64
%25 = load i64, i64* %10
%26 = sub nsw i64 %25, 1
%27 = icmp slt i64 %24, %26
br i1 %27, label %28, label %45
28: ; preds = %22
%29 = load i8*, i8** %6
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = call i32 @isxdigit(i8 signext %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %28
%37 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* @stdin
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @fseek(i32 %38, i32 0, i32 %39)
store i32 -1, i32* %5
br label %50
41: ; preds = %28
br label %42
42: ; preds = %41
%43 = load i32, i32* %11
%44 = add nsw i32 %43, 1
store i32 %44, i32* %11
br label %22
45: ; preds = %22
br label %46
46: ; preds = %45, %4
%47 = load i8*, i8** %6
%48 = load i32*, i32** %9
%49 = call i32 @sscanf(i8* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %48)
br label %50
50: ; preds = %46, %36
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@stdin = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c" %X\00"
@str = [24 x i8] c"Enter a valid hex digit\00"
define i32 @intoi(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = add nsw i64 %1, 1
%6 = load i32, i32* @stdin
%7 = tail call i32 @fgets(i8* %0, i64 %5, i32 %6)
%8 = icmp eq i64 %2, 0
br i1 %8, label %27, label %9
9: ; preds = %4
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = add i64 %10, -1
%12 = icmp sgt i64 %10, 1
br i1 %12, label %15, label %27
13: ; preds = %15
%14 = icmp eq i64 %21, %11
br i1 %14, label %27, label %15
15: ; preds = %9, %13
%16 = phi i64 [ %21, %13 ], [ 0, %9 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = tail call i32 @isxdigit(i8 signext %18)
%20 = icmp eq i32 %19, 0
%21 = add nuw nsw i64 %16, 1
br i1 %20, label %22, label %13
22: ; preds = %15
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @stdin
%25 = load i32, i32* @SEEK_SET
%26 = tail call i32 @fseek(i32 %24, i32 0, i32 %25)
br label %29
27: ; preds = %13, %9, %4
%28 = tail call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %3)
br label %29
29: ; preds = %22, %27
ret i32 -1
}