#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, int);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ hexdigitValue(int, int *);
int /*<<< orphan*/ stderr;
bool read_hex(FILE *f, int n, int *v) {
  *v = 0;
  while (n-- > 0) {
    int dv, ch = fgetc(f);
    if (hexdigitValue(ch, &dv)) {
      fprintf(stderr, "Expected hexdigit at pos %ld, got %d", ftell(f) - 1, ch);
      return true;
    }
    *v = 16 * *v + dv;
  }
  return false;
}


x86 assembly (clang, -O0):
<read_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    11e4 <read_hex+0xa4>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1040 <hexdigitValue@plt>
cmp    $0x0,%rax
je     11cd <read_hex+0x8d>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
sub    $0x1,%rdx
mov    -0x28(%rbp),%ecx
lea    0xe44(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11eb <read_hex+0xab>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
shl    $0x4,%ecx
add    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    115d <read_hex+0x1d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rdx)
xor    %r14d,%r14d
test   %esi,%esi
jle    11cd <read_hex+0x8d>
mov    %rdx,%r13
mov    %esi,%ebx
mov    %rdi,%r15
add    $0x1,%ebx
lea    0x4(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <fgetc@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
call   1040 <hexdigitValue@plt>
test   %rax,%rax
jne    11a2 <read_hex+0x62>
mov    0x0(%r13),%eax
shl    $0x4,%eax
add    0x4(%rsp),%eax
mov    %eax,0x0(%r13)
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jg     1170 <read_hex+0x30>
jmp    11cd <read_hex+0x8d>
mov    0x2e37(%rip),%rax
mov    (%rax),%ebx
mov    %r15,%rdi
call   1060 <ftell@plt>
lea    -0x1(%rax),%rdx
lea    0xe42(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%ecx
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Expected hexdigit at pos %ld, got %d\00"
define i32 @read_hex(i32* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32*, i32** %7
store i32 0, i32* %10
br label %11
11: ; preds = %28, %3
%12 = load i32, i32* %6
%13 = add nsw i32 %12, -1
store i32 %13, i32* %6
%14 = icmp sgt i32 %12, 0
br i1 %14, label %15, label %35
15: ; preds = %11
%16 = load i32*, i32** %5
%17 = call i32 @fgetc(i32* %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = call i64 @hexdigitValue(i32 %18, i32* %8)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = load i32*, i32** %5
%24 = call i64 @ftell(i32* %23)
%25 = sub nsw i64 %24, 1
%26 = load i32, i32* %9
%27 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %25, i32 %26)
store i32 1, i32* %4
br label %36
28: ; preds = %15
%29 = load i32*, i32** %7
%30 = load i32, i32* %29
%31 = mul nsw i32 16, %30
%32 = load i32, i32* %8
%33 = add nsw i32 %31, %32
%34 = load i32*, i32** %7
store i32 %33, i32* %34
br label %11
35: ; preds = %11
store i32 0, i32* %4
br label %36
36: ; preds = %35, %21
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Expected hexdigit at pos %ld, got %d\00"
define i32 @read_hex(i32* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = alloca i32
store i32 0, i32* %2
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %25
6: ; preds = %3
%7 = bitcast i32* %4 to i8*
br label %8
8: ; preds = %6, %18
%9 = phi i32 [ %1, %6 ], [ %19, %18 ]
%10 = call i32 @fgetc(i32* %0)
%11 = call i64 @hexdigitValue(i32 %10, i32* %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = call i64 @ftell(i32* %0)
%16 = add nsw i64 %15, -1
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %16, i32 %10)
br label %25
18: ; preds = %8
%19 = add nsw i32 %9, -1
%20 = load i32, i32* %2
%21 = shl nsw i32 %20, 4
%22 = load i32, i32* %4
%23 = add nsw i32 %21, %22
store i32 %23, i32* %2
%24 = icmp sgt i32 %9, 1
br i1 %24, label %8, label %25
25: ; preds = %18, %3, %13
%26 = phi i32 [ 1, %13 ], [ 0, %3 ], [ 0, %18 ]
ret i32 %26
}