#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*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ isxdigit(char);
int /*<<< orphan*/ strtol(char *, int /*<<< orphan*/ *, int);
int parse_hex_file(FILE *fp, unsigned char *mem) {
  int bytecount = 0;
  char byte[3];
  while (fgets(byte, 3, fp)) {
    if (isxdigit(byte[0]) && isxdigit(byte[1])) {
      if (bytecount < 0x8000) {
        *mem++ = strtol(byte, NULL, 16);
        ++bytecount;
      } else {
        return EXIT_FAILURE;
      }
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<parse_hex_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x1f(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3,%esi
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     11e4 <parse_hex_file+0xb4>
movsbl -0x1f(%rbp),%edi
call   1050 <isxdigit@plt>
cmp    $0x0,%rax
je     11df <parse_hex_file+0xaf>
movsbl -0x1e(%rbp),%edi
call   1050 <isxdigit@plt>
cmp    $0x0,%rax
je     11df <parse_hex_file+0xaf>
cmpl   $0x8000,-0x1c(%rbp)
jge    11c9 <parse_hex_file+0x99>
lea    -0x1f(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11da <parse_hex_file+0xaa>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <parse_hex_file+0xc0>
jmp    11df <parse_hex_file+0xaf>
jmp    1147 <parse_hex_file+0x17>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hex_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
lea    0x5(%rsp),%rdi
mov    $0x3,%esi
mov    %r14,%rdx
call   1030 <fgets@plt>
mov    0x2e7e(%rip),%r12
test   %rax,%rax
je     11ca <parse_hex_file+0x9a>
xor    %ebx,%ebx
lea    0x5(%rsp),%r15
mov    0x2e73(%rip),%rbp
jmp    1185 <parse_hex_file+0x55>
nop
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r14,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
je     11ca <parse_hex_file+0x9a>
movsbl 0x5(%rsp),%edi
call   1050 <isxdigit@plt>
test   %rax,%rax
je     1170 <parse_hex_file+0x40>
movsbl 0x6(%rsp),%edi
call   1050 <isxdigit@plt>
test   %rax,%rax
je     1170 <parse_hex_file+0x40>
cmp    $0x7fff,%ebx
jg     11c7 <parse_hex_file+0x97>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,0x0(%r13)
add    $0x1,%r13
add    $0x1,%ebx
jmp    1170 <parse_hex_file+0x40>
mov    %rbp,%r12
mov    (%r12),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @parse_hex_file(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [3 x i8]
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 0, i32* %6
br label %8
8: ; preds = %37, %2
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%10 = load i32*, i32** %4
%11 = call i64 @fgets(i8* %9, i32 3, i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %38
13: ; preds = %8
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%15 = load i8, i8* %14
%16 = call i64 @isxdigit(i8 signext %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %37
18: ; preds = %13
%19 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
%20 = load i8, i8* %19
%21 = call i64 @isxdigit(i8 signext %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %18
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 32768
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%28 = call i32 @strtol(i8* %27, i32* null, i32 16)
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
store i8 %29, i8* %30
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %36
34: ; preds = %23
%35 = load i32, i32* @EXIT_FAILURE
store i32 %35, i32* %3
br label %40
36: ; preds = %26
br label %37
37: ; preds = %36, %18, %13
br label %8
38: ; preds = %8
%39 = load i32, i32* @EXIT_SUCCESS
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @parse_hex_file(i32* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = call i64 @fgets(i8* %4, i32 3, i32* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %31, label %7
7: ; preds = %2
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
br label %9
9: ; preds = %7, %26
%10 = phi i32 [ 0, %7 ], [ %28, %26 ]
%11 = phi i8* [ %1, %7 ], [ %27, %26 ]
%12 = load i8, i8* %4
%13 = call i64 @isxdigit(i8 signext %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %9
%16 = load i8, i8* %8
%17 = call i64 @isxdigit(i8 signext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %15
%20 = icmp slt i32 %10, 32768
br i1 %20, label %21, label %31
21: ; preds = %19
%22 = call i32 @strtol(i8* nocapture %4, i32* null, i32 16)
%23 = trunc i32 %22 to i8
%24 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %23, i8* %11
%25 = add nsw i32 %10, 1
br label %26
26: ; preds = %21, %15, %9
%27 = phi i8* [ %24, %21 ], [ %11, %15 ], [ %11, %9 ]
%28 = phi i32 [ %25, %21 ], [ %10, %15 ], [ %10, %9 ]
%29 = call i64 @fgets(i8* %4, i32 3, i32* %0)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %9
31: ; preds = %19, %26, %2
%32 = phi i32* [ @EXIT_SUCCESS, %2 ], [ @EXIT_SUCCESS, %26 ], [ @EXIT_FAILURE, %19 ]
%33 = load i32, i32* %32
ret i32 %33
}