#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 pow(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int strlen(char *);
char valid_hex(char *);
int main() {
  char str[10] = {0};
  char valid;
  int tmp, i;
  int num;
  printf("Enter str.(hex)");
  scanf("%s", str);
  printf("str : %s\n", str);
  valid = valid_hex(str);
  if (!valid) {
    printf("Invalid input");
    goto out;
  }
  tmp = strlen(str) - 1;
  num = 0;
  for (i = 0; tmp >= 0; i++, tmp--) {
    if (str[tmp] >= '0' && str[tmp] <= '9') {
      num += ((str[tmp] - '0') * pow(16, i));
    } else if (str[tmp] >= 'A' && str[tmp] <= 'F') {
      num += ((str[tmp] - 55) * pow(16, i));
    } else if (str[tmp] >= 'a' && str[tmp] <= 'f') {
      num += ((str[tmp] - 87) * pow(16, i));
    }
  }
  printf("num: %d\n", num);
out:
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xe(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xe(%rbp),%rsi
lea    0xe78(%rip),%rdi
call   1070 <scanf@plt>
lea    -0xe(%rbp),%rsi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xe(%rbp),%rdi
call   1060 <valid_hex@plt>
mov    %al,-0xf(%rbp)
cmpb   $0x0,-0xf(%rbp)
jne    11d8 <main+0x78>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1333 <main+0x1d3>
lea    -0xe(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1322 <main+0x1c2>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
cmp    $0x30,%eax
jl     1255 <main+0xf5>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
cmp    $0x39,%eax
jg     1255 <main+0xf5>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
sub    $0x30,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x10,%edi
call   1080 <pow@plt>
mov    %al,%cl
mov    -0x20(%rbp),%eax
movsbl %cl,%ecx
imul   %ecx,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1306 <main+0x1a6>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
cmp    $0x41,%eax
jl     12ab <main+0x14b>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
cmp    $0x46,%eax
jg     12ab <main+0x14b>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
sub    $0x37,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x10,%edi
call   1080 <pow@plt>
mov    %al,%cl
mov    -0x24(%rbp),%eax
movsbl %cl,%ecx
imul   %ecx,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1301 <main+0x1a1>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
cmp    $0x61,%eax
jl     12fc <main+0x19c>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
cmp    $0x66,%eax
jg     12fc <main+0x19c>
movslq -0x14(%rbp),%rax
movsbl -0xe(%rbp,%rax,1),%eax
sub    $0x57,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x10,%edi
call   1080 <pow@plt>
mov    %al,%cl
mov    -0x28(%rbp),%eax
movsbl %cl,%ecx
imul   %ecx,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1301 <main+0x1a1>
jmp    1306 <main+0x1a6>
jmp    130b <main+0x1ab>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11f5 <main+0x95>
mov    -0x1c(%rbp),%esi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movw   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe8f(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe8e(%rip),%rdi
lea    0x8(%rsp),%rbp
mov    %rbp,%rsi
call   1060 <scanf@plt>
lea    0xe7d(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
call   1050 <valid_hex@plt>
test   %al,%al
je     1238 <main+0xe8>
lea    0x8(%rsp),%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1225 <main+0xd5>
mov    %eax,%ebx
add    $0xffffffffffffffff,%rbx
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    11f5 <main+0xa5>
nopl   0x0(%rax)
add    %eax,%ebp
mov    $0x10,%edi
mov    %r15d,%esi
call   1070 <pow@plt>
movsbl %al,%eax
imul   %ebp,%eax
add    %eax,%r14d
add    $0x1,%r15d
add    $0xffffffffffffffff,%rbx
cmp    $0xffffffff,%ebx
je     1225 <main+0xd5>
mov    %ebx,%eax
movsbl 0x8(%rsp,%rax,1),%eax
lea    -0x30(%rax),%ecx
mov    $0xffffffd0,%ebp
cmp    $0xa,%cl
jb     11d0 <main+0x80>
lea    -0x41(%rax),%ecx
mov    $0xffffffc9,%ebp
cmp    $0x6,%cl
jb     11d0 <main+0x80>
lea    -0x61(%rax),%ecx
mov    $0xffffffa9,%ebp
cmp    $0x5,%cl
jbe    11d0 <main+0x80>
jmp    11e8 <main+0x98>
lea    0xdff(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1246 <main+0xf6>
lea    0xdde(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Enter str.(hex)\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [10 x i8] c"str : %s\0A\00"
@.str.3 = [14 x i8] c"Invalid input\00"
@.str.4 = [9 x i8] c"num: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [10 x i8]* %2 to i8*
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%14 = call signext i8 @valid_hex(i8* %13)
store i8 %14, i8* %3
%15 = load i8, i8* %3
%16 = icmp ne i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %118
19: ; preds = %0
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = sub nsw i32 %21, 1
store i32 %22, i32* %4
store i32 0, i32* %6
store i32 0, i32* %5
br label %23
23: ; preds = %110, %19
%24 = load i32, i32* %4
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %115
26: ; preds = %23
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp sge i32 %31, 48
br i1 %32, label %33, label %53
33: ; preds = %26
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp sle i32 %38, 57
br i1 %39, label %40, label %53
40: ; preds = %33
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = sub nsw i32 %45, 48
%47 = load i32, i32* %5
%48 = call signext i8 @pow(i32 16, i32 %47)
%49 = sext i8 %48 to i32
%50 = mul nsw i32 %46, %49
%51 = load i32, i32* %6
%52 = add nsw i32 %51, %50
store i32 %52, i32* %6
br label %109
53: ; preds = %33, %26
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp sge i32 %58, 65
br i1 %59, label %60, label %80
60: ; preds = %53
%61 = load i32, i32* %4
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %62
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp sle i32 %65, 70
br i1 %66, label %67, label %80
67: ; preds = %60
%68 = load i32, i32* %4
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %69
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = sub nsw i32 %72, 55
%74 = load i32, i32* %5
%75 = call signext i8 @pow(i32 16, i32 %74)
%76 = sext i8 %75 to i32
%77 = mul nsw i32 %73, %76
%78 = load i32, i32* %6
%79 = add nsw i32 %78, %77
store i32 %79, i32* %6
br label %108
80: ; preds = %60, %53
%81 = load i32, i32* %4
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %82
%84 = load i8, i8* %83
%85 = sext i8 %84 to i32
%86 = icmp sge i32 %85, 97
br i1 %86, label %87, label %107
87: ; preds = %80
%88 = load i32, i32* %4
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %89
%91 = load i8, i8* %90
%92 = sext i8 %91 to i32
%93 = icmp sle i32 %92, 102
br i1 %93, label %94, label %107
94: ; preds = %87
%95 = load i32, i32* %4
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %96
%98 = load i8, i8* %97
%99 = sext i8 %98 to i32
%100 = sub nsw i32 %99, 87
%101 = load i32, i32* %5
%102 = call signext i8 @pow(i32 16, i32 %101)
%103 = sext i8 %102 to i32
%104 = mul nsw i32 %100, %103
%105 = load i32, i32* %6
%106 = add nsw i32 %105, %104
store i32 %106, i32* %6
br label %107
107: ; preds = %94, %87, %80
br label %108
108: ; preds = %107, %67
br label %109
109: ; preds = %108, %40
br label %110
110: ; preds = %109
%111 = load i32, i32* %5
%112 = add nsw i32 %111, 1
store i32 %112, i32* %5
%113 = load i32, i32* %4
%114 = add nsw i32 %113, -1
store i32 %114, i32* %4
br label %23
115: ; preds = %23
%116 = load i32, i32* %6
%117 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %116)
br label %118
118: ; preds = %115, %17
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"Enter str.(hex)\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [10 x i8] c"str : %s\0A\00"
@.str.3 = [14 x i8] c"Invalid input\00"
@.str.4 = [9 x i8] c"num: %d\0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%6 = call signext i8 @valid_hex(i8* %2)
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %46
10: ; preds = %0
%11 = call i32 @strlen(i8* %2)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %43
13: ; preds = %10
%14 = zext i32 %11 to i64
br label %15
15: ; preds = %13, %39
%16 = phi i64 [ %14, %13 ], [ %19, %39 ]
%17 = phi i32 [ 0, %13 ], [ %40, %39 ]
%18 = phi i32 [ 0, %13 ], [ %41, %39 ]
%19 = add nsw i64 %16, -1
%20 = and i64 %19, 4294967295
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = add i8 %22, -48
%25 = icmp ult i8 %24, 10
br i1 %25, label %32, label %26
26: ; preds = %15
%27 = add i8 %22, -65
%28 = icmp ult i8 %27, 6
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = add i8 %22, -97
%31 = icmp ult i8 %30, 6
br i1 %31, label %32, label %39
32: ; preds = %29, %26, %15
%33 = phi i32 [ -48, %15 ], [ -55, %26 ], [ -87, %29 ]
%34 = add nsw i32 %33, %23
%35 = call signext i8 @pow(i32 16, i32 %18)
%36 = sext i8 %35 to i32
%37 = mul nsw i32 %34, %36
%38 = add nsw i32 %37, %17
br label %39
39: ; preds = %32, %29
%40 = phi i32 [ %17, %29 ], [ %38, %32 ]
%41 = add nuw nsw i32 %18, 1
%42 = icmp eq i32 %41, %11
br i1 %42, label %43, label %15
43: ; preds = %39, %10
%44 = phi i32 [ 0, %10 ], [ %40, %39 ]
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43, %8
ret i32 0
}