#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 */
scalar_t__ ERANGE;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, ...);
long strtol(char const *, char **, int);
void convertHexToDec(const char *hex) {
  errno = 0;
  long int decimal = strtol(hex, NULL, 16);
  printf("hexadecimal value %s is %ld in decimal\n", hex, decimal);
  if (errno == ERANGE) {
    if (decimal == LONG_MIN)
      printf("   Underflow occurred on conversion immediately above.\n");
    else if (decimal == LONG_MAX)
      printf("   Overflow occurred on conversion immediately above.\n");
  } else if (errno != 0) {
    printf("   Some other kind of error occurred immediately above.\n");
  }
  char *invalidChar;
  decimal = strtol(hex, &invalidChar, 16);
  if (*invalidChar)
    printf("   First invalid character was %c\n", *invalidChar);
}


x86 assembly (clang, -O0):
<convertHexToDec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%rax
jne    11d3 <convertHexToDec+0xb3>
mov    -0x10(%rbp),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a7 <convertHexToDec+0x87>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ce <convertHexToDec+0xae>
mov    -0x10(%rbp),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11c9 <convertHexToDec+0xa9>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ce <convertHexToDec+0xae>
jmp    11f7 <convertHexToDec+0xd7>
mov    0x2dee(%rip),%rax
cmpq   $0x0,(%rax)
je     11f2 <convertHexToDec+0xd2>
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <convertHexToDec+0xd7>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     122f <convertHexToDec+0x10f>
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convertHexToDec>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%r15
movq   $0x0,(%r15)
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,%rbx
lea    0xea0(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%rax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <convertHexToDec+0x61>
mov    0x2e65(%rip),%rax
cmp    (%rax),%rbx
jne    119f <convertHexToDec+0x6f>
lea    0xf2a(%rip),%rdi
jmp    11b2 <convertHexToDec+0x82>
test   %rax,%rax
je     11b7 <convertHexToDec+0x87>
lea    0xeae(%rip),%rdi
jmp    11b2 <convertHexToDec+0x82>
mov    0x2e2a(%rip),%rax
cmp    (%rax),%rbx
jne    11b7 <convertHexToDec+0x87>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    0x8(%rsp),%rax
movsbl (%rax),%esi
test   %esi,%esi
je     11ec <convertHexToDec+0xbc>
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [40 x i8] c"hexadecimal value %s is %ld in decimal\0A\00"
@ERANGE = global i64 0
@LONG_MIN = global i64 0
@.str.1 = [56 x i8] c" Underflow occurred on conversion immediately above.\0A\00"
@LONG_MAX = global i64 0
@.str.2 = [55 x i8] c" Overflow occurred on conversion immediately above.\0A\00"
@.str.3 = [57 x i8] c" Some other kind of error occurred immediately above.\0A\00"
@.str.4 = [35 x i8] c" First invalid character was %c\0A\00"
define void @convertHexToDec(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtol(i8* %5, i8** null, i32 16)
store i64 %6, i64* %3
%7 = load i8*, i8** %2
%8 = load i64, i64* %3
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %7, i64 %8)
%10 = load i64, i64* @errno
%11 = load i64, i64* @ERANGE
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %27
13: ; preds = %1
%14 = load i64, i64* %3
%15 = load i64, i64* @LONG_MIN
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
br label %26
19: ; preds = %13
%20 = load i64, i64* %3
%21 = load i64, i64* @LONG_MAX
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
br label %26
26: ; preds = %25, %17
br label %33
27: ; preds = %1
%28 = load i64, i64* @errno
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
br label %33
33: ; preds = %32, %26
%34 = load i8*, i8** %2
%35 = call i64 @strtol(i8* %34, i8** %4, i32 16)
store i64 %35, i64* %3
%36 = load i8*, i8** %4
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %33
%40 = load i8*, i8** %4
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %39, %33
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [40 x i8] c"hexadecimal value %s is %ld in decimal\0A\00"
@ERANGE = global i64 0
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
@.str.4 = [35 x i8] c" First invalid character was %c\0A\00"
@str = [56 x i8] c" Some other kind of error occurred immediately above.\00"
@str.5 = [54 x i8] c" Overflow occurred on conversion immediately above.\00"
@str.6 = [55 x i8] c" Underflow occurred on conversion immediately above.\00"
define void @convertHexToDec(i8* %0) {
%2 = alloca i8*
store i64 0, i64* @errno
%3 = tail call i64 @strtol(i8* nocapture %0, i8** null, i32 16)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %3)
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i64, i64* @LONG_MIN
%10 = icmp eq i64 %3, %9
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = load i64, i64* @LONG_MAX
%13 = icmp eq i64 %3, %12
br i1 %13, label %16, label %19
14: ; preds = %1
%15 = icmp eq i64 %5, 0
br i1 %15, label %19, label %16
16: ; preds = %14, %11, %8
%17 = phi i8* [ getelementptr inbounds ([55 x i8], [55 x i8]* @str.6, i64 0, i64 0), %8 ], [ getelementptr inbounds ([54 x i8], [54 x i8]* @str.5, i64 0, i64 0), %11 ], [ getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0), %14 ]
%18 = tail call i32 @puts(i8* dereferenceable(1) %17)
br label %19
19: ; preds = %16, %14, %11
%20 = bitcast i8** %2 to i8*
%21 = call i64 @strtol(i8* %0, i8** %2, i32 16)
%22 = load i8*, i8** %2
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %19
%26 = sext i8 %23 to i32
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %19
ret void
}