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

/* Variables and functions */
int /*<<< orphan*/ fgets(unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sscanf(unsigned char *, char *, unsigned int *);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(unsigned char *, char *);
unsigned int strlen(unsigned char *);
unsigned char tolower(unsigned char);
int dpu_modify(void *memptr, unsigned int offset) {
  unsigned char input[0x100];
  unsigned char flush[0x100];
  unsigned int byte;
  unsigned int i;
  printf("*All byte values accepted in hex.*\nEnter '.' to stop.\n\n");
  for (;;) {
    printf("%04X : %02X > ", offset, *((unsigned char *)memptr + offset));
    fgets(input, 0x100, stdin);
    input[2] = '\0';
    if (input[0] == '.') {
      break;
    }
    if (strcmp(input, "") == 0) {
      continue;
    }
    if (strcmp(input, "\n") == 0) {
      continue;
    }
    for (i = 0; i < strlen(input); i++) {
      input[i] = tolower(input[i]);
    }
    if (sscanf(input, "%x", &byte) == 0) {
      continue;
    }
    *((unsigned char *)memptr + offset) = byte;
    ++offset;
    if (offset == 0x4000) {
      printf("End of memory.\n");
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<dpu_modify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1050 <fgets@plt>
movb   $0x0,-0x10e(%rbp)
movzbl -0x110(%rbp),%eax
cmp    $0x2e,%eax
jne    11d2 <dpu_modify+0x72>
jmp    12e7 <dpu_modify+0x187>
lea    -0x110(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11f4 <dpu_modify+0x94>
jmp    1180 <dpu_modify+0x20>
lea    -0x110(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1216 <dpu_modify+0xb6>
jmp    1180 <dpu_modify+0x20>
movl   $0x0,-0x218(%rbp)
mov    -0x218(%rbp),%eax
mov    %eax,-0x21c(%rbp)
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x21c(%rbp),%eax
cmp    %ecx,%eax
jae    127e <dpu_modify+0x11e>
mov    -0x218(%rbp),%eax
movzbl -0x110(%rbp,%rax,1),%edi
ff
call   1070 <tolower@plt>
mov    %al,%cl
mov    -0x218(%rbp),%eax
mov    %cl,-0x110(%rbp,%rax,1)
mov    -0x218(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x218(%rbp)
jmp    1220 <dpu_modify+0xc0>
lea    -0x110(%rbp),%rdi
lea    0xdbb(%rip),%rsi
lea    -0x214(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x0,%rax
jne    12a7 <dpu_modify+0x147>
jmp    1180 <dpu_modify+0x20>
mov    -0x214(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x4000,-0xc(%rbp)
jne    12e2 <dpu_modify+0x182>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12e7 <dpu_modify+0x187>
jmp    1180 <dpu_modify+0x20>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dpu_modify>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %esi,%r12d
mov    %rdi,%r15
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
lea    0xe66(%rip),%r14
mov    0x2e47(%rip),%r13
lea    0x10(%rsp),%rbx
lea    0xe9b(%rip),%rbp
mov    %r15,0x8(%rsp)
mov    %r12d,%esi
mov    %r12d,%r12d
movzbl (%r15,%r12,1),%edx
mov    %r14,%rdi
jmp    1206 <dpu_modify+0x96>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
lea    0xe35(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1090 <sscanf@plt>
test   %rax,%rax
mov    %rbp,%r14
mov    %r13,%rbp
mov    %r15,%r13
mov    0x8(%rsp),%r15
jne    12a0 <dpu_modify+0x130>
movzbl (%r15,%r12,1),%edx
mov    %r14,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
movb   $0x0,0x12(%rsp)
cmpb   $0x2e,0x10(%rsp)
je     12c5 <dpu_modify+0x155>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11fb <dpu_modify+0x8b>
mov    %rbx,%rdi
lea    0xdff(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11fb <dpu_modify+0x8b>
mov    %r13,%r15
mov    %rbp,%r13
mov    %r14,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     11d0 <dpu_modify+0x60>
xor    %r14d,%r14d
xchg   %ax,%ax
movzbl 0x10(%rsp,%r14,1),%edi
call   1080 <tolower@plt>
mov    %al,0x10(%rsp,%r14,1)
add    $0x1,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%r14
jb     1270 <dpu_modify+0x100>
jmp    11d0 <dpu_modify+0x60>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%al
mov    %al,(%r15,%r12,1)
add    $0x1,%r12d
cmp    $0x4000,%r12d
jne    11b2 <dpu_modify+0x42>
lea    0xd89(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"*All byte values accepted in hex.*\0AEnter '.' to stop.\0A\0A\00"
@.str.1 = [15 x i8] c"%04X : %02X > \00"
@stdin = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [3 x i8] c"%x\00"
@.str.5 = [16 x i8] c"End of memory.\0A\00"
define i32 @dpu_modify(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %74, %60, %37, %32, %2
%11 = load i32, i32* %4
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* @stdin
%21 = call i32 @fgets(i8* %19, i32 256, i32 %20)
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 2
store i8 0, i8* %22
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = icmp eq i32 %25, 46
br i1 %26, label %27, label %28
27: ; preds = %10
br label %75
28: ; preds = %10
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %10
33: ; preds = %28
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
br label %10
38: ; preds = %33
store i32 0, i32* %8
br label %39
39: ; preds = %53, %38
%40 = load i32, i32* %8
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = icmp ult i32 %40, %42
br i1 %43, label %44, label %56
44: ; preds = %39
%45 = load i32, i32* %8
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = call zeroext i8 @tolower(i8 zeroext %48)
%50 = load i32, i32* %8
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %51
store i8 %49, i8* %52
br label %53
53: ; preds = %44
%54 = load i32, i32* %8
%55 = add i32 %54, 1
store i32 %55, i32* %8
br label %39
56: ; preds = %39
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%58 = call i64 @sscanf(i8* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %7)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %56
br label %10
61: ; preds = %56
%62 = load i32, i32* %7
%63 = trunc i32 %62 to i8
%64 = load i8*, i8** %3
%65 = load i32, i32* %4
%66 = zext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 %63, i8* %67
%68 = load i32, i32* %4
%69 = add i32 %68, 1
store i32 %69, i32* %4
%70 = load i32, i32* %4
%71 = icmp eq i32 %70, 16384
br i1 %71, label %72, label %74
72: ; preds = %61
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %75
74: ; preds = %61
br label %10
75: ; preds = %72, %27
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"%04X : %02X > \00"
@stdin = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [3 x i8] c"%x\00"
@str = [55 x i8] c"*All byte values accepted in hex.*\0AEnter '.' to stop.\0A\00"
@str.6 = [15 x i8] c"End of memory.\00"
define i32 @dpu_modify(i8* nocapture %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 2
br label %9
9: ; preds = %49, %2
%10 = phi i32 [ %52, %49 ], [ %1, %2 ]
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %14)
%16 = load i32, i32* @stdin
%17 = call i32 @fgets(i8* %5, i32 256, i32 %16)
store i8 0, i8* %8
%18 = load i8, i8* %5
%19 = icmp eq i8 %18, 46
br i1 %19, label %56, label %20
20: ; preds = %9, %23
%21 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20, %31, %46
%24 = load i8, i8* %12
%25 = zext i8 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %25)
%27 = load i32, i32* @stdin
%28 = call i32 @fgets(i8* %5, i32 256, i32 %27)
store i8 0, i8* %8
%29 = load i8, i8* %5
%30 = icmp eq i8 %29, 46
br i1 %30, label %56, label %20
31: ; preds = %20
%32 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %23, label %34
34: ; preds = %31
%35 = call i32 @strlen(i8* %5)
%36 = icmp eq i32 %35, 0
br i1 %36, label %46, label %37
37: ; preds = %34, %37
%38 = phi i64 [ %42, %37 ], [ 0, %34 ]
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = call zeroext i8 @tolower(i8 zeroext %40)
store i8 %41, i8* %39
%42 = add nuw nsw i64 %38, 1
%43 = call i32 @strlen(i8* %5)
%44 = zext i32 %43 to i64
%45 = icmp ult i64 %42, %44
br i1 %45, label %37, label %46
46: ; preds = %37, %34
%47 = call i64 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%48 = icmp eq i64 %47, 0
br i1 %48, label %23, label %49
49: ; preds = %46
%50 = load i32, i32* %4
%51 = trunc i32 %50 to i8
store i8 %51, i8* %12
%52 = add i32 %10, 1
%53 = icmp eq i32 %52, 16384
br i1 %53, label %54, label %9
54: ; preds = %49
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
br label %56
56: ; preds = %9, %23, %54
ret i32 0
}