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

/* Variables and functions */
unsigned char HexValue(int /*<<< orphan*/);
int /*<<< orphan*/ IsHexChar(char);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
void main(int argc, char **argv) {
  FILE *InFile;
  FILE *OutFile;
  char NewLine[256];
  char *Next;
  unsigned char HiNibble;
  unsigned char LoNibble;
  if (argc != 3) {
    printf("Usage: %s LogFile BinFile\n", argv[0]);
    return;
  }
  InFile = fopen(argv[1], "r");
  if (!InFile) {
    printf("Cannot open input file %s\n", argv[1]);
    return;
  }
  OutFile = fopen(argv[2], "wb");
  if (!OutFile) {
    printf("Cannot open output file %s\n", argv[2]);
    fclose(InFile);
    return;
  }
  while (fgets(NewLine, sizeof(NewLine), InFile)) {
    if (strlen(NewLine) < 9)
      continue;
    if (NewLine[8] != ':')
      continue;
    Next = NewLine + 10;
    while (1) {
      if (!IsHexChar(*Next)) {
        printf("Invalid character %c in line %s\n", *Next, NewLine);
        goto Exit;
      }
      HiNibble = HexValue(*(Next++));
      if (!IsHexChar(*Next)) {
        printf("Invalid character %c in line %s\n", *Next, NewLine);
        goto Exit;
      }
      LoNibble = HexValue(*(Next++));
      LoNibble |= HiNibble << 4;
      fwrite(&LoNibble, 1, 1, OutFile);
      if (*Next == ' ') {
        Next++;
        continue;
      }
      break;
    }
  }
Exit:
  fclose(InFile);
  fclose(OutFile);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x4(%rbp)
je     11b6 <main+0x36>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13d9 <main+0x259>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <main+0x74>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13d9 <main+0x259>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe35(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123b <main+0xbb>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    13d9 <main+0x259>
jmp    1240 <main+0xc0>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     13c2 <main+0x242>
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x9,%eax
jge    1279 <main+0xf9>
jmp    1240 <main+0xc0>
movsbl -0x118(%rbp),%eax
cmp    $0x3a,%eax
je     128e <main+0x10e>
jmp    1240 <main+0xc0>
lea    -0x120(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rax
movsbl (%rax),%edi
call   1030 <IsHexChar@plt>
cmp    $0x0,%eax
jne    12dc <main+0x15c>
mov    -0x128(%rbp),%rax
movsbl (%rax),%esi
lea    -0x120(%rbp),%rdx
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13c7 <main+0x247>
mov    -0x128(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x128(%rbp)
movsbl (%rax),%edi
call   1070 <HexValue@plt>
mov    %al,-0x129(%rbp)
mov    -0x128(%rbp),%rax
movsbl (%rax),%edi
call   1030 <IsHexChar@plt>
cmp    $0x0,%eax
jne    133b <main+0x1bb>
mov    -0x128(%rbp),%rax
movsbl (%rax),%esi
lea    -0x120(%rbp),%rdx
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13c7 <main+0x247>
mov    -0x128(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x128(%rbp)
movsbl (%rax),%edi
call   1070 <HexValue@plt>
mov    %al,-0x12a(%rbp)
movzbl -0x129(%rbp),%ecx
shl    $0x4,%ecx
movzbl -0x12a(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x12a(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x12a(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10a0 <fwrite@plt>
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    13b8 <main+0x238>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    12a0 <main+0x120>
jmp    13bd <main+0x23d>
jmp    1240 <main+0xc0>
jmp    13c7 <main+0x247>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    11f4 <main+0x74>
mov    0x8(%rbx),%rdi
lea    0xe77(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12b2 <main+0x132>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe78(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12d5 <main+0x155>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1319 <main+0x199>
lea    0xa(%rsp),%r13
mov    %rsp,%r15
jmp    1229 <main+0xa9>
mov    (%rbx),%rsi
lea    0xe02(%rip),%rdi
jmp    12bd <main+0x13d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1319 <main+0x199>
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    $0x9,%eax
jl     1210 <main+0x90>
cmpb   $0x3a,0x8(%rsp)
jne    1210 <main+0x90>
movsbl 0xa(%rsp),%edi
call   1030 <IsHexChar@plt>
test   %eax,%eax
je     133b <main+0x1bb>
mov    %r13,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl 0x0(%rbp),%edi
call   1070 <HexValue@plt>
mov    %eax,%ebx
movsbl 0x1(%rbp),%edi
call   1030 <IsHexChar@plt>
test   %eax,%eax
je     1300 <main+0x180>
movsbl 0x1(%rbp),%edi
call   1070 <HexValue@plt>
shl    $0x4,%bl
or     %al,%bl
movsbl %bl,%edi
mov    %r12,%rsi
call   1080 <fputc@plt>
cmpb   $0x20,0x2(%rbp)
jne    1210 <main+0x90>
movsbl 0x3(%rbp),%edi
add    $0x3,%rbp
call   1030 <IsHexChar@plt>
test   %eax,%eax
jne    1260 <main+0xe0>
jmp    1304 <main+0x184>
mov    0x8(%rbx),%rsi
lea    0xd60(%rip),%rdi
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>
mov    0x10(%rbx),%rsi
lea    0xd5b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
add    $0x1,%rbp
movsbl 0x0(%rbp),%esi
lea    0xd48(%rip),%rdi
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rbp
jmp    1304 <main+0x184>

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: %s LogFile BinFile\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Cannot open input file %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [28 x i8] c"Cannot open output file %s\0A\00"
@.str.5 = [33 x i8] c"Invalid character %c in line %s\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i8
store i32 %0, i32* %3
store i8** %1, i8*** %4
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %4
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %121
18: ; preds = %2
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %30, label %25
25: ; preds = %18
%26 = load i8**, i8*** %4
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %121
30: ; preds = %18
%31 = load i8**, i8*** %4
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %34, i32** %6
%35 = load i32*, i32** %6
%36 = icmp ne i32* %35, null
br i1 %36, label %44, label %37
37: ; preds = %30
%38 = load i8**, i8*** %4
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = load i32*, i32** %5
%43 = call i32 @fclose(i32* %42)
br label %121
44: ; preds = %30
br label %45
45: ; preds = %114, %60, %54, %44
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%47 = load i32*, i32** %5
%48 = call i64 @fgets(i8* %46, i32 256, i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %115
50: ; preds = %45
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = icmp slt i32 %52, 9
br i1 %53, label %54, label %55
54: ; preds = %50
br label %45
55: ; preds = %50
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 8
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %58, 58
br i1 %59, label %60, label %61
60: ; preds = %55
br label %45
61: ; preds = %55
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%63 = getelementptr inbounds i8, i8* %62, i64 10
store i8* %63, i8** %8
br label %64
64: ; preds = %61, %110
%65 = load i8*, i8** %8
%66 = load i8, i8* %65
%67 = call i32 @IsHexChar(i8 signext %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %75, label %69
69: ; preds = %64
%70 = load i8*, i8** %8
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %72, i8* %73)
br label %116
75: ; preds = %64
%76 = load i8*, i8** %8
%77 = getelementptr inbounds i8, i8* %76, i32 1
store i8* %77, i8** %8
%78 = load i8, i8* %76
%79 = sext i8 %78 to i32
%80 = call zeroext i8 @HexValue(i32 %79)
store i8 %80, i8* %9
%81 = load i8*, i8** %8
%82 = load i8, i8* %81
%83 = call i32 @IsHexChar(i8 signext %82)
%84 = icmp ne i32 %83, 0
br i1 %84, label %91, label %85
85: ; preds = %75
%86 = load i8*, i8** %8
%87 = load i8, i8* %86
%88 = sext i8 %87 to i32
%89 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %88, i8* %89)
br label %116
91: ; preds = %75
%92 = load i8*, i8** %8
%93 = getelementptr inbounds i8, i8* %92, i32 1
store i8* %93, i8** %8
%94 = load i8, i8* %92
%95 = sext i8 %94 to i32
%96 = call zeroext i8 @HexValue(i32 %95)
store i8 %96, i8* %10
%97 = load i8, i8* %9
%98 = zext i8 %97 to i32
%99 = shl i32 %98, 4
%100 = load i8, i8* %10
%101 = zext i8 %100 to i32
%102 = or i32 %101, %99
%103 = trunc i32 %102 to i8
store i8 %103, i8* %10
%104 = load i32*, i32** %6
%105 = call i32 @fwrite(i8* %10, i32 1, i32 1, i32* %104)
%106 = load i8*, i8** %8
%107 = load i8, i8* %106
%108 = sext i8 %107 to i32
%109 = icmp eq i32 %108, 32
br i1 %109, label %110, label %113
110: ; preds = %91
%111 = load i8*, i8** %8
%112 = getelementptr inbounds i8, i8* %111, i32 1
store i8* %112, i8** %8
br label %64
113: ; preds = %91
br label %114
114: ; preds = %113
br label %45
115: ; preds = %45
br label %116
116: ; preds = %115, %85, %69
%117 = load i32*, i32** %5
%118 = call i32 @fclose(i32* %117)
%119 = load i32*, i32** %6
%120 = call i32 @fclose(i32* %119)
br label %121
121: ; preds = %116, %37, %25, %13
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"Usage: %s LogFile BinFile\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Cannot open input file %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [28 x i8] c"Cannot open output file %s\0A\00"
@.str.5 = [33 x i8] c"Invalid character %c in line %s\0A\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %78
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i8*, i8** %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %78
17: ; preds = %9
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %28, label %22
22: ; preds = %17
%23 = call i64 @fgets(i8* %4, i32 256, i32* %12)
%24 = icmp eq i64 %23, 0
br i1 %24, label %75, label %25
25: ; preds = %22
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 8
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 10
br label %32
28: ; preds = %17
%29 = load i8*, i8** %18
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = tail call i32 @fclose(i32* %12)
br label %78
32: ; preds = %25, %38
%33 = call i32 @strlen(i8* %4)
%34 = icmp slt i32 %33, 9
%35 = load i8, i8* %26
%36 = icmp ne i8 %35, 58
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %38, label %41
38: ; preds = %54, %32
%39 = call i64 @fgets(i8* %4, i32 256, i32* %12)
%40 = icmp eq i64 %39, 0
br i1 %40, label %75, label %32
41: ; preds = %32
%42 = load i8, i8* %27
%43 = call i32 @IsHexChar(i8 signext %42)
%44 = icmp eq i32 %43, 0
br i1 %44, label %70, label %45
45: ; preds = %41, %65
%46 = phi i8* [ %66, %65 ], [ %27, %41 ]
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8, i8* %46
%49 = sext i8 %48 to i32
%50 = call zeroext i8 @HexValue(i32 %49)
%51 = load i8, i8* %47
%52 = call i32 @IsHexChar(i8 signext %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %70, label %54
54: ; preds = %45
%55 = getelementptr inbounds i8, i8* %46, i64 2
%56 = load i8, i8* %47
%57 = sext i8 %56 to i32
%58 = call zeroext i8 @HexValue(i32 %57)
%59 = shl i8 %50, 4
%60 = or i8 %58, %59
%61 = sext i8 %60 to i32
%62 = call i32 @fputc(i32 %61, i32* %20)
%63 = load i8, i8* %55
%64 = icmp eq i8 %63, 32
br i1 %64, label %65, label %38
65: ; preds = %54
%66 = getelementptr inbounds i8, i8* %46, i64 3
%67 = load i8, i8* %66
%68 = call i32 @IsHexChar(i8 signext %67)
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %45
70: ; preds = %41, %45, %65
%71 = phi i8* [ %66, %65 ], [ %47, %45 ], [ %27, %41 ]
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %73, i8* %4)
br label %75
75: ; preds = %38, %70, %22
%76 = call i32 @fclose(i32* %12)
%77 = call i32 @fclose(i32* %20)
br label %78
78: ; preds = %75, %28, %14, %6
ret void
}