#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*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
void MemMod(void *memptr) {
  unsigned int address;
  unsigned char newValue;
  int check = 0;
  printf("\n-----------MEMORY MODIFY---------------\n");
  while (check == 0) {
    printf("\nEnter a starting address(HEX): ");
    scanf(" %x", &address);
    if (address <= 0x1388) {
      printf("value(HEX) at 0x%X, is: %02X\n", address,
             ((unsigned char *)memptr)[address]);
      printf("value(ASCII) at 0x%X, is: %c\n", address,
             ((unsigned char *)memptr)[address]);
      printf("\nEnter a new value: ");
      scanf(" %c", &newValue);
      if (newValue != '.') {
        ((unsigned char *)memptr)[address] = newValue;
        printf("value(HEX) at 0x%X, is: %02X\n", address,
               ((unsigned char *)memptr)[address]);
        printf("value(ASCII) at 0x%X, is: %c\n", address,
               ((unsigned char *)memptr)[address]);
      } else {
        printf("\nInvalid value entered, Exiting\n");
      }
      check = 1;
    } else {
      printf("\nStarting address Exceeds buffer size, 5000(1388 HEX). Try "
             "again.......\n");
    }
  }
}


x86 assembly (clang, -O0):
<MemMod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xec6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1254 <MemMod+0x134>
lea    0xed8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeeb(%rip),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
cmpl   $0x1388,-0xc(%rbp)
ja     1241 <MemMod+0x121>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xec2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xedb(%rip),%rdi
lea    -0xd(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movzbl -0xd(%rbp),%eax
cmp    $0x2e,%eax
je     1227 <MemMod+0x107>
mov    -0xd(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1235 <MemMod+0x115>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    124f <MemMod+0x12f>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1141 <MemMod+0x21>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MemMod>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xf33(%rip),%rdi
call   1030 <puts@plt>
lea    0xead(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec0(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0xc(%rsp),%esi
cmp    $0x1389,%esi
jb     11cb <MemMod+0x9b>
lea    0xf23(%rip),%r15
lea    0xe79(%rip),%r12
lea    0xe93(%rip),%r13
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0xc(%rsp),%esi
cmp    $0x1389,%esi
jae    11a0 <MemMod+0x70>
mov    %esi,%eax
movzbl (%r14,%rax,1),%edx
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%esi
movzbl (%r14,%rsi,1),%edx
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe63(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe6a(%rip),%rdi
lea    0xb(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
movzbl 0xb(%rsp),%edx
cmp    $0x2e,%edx
jne    1230 <MemMod+0x100>
lea    0xec2(%rip),%rdi
call   1030 <puts@plt>
jmp    125d <MemMod+0x12d>
mov    0xc(%rsp),%esi
mov    %dl,(%r14,%rsi,1)
lea    0xde6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%esi
movzbl (%r14,%rsi,1),%edx
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [42 x i8] c"\0A-----------MEMORY MODIFY---------------\0A\00"
@.str.1 = [33 x i8] c"\0AEnter a starting address(HEX): \00"
@.str.2 = [4 x i8] c" %x\00"
@.str.3 = [30 x i8] c"value(HEX) at 0x%X, is: %02X\0A\00"
@.str.4 = [30 x i8] c"value(ASCII) at 0x%X, is: %c\0A\00"
@.str.5 = [21 x i8] c"\0AEnter a new value: \00"
@.str.6 = [4 x i8] c" %c\00"
@.str.7 = [33 x i8] c"\0AInvalid value entered, Exiting\0A\00"
@.str.8 = [73 x i8] c"\0AStarting address Exceeds buffer size, 5000(1388 HEX). Try again.......\0A\00"
define void @MemMod(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %64, %1
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %65
10: ; preds = %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%13 = load i32, i32* %3
%14 = icmp ule i32 %13, 5000
br i1 %14, label %15, label %62
15: ; preds = %10
%16 = load i32, i32* %3
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %22)
%24 = load i32, i32* %3
%25 = load i8*, i8** %2
%26 = load i32, i32* %3
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %24, i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%34 = load i8, i8* %4
%35 = zext i8 %34 to i32
%36 = icmp ne i32 %35, 46
br i1 %36, label %37, label %59
37: ; preds = %15
%38 = load i8, i8* %4
%39 = load i8*, i8** %2
%40 = load i32, i32* %3
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 %38, i8* %42
%43 = load i32, i32* %3
%44 = load i8*, i8** %2
%45 = load i32, i32* %3
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %43, i32 %49)
%51 = load i32, i32* %3
%52 = load i8*, i8** %2
%53 = load i32, i32* %3
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %51, i32 %57)
br label %61
59: ; preds = %15
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %61
61: ; preds = %59, %37
store i32 1, i32* %5
br label %64
62: ; preds = %10
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.8, i64 0, i64 0))
br label %64
64: ; preds = %62, %61
br label %7
65: ; preds = %7
ret void
}

LLVM IR (-O2):
@.str.1 = [33 x i8] c"\0AEnter a starting address(HEX): \00"
@.str.2 = [4 x i8] c" %x\00"
@.str.3 = [30 x i8] c"value(HEX) at 0x%X, is: %02X\0A\00"
@.str.4 = [30 x i8] c"value(ASCII) at 0x%X, is: %c\0A\00"
@.str.5 = [21 x i8] c"\0AEnter a new value: \00"
@.str.6 = [4 x i8] c" %c\00"
@str = [41 x i8] c"\0A-----------MEMORY MODIFY---------------\00"
@str.9 = [72 x i8] c"\0AStarting address Exceeds buffer size, 5000(1388 HEX). Try again.......\00"
@str.10 = [32 x i8] c"\0AInvalid value entered, Exiting\00"
define void @MemMod(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca i8
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%8 = load i32, i32* %2
%9 = icmp ult i32 %8, 5001
br i1 %9, label %10, label %41
10: ; preds = %41, %1
%11 = phi i32 [ %8, %1 ], [ %45, %41 ]
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %11, i32 %15)
%17 = load i32, i32* %2
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %17, i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%24 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %3)
%25 = load i8, i8* %3
%26 = icmp eq i8 %25, 46
br i1 %26, label %39, label %27
27: ; preds = %10
%28 = load i32, i32* %2
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
store i8 %25, i8* %30
%31 = zext i8 %25 to i32
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %31)
%33 = load i32, i32* %2
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %0, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %33, i32 %37)
br label %47
39: ; preds = %10
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0))
br label %47
41: ; preds = %1, %41
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str.9, i64 0, i64 0))
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%45 = load i32, i32* %2
%46 = icmp ult i32 %45, 5001
br i1 %46, label %10, label %41
47: ; preds = %27, %39
ret void
}