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

/* Variables and functions */
int fgetc(int /*<<< orphan*/ *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
int parse_val(FILE *fp) {
  char hex_string[] = "0x00";
  int c;
  for (int i = 0; i < (sizeof(hex_string) - 1); ++i) {
    c = fgetc(fp);
    hex_string[i] = c;
  }
  return (int)strtol(hex_string, NULL, 16);
}


x86 assembly (clang, -O0):
<parse_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xece(%rip),%eax
mov    %eax,-0xd(%rbp)
mov    0xec9(%rip),%al
mov    %al,-0x9(%rbp)
movl   $0x0,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
jae    117a <parse_val+0x5a>
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%cl
movslq -0x18(%rbp),%rax
mov    %cl,-0xd(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1145 <parse_val+0x25>
lea    -0xd(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_val>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movb   $0x0,0xc(%rsp)
movl   $0x30307830,0x8(%rsp)
30
call   1030 <fgetc@plt>
mov    %al,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <fgetc@plt>
mov    %al,0x9(%rsp)
mov    %rbx,%rdi
call   1030 <fgetc@plt>
mov    %al,0xa(%rsp)
mov    %rbx,%rdi
call   1030 <fgetc@plt>
mov    %al,0xb(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.parse_val.hex_string = [5 x i8] c"0x00\00"
define i32 @parse_val(i32* %0) {
%2 = alloca i32*
%3 = alloca [5 x i8]
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
%6 = bitcast [5 x i8]* %3 to i8*
store i32 0, i32* %5
br label %7
7: ; preds = %19, %1
%8 = load i32, i32* %5
%9 = sext i32 %8 to i64
%10 = icmp ult i64 %9, 4
br i1 %10, label %11, label %22
11: ; preds = %7
%12 = load i32*, i32** %2
%13 = call i32 @fgetc(i32* %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = trunc i32 %14 to i8
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 %17
store i8 %15, i8* %18
br label %19
19: ; preds = %11
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
store i32 %21, i32* %5
br label %7
22: ; preds = %7
%23 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%24 = call i64 @strtol(i8* %23, i32* null, i32 16)
%25 = trunc i64 %24 to i32
ret i32 %25
}

LLVM IR (-O2):
@__const.parse_val.hex_string = [5 x i8] c"0x00\00"
define i32 @parse_val(i32* nocapture %0) {
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @fgetc(i32* %0)
%5 = trunc i32 %4 to i8
store i8 %5, i8* %3
%6 = tail call i32 @fgetc(i32* %0)
%7 = trunc i32 %6 to i8
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 1
store i8 %7, i8* %8
%9 = tail call i32 @fgetc(i32* %0)
%10 = trunc i32 %9 to i8
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 2
store i8 %10, i8* %11
%12 = tail call i32 @fgetc(i32* %0)
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 3
store i8 %13, i8* %14
%15 = call i64 @strtol(i8* nocapture %3, i32* null, i32 16)
%16 = trunc i64 %15 to i32
ret i32 %16
}