#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 uint8_t;

/* Variables and functions */
char fgetc(int /*<<< orphan*/);
char gethexdigit(char);
scalar_t__ hexdigit(char);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
void writehex(uint8_t *buf, size_t lim) {
  size_t off = 0;
  char prev;
  char ch;
  uint8_t i = 0;
  printf("\nW> ");
  ch = fgetc(stdin);
  while ((0xa != ch) && ((0x20 == ch) || hexdigit(ch))) {
    printf("%c", ch);
    if ((0x20 == ch) && (1 == i)) {
      printf("\nINVALID BYTE\n");
      break;
    } else if (0x20 != ch) {
      if (i) {
        ch = gethexdigit(ch);
        i = ((prev << 4) + ch);
        buf[off++] = i;
        i = 0;
      } else {
        prev = gethexdigit(ch);
        i++;
      }
    }
    if (off == lim) {
      break;
    }
    ch = fgetc(stdin);
  }
  printf("\nWROTE %lu BYTE(S)\n", (long unsigned)off);
}


x86 assembly (clang, -O0):
<writehex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %al,-0x1a(%rbp)
movsbl -0x1a(%rbp),%edx
xor    %eax,%eax
mov    $0xa,%ecx
cmp    %edx,%ecx
mov    %al,-0x21(%rbp)
je     11c3 <writehex+0x83>
movsbl -0x1a(%rbp),%edx
mov    $0x1,%al
mov    $0x20,%ecx
cmp    %edx,%ecx
mov    %al,-0x22(%rbp)
je     11bd <writehex+0x7d>
movsbl -0x1a(%rbp),%edi
call   1060 <hexdigit@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11d3 <writehex+0x93>
jmp    12bb <writehex+0x17b>
movsbl -0x1a(%rbp),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x1a(%rbp),%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
jne    1217 <writehex+0xd7>
mov    $0x1,%eax
cmp    -0x20(%rbp),%eax
jne    1217 <writehex+0xd7>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bb <writehex+0x17b>
movsbl -0x1a(%rbp),%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
je     128d <writehex+0x14d>
cmpl   $0x0,-0x20(%rbp)
je     1273 <writehex+0x133>
movsbl -0x1a(%rbp),%edi
call   1030 <gethexdigit@plt>
mov    %al,-0x1a(%rbp)
movsbl -0x19(%rbp),%eax
shl    $0x4,%eax
movsbl -0x1a(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
mov    %edx,(%rax,%rcx,4)
movl   $0x0,-0x20(%rbp)
jmp    1288 <writehex+0x148>
movsbl -0x1a(%rbp),%edi
call   1030 <gethexdigit@plt>
mov    %al,-0x19(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    128d <writehex+0x14d>
jmp    1292 <writehex+0x152>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    12a5 <writehex+0x165>
jmp    12bb <writehex+0x17b>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %al,-0x1a(%rbp)
jmp    117e <writehex+0x3e>
mov    -0x18(%rbp),%rsi
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writehex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,(%rsp)
lea    0xe87(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e5f(%rip),%r12
xor    %r15d,%r15d
jmp    11a2 <writehex+0x42>
xchg   %ax,%ax
mov    %ebp,%edi
call   1030 <putchar@plt>
cmp    $0x1,%r15d
je     1203 <writehex+0xa3>
cmp    %r14,%rbx
je     11fe <writehex+0x9e>
mov    (%r12),%edi
call   1070 <fgetc@plt>
movsbl %al,%ebp
cmp    $0x20,%bpl
je     1190 <writehex+0x30>
cmp    $0xa,%al
je     120f <writehex+0xaf>
mov    %ebp,%edi
call   1080 <hexdigit@plt>
test   %rax,%rax
je     120f <writehex+0xaf>
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    %ebp,%edi
call   1050 <gethexdigit@plt>
test   %r15d,%r15d
jne    11e2 <writehex+0x82>
mov    $0x1,%r15d
mov    %eax,%r13d
jmp    119d <writehex+0x3d>
movsbl %r13b,%ecx
shl    $0x4,%ecx
movsbl %al,%eax
add    %ecx,%eax
mov    (%rsp),%rcx
mov    %eax,(%rcx,%rbx,4)
add    $0x1,%rbx
xor    %r15d,%r15d
jmp    119d <writehex+0x3d>
mov    %r14,%rbx
jmp    120f <writehex+0xaf>
lea    0xe0f(%rip),%rdi
call   1040 <puts@plt>
lea    0xdef(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"\0AW> \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%c\00"
@.str.2 = [15 x i8] c"\0AINVALID BYTE\0A\00"
@.str.3 = [20 x i8] c"\0AWROTE %lu BYTE(S)\0A\00"
define void @writehex(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %5
store i32 0, i32* %8
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @stdin
%11 = call signext i8 @fgetc(i32 %10)
store i8 %11, i8* %7
br label %12
12: ; preds = %73, %2
%13 = load i8, i8* %7
%14 = sext i8 %13 to i32
%15 = icmp ne i32 10, %14
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load i8, i8* %7
%18 = sext i8 %17 to i32
%19 = icmp eq i32 32, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i8, i8* %7
%22 = call i64 @hexdigit(i8 signext %21)
%23 = icmp ne i64 %22, 0
br label %24
24: ; preds = %20, %16
%25 = phi i1 [ true, %16 ], [ %23, %20 ]
br label %26
26: ; preds = %24, %12
%27 = phi i1 [ false, %12 ], [ %25, %24 ]
br i1 %27, label %28, label %76
28: ; preds = %26
%29 = load i8, i8* %7
%30 = sext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i8, i8* %7
%33 = sext i8 %32 to i32
%34 = icmp eq i32 32, %33
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i32, i32* %8
%37 = icmp eq i32 1, %36
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %76
40: ; preds = %35, %28
%41 = load i8, i8* %7
%42 = sext i8 %41 to i32
%43 = icmp ne i32 32, %42
br i1 %43, label %44, label %67
44: ; preds = %40
%45 = load i32, i32* %8
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %61
47: ; preds = %44
%48 = load i8, i8* %7
%49 = call signext i8 @gethexdigit(i8 signext %48)
store i8 %49, i8* %7
%50 = load i8, i8* %6
%51 = sext i8 %50 to i32
%52 = shl i32 %51, 4
%53 = load i8, i8* %7
%54 = sext i8 %53 to i32
%55 = add nsw i32 %52, %54
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = load i32*, i32** %3
%58 = load i64, i64* %5
%59 = add i64 %58, 1
store i64 %59, i64* %5
%60 = getelementptr inbounds i32, i32* %57, i64 %58
store i32 %56, i32* %60
store i32 0, i32* %8
br label %66
61: ; preds = %44
%62 = load i8, i8* %7
%63 = call signext i8 @gethexdigit(i8 signext %62)
store i8 %63, i8* %6
%64 = load i32, i32* %8
%65 = add nsw i32 %64, 1
store i32 %65, i32* %8
br label %66
66: ; preds = %61, %47
br label %67
67: ; preds = %66, %40
br label %68
68: ; preds = %67
%69 = load i64, i64* %5
%70 = load i64, i64* %4
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %73
72: ; preds = %68
br label %76
73: ; preds = %68
%74 = load i32, i32* @stdin
%75 = call signext i8 @fgetc(i32 %74)
store i8 %75, i8* %7
br label %12
76: ; preds = %72, %38, %26
%77 = load i64, i64* %5
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %77)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"\0AW> \00"
@stdin = global i32 0
@.str.3 = [20 x i8] c"\0AWROTE %lu BYTE(S)\0A\00"
@str = [14 x i8] c"\0AINVALID BYTE\00"
define void @writehex(i32* nocapture writeonly %0, i64 %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %4
4: ; preds = %30, %2
%5 = phi i64 [ 0, %2 ], [ %31, %30 ]
%6 = phi i8 [ undef, %2 ], [ %32, %30 ]
%7 = phi i32 [ 0, %2 ], [ %33, %30 ]
%8 = load i32, i32* @stdin
%9 = tail call signext i8 @fgetc(i32 %8)
%10 = sext i8 %9 to i32
switch i8 %9, label %11 [
i8 10, label %35
i8 32, label %14
]
11: ; preds = %4
%12 = tail call i64 @hexdigit(i8 signext %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %35, label %19
14: ; preds = %4
%15 = tail call i32 @putchar(i32 %10)
%16 = icmp eq i32 %7, 1
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %35
19: ; preds = %11
%20 = tail call i32 @putchar(i32 %10)
%21 = icmp eq i32 %7, 0
%22 = tail call signext i8 @gethexdigit(i8 signext %9)
br i1 %21, label %30, label %23
23: ; preds = %19
%24 = sext i8 %6 to i32
%25 = shl nsw i32 %24, 4
%26 = sext i8 %22 to i32
%27 = add nsw i32 %25, %26
%28 = add i64 %5, 1
%29 = getelementptr inbounds i32, i32* %0, i64 %5
store i32 %27, i32* %29
br label %30
30: ; preds = %19, %14, %23
%31 = phi i64 [ %28, %23 ], [ %5, %14 ], [ %5, %19 ]
%32 = phi i8 [ %6, %23 ], [ %6, %14 ], [ %22, %19 ]
%33 = phi i32 [ 0, %23 ], [ %7, %14 ], [ 1, %19 ]
%34 = icmp eq i64 %31, %1
br i1 %34, label %35, label %4
35: ; preds = %4, %30, %11, %17
%36 = phi i64 [ %5, %17 ], [ %5, %4 ], [ %5, %11 ], [ %1, %30 ]
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %36)
ret void
}