#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 */
scalar_t__ EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
scalar_t__ scanf(char *, unsigned int *);
int main(int argc, char *argv[]) {
  unsigned int input;
  FILE *fout;
  fout = fopen("exploit.bytes", "w");
  while ((scanf(" %x", &input)) != EOF) {
    if (input < 0x00 || input > 0xFF) {
      printf("Error: over range(0x00 <= input <= 0xFF)\n");
      fclose(fout);
      exit(1);
    }
    fwrite((char *)&input, sizeof(char), 1, fout);
  }
  fwrite("\n", sizeof(char), 1, fout);
  fclose(fout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    0xe7c(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1050 <scanf@plt>
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
je     11fc <main+0x9c>
cmpl   $0x0,-0x14(%rbp)
jb     11c4 <main+0x64>
cmpl   $0xff,-0x14(%rbp)
jbe    11e3 <main+0x83>
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x14(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1080 <fwrite@plt>
jmp    118d <main+0x2d>
mov    -0x20(%rbp),%rcx
lea    0xe35(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe91(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe8b(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x2e52(%rip),%r12
cmp    (%r12),%rax
je     11d7 <main+0x77>
lea    0xe6d(%rip),%r14
lea    0x4(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
cmp    $0x100,%eax
jae    11fa <main+0x9a>
movsbl %al,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <scanf@plt>
cmp    (%r12),%rax
jne    11b0 <main+0x50>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe13(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"exploit.bytes\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c" %x\00"
@EOF = global i64 0
@.str.3 = [42 x i8] c"Error: over range(0x00 <= input <= 0xFF)\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %7
br label %9
9: ; preds = %24, %2
%10 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%11 = load i64, i64* @EOF
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %28
13: ; preds = %9
%14 = load i32, i32* %6
%15 = icmp ult i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* %6
%18 = icmp ugt i32 %17, 255
br i1 %18, label %19, label %24
19: ; preds = %16, %13
%20 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32*, i32** %7
%22 = call i32 @fclose(i32* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = bitcast i32* %6 to i8*
%26 = load i32*, i32** %7
%27 = call i32 @fwrite(i8* %25, i32 1, i32 1, i32* %26)
br label %9
28: ; preds = %9
%29 = load i32*, i32** %7
%30 = call i32 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 1, i32* %29)
%31 = load i32*, i32** %7
%32 = call i32 @fclose(i32* %31)
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"exploit.bytes\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c" %x\00"
@EOF = global i64 0
@str = [41 x i8] c"Error: over range(0x00 <= input <= 0xFF)\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%7 = load i64, i64* @EOF
%8 = icmp eq i64 %6, %7
br i1 %8, label %23, label %9
9: ; preds = %2, %16
%10 = load i32, i32* %3
%11 = icmp ugt i32 %10, 255
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%14 = call i32 @fclose(i32* %5)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = shl nuw i32 %10, 24
%18 = ashr exact i32 %17, 24
%19 = call i32 @fputc(i32 %18, i32* %5)
%20 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%21 = load i64, i64* @EOF
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %9
23: ; preds = %16, %2
%24 = call i32 @fputc(i32 10, i32* %5)
%25 = call i32 @fclose(i32* %5)
ret i32 0
}