#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*/ printf(char *, ...);
int sscanf(char const *, char *, unsigned char *);
int scanhex(const char *name, unsigned n, const char *str, unsigned char *b) {
  int i;
  for (i = 0; i < n; i++)
    if (sscanf(str + (i << 1), "%2hhx", b + i) != 1)
      return 1;
  printf("%6s: ", name);
  for (i = 0; i < n; i++)
    printf("%02x", b[i]);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<scanhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1198 <scanhex+0x78>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
movslq -0x2c(%rbp),%rax
add    %rax,%rdx
lea    0xe95(%rip),%rsi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
je     1185 <scanhex+0x65>
movl   $0x1,-0x4(%rbp)
jmp    11fa <scanhex+0xda>
jmp    118a <scanhex+0x6a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    113e <scanhex+0x1e>
mov    -0x10(%rbp),%rsi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11e5 <scanhex+0xc5>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b1 <scanhex+0x91>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanhex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rax
test   %esi,%esi
je     11d9 <scanhex+0xa9>
mov    %rdx,%rbp
mov    %rax,0x8(%rsp)
mov    %esi,0x4(%rsp)
mov    %esi,%r14d
mov    %rcx,%rbx
lea    (%r14,%r14,1),%r15
xor    %r12d,%r12d
lea    0xe97(%rip),%r13
mov    %rcx,0x10(%rsp)
xchg   %ax,%ax
movslq %r12d,%rdi
add    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11f8 <scanhex+0xc8>
add    $0x2,%r12
add    $0x1,%rbx
cmp    %r12,%r15
jne    1170 <scanhex+0x40>
lea    0xe6c(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,0x4(%rsp)
mov    0x10(%rsp),%rbx
je     11ea <scanhex+0xba>
lea    0xe53(%rip),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl (%rbx,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    11c0 <scanhex+0x90>
jmp    11ea <scanhex+0xba>
lea    0xe26(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    11fd <scanhex+0xcd>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%2hhx\00"
@.str.1 = [6 x i8] c"%6s: \00"
@.str.2 = [5 x i8] c"%02x\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @scanhex(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 0, i32* %10
br label %11
11: ; preds = %29, %4
%12 = load i32, i32* %10
%13 = load i32, i32* %7
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %32
15: ; preds = %11
%16 = load i8*, i8** %8
%17 = load i32, i32* %10
%18 = shl i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i8*, i8** %9
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %15
store i32 1, i32* %5
br label %52
28: ; preds = %15
br label %29
29: ; preds = %28
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
br label %11
32: ; preds = %11
%33 = load i8*, i8** %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %33)
store i32 0, i32* %10
br label %35
35: ; preds = %47, %32
%36 = load i32, i32* %10
%37 = load i32, i32* %7
%38 = icmp ult i32 %36, %37
br i1 %38, label %39, label %50
39: ; preds = %35
%40 = load i8*, i8** %9
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %39
%48 = load i32, i32* %10
%49 = add nsw i32 %48, 1
store i32 %49, i32* %10
br label %35
50: ; preds = %35
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %5
br label %52
52: ; preds = %50, %27
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@.str = [6 x i8] c"%2hhx\00"
@.str.1 = [6 x i8] c"%6s: \00"
@.str.2 = [5 x i8] c"%02x\00"
define i32 @scanhex(i8* %0, i32 %1, i8* nocapture %2, i8* %3) {
%5 = icmp eq i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %34
8: ; preds = %4
%9 = zext i32 %1 to i64
br label %12
10: ; preds = %12
%11 = icmp eq i64 %21, %9
br i1 %11, label %22, label %12
12: ; preds = %8, %10
%13 = phi i64 [ 0, %8 ], [ %21, %10 ]
%14 = trunc i64 %13 to i32
%15 = shl i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %2, i64 %16
%18 = getelementptr inbounds i8, i8* %3, i64 %13
%19 = tail call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = icmp eq i32 %19, 1
%21 = add nuw nsw i64 %13, 1
br i1 %20, label %10, label %36
22: ; preds = %10
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br i1 %5, label %34, label %24
24: ; preds = %22
%25 = zext i32 %1 to i64
br label %26
26: ; preds = %24, %26
%27 = phi i64 [ 0, %24 ], [ %32, %26 ]
%28 = getelementptr inbounds i8, i8* %3, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = add nuw nsw i64 %27, 1
%33 = icmp eq i64 %32, %25
br i1 %33, label %34, label %26
34: ; preds = %26, %6, %22
%35 = tail call i32 @putchar(i32 10)
br label %36
36: ; preds = %12, %34
%37 = phi i32 [ 0, %34 ], [ 1, %12 ]
ret i32 %37
}