#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*/ fgets(char *, int, int /*<<< orphan*/);
int get_digit(char);
int power(int, size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
size_t strlen(char *);
int main() {
  printf("Please enter a number in hex to convert to decimal: ");
  char line[17];
  fgets(line, 17, stdin);
  size_t length = strlen(line);
  if (line[length - 1] == '\n') {
    line[length - 1] = '\0';
  }
  length = strlen(line);
  int i;
  long long sum = 0;
  for (i = length - 1; i >= 0; i--) {
    int coeff = get_digit(line[i]);
    sum += coeff * power(16, length - i - 1);
  }
  printf("%lld\n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
mov    $0x11,%esi
call   1060 <fgets@plt>
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
movsbl -0x20(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    11b4 <main+0x64>
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     122e <main+0xde>
movslq -0x2c(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%edi
call   1070 <get_digit@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
movslq -0x2c(%rbp),%rax
sub    %rax,%rsi
sub    $0x1,%rsi
mov    $0x10,%edi
call   1030 <power@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
imul   %ecx,%eax
cltq
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d4 <main+0x84>
mov    -0x38(%rbp),%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xe9d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x11,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1197 <main+0x47>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    %rsp,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
add    $0xffffffff,%eax
js     11f5 <main+0xa5>
mov    %eax,%ebx
lea    (%rsp,%rbx,1),%rbp
not    %rbx
add    %r15,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
movsbl 0x0(%rbp),%edi
call   1070 <get_digit@plt>
mov    %eax,%r12d
mov    $0x10,%edi
mov    %rbx,%rsi
call   1030 <power@plt>
imul   %r12d,%eax
cltq
add    %rax,%r14
add    $0xffffffff,%r15d
add    $0xffffffffffffffff,%rbp
add    $0x1,%rbx
test   %r15d,%r15d
jg     11c0 <main+0x70>
jmp    11f8 <main+0xa8>
xor    %r14d,%r14d
lea    0xe36(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [53 x i8] c"Please enter a number in hex to convert to decimal: \00"
@stdin = global i32 0
@.str.1 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [17 x i8]
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 17, i32 %9)
%11 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = sub i64 %13, 1
%15 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %23
19: ; preds = %0
%20 = load i64, i64* %3
%21 = sub i64 %20, 1
%22 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %19, %0
%24 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%25 = call i64 @strlen(i8* %24)
store i64 %25, i64* %3
store i64 0, i64* %5
%26 = load i64, i64* %3
%27 = sub i64 %26, 1
%28 = trunc i64 %27 to i32
store i32 %28, i32* %4
br label %29
29: ; preds = %49, %23
%30 = load i32, i32* %4
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %52
32: ; preds = %29
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = call i32 @get_digit(i8 signext %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = load i64, i64* %3
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = sub i64 %39, %41
%43 = sub i64 %42, 1
%44 = call i32 @power(i32 16, i64 %43)
%45 = mul nsw i32 %38, %44
%46 = sext i32 %45 to i64
%47 = load i64, i64* %5
%48 = add nsw i64 %47, %46
store i64 %48, i64* %5
br label %49
49: ; preds = %32
%50 = load i32, i32* %4
%51 = add nsw i32 %50, -1
store i32 %51, i32* %4
br label %29
52: ; preds = %29
%53 = load i64, i64* %5
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %53)
ret i32 0
}

LLVM IR (-O2):
@.str = [53 x i8] c"Please enter a number in hex to convert to decimal: \00"
@stdin = global i32 0
@.str.1 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca [17 x i8]
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%4 = load i32, i32* @stdin
%5 = call i32 @fgets(i8* %3, i32 17, i32 %4)
%6 = call i64 @strlen(i8* %3)
%7 = add i64 %6, -1
%8 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 10
br i1 %10, label %11, label %12
11: ; preds = %0
store i8 0, i8* %8
br label %12
12: ; preds = %11, %0
%13 = call i64 @strlen(i8* %3)
%14 = trunc i64 %13 to i32
%15 = add i32 %14, -1
%16 = icmp sgt i32 %15, -1
br i1 %16, label %17, label %32
17: ; preds = %12, %17
%18 = phi i32 [ %30, %17 ], [ %15, %12 ]
%19 = phi i64 [ %29, %17 ], [ 0, %12 ]
%20 = zext i32 %18 to i64
%21 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = call i32 @get_digit(i8 signext %22)
%24 = xor i64 %20, -1
%25 = add i64 %13, %24
%26 = call i32 @power(i32 16, i64 %25)
%27 = mul nsw i32 %26, %23
%28 = sext i32 %27 to i64
%29 = add nsw i64 %19, %28
%30 = add nsw i32 %18, -1
%31 = icmp sgt i32 %18, 0
br i1 %31, label %17, label %32
32: ; preds = %17, %12
%33 = phi i64 [ 0, %12 ], [ %29, %17 ]
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %33)
ret i32 0
}