#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*/ valuearray;
typedef int uint8_t;

/* Variables and functions */
scalar_t__ ENOMEM;
scalar_t__ errno;
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write_byte_array(int, int *, int);
void rtc_write_memory(uint8_t address, uint8_t *valuearray) {
  if (address >= 0x08 && address <= 0x3F) {
    if (address + sizeof(valuearray) <= 0x3F) {
      uint8_t length = sizeof(valuearray);
      uint8_t *writearray = malloc(length + 1);
      if (errno == ENOMEM) {
        free(writearray);
        printf("memory allocation error: not enough system memory to allocate "
               "array");
      } else {
        writearray[0] = address;
        int a;
        for (a = 0; a < length; a++) {
          writearray[a + 1] = valuearray[a];
        }
        write_byte_array(0x68, writearray, (uint8_t)length + 1);
        free(writearray);
      }
    } else {
      printf("memory overflow error: address + length exceeds 0x3F");
    }
  } else {
    printf("address out of range");
  }
}


x86 assembly (clang, -O0):
<rtc_write_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x8,-0x4(%rbp)
jl     1237 <rtc_write_memory+0xf7>
cmpl   $0x3f,-0x4(%rbp)
jg     1237 <rtc_write_memory+0xf7>
movslq -0x4(%rbp),%rax
add    $0x8,%rax
cmp    $0x3f,%rax
ja     1226 <rtc_write_memory+0xe6>
movl   $0x8,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <rtc_write_memory+0x7f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
jmp    1221 <rtc_write_memory+0xe1>
mov    -0x4(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1204 <rtc_write_memory+0xc4>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11cf <rtc_write_memory+0x8f>
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
mov    $0x68,%edi
call   1060 <write_byte_array@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1232 <rtc_write_memory+0xf2>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
jmp    1243 <rtc_write_memory+0x103>
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtc_write_memory>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%ebx
lea    -0x8(%rbx),%eax
cmp    $0x37,%eax
ja     118b <rtc_write_memory+0x4b>
cmp    $0x37,%ebx
ja     1194 <rtc_write_memory+0x54>
mov    %rsi,%r14
mov    $0x9,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    11a5 <rtc_write_memory+0x65>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xe77(%rip),%rdi
jmp    119b <rtc_write_memory+0x5b>
lea    0xee7(%rip),%rdi
jmp    119b <rtc_write_memory+0x5b>
lea    0xea9(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <printf@plt>
mov    %ebx,(%r15)
movups (%r14),%xmm0
movups 0x10(%r14),%xmm1
movups %xmm0,0x4(%r15)
movups %xmm1,0x14(%r15)
mov    $0x68,%edi
mov    %r15,%rsi
mov    $0x9,%edx
call   1060 <write_byte_array@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@errno = global i64 0
@ENOMEM = global i64 0
@.str = [68 x i8] c"memory allocation error: not enough system memory to allocate array\00"
@.str.1 = [53 x i8] c"memory overflow error: address + length exceeds 0x3F\00"
@.str.2 = [21 x i8] c"address out of range\00"
define void @rtc_write_memory(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%8 = load i32, i32* %3
%9 = icmp sge i32 %8, 8
br i1 %9, label %10, label %62
10: ; preds = %2
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 63
br i1 %12, label %13, label %62
13: ; preds = %10
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = add i64 %15, 8
%17 = icmp ule i64 %16, 63
br i1 %17, label %18, label %59
18: ; preds = %13
store i32 8, i32* %5
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
%21 = call i32* @malloc(i32 %20)
store i32* %21, i32** %6
%22 = load i64, i64* @errno
%23 = load i64, i64* @ENOMEM
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i32*, i32** %6
%27 = call i32 @free(i32* %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0))
br label %58
29: ; preds = %18
%30 = load i32, i32* %3
%31 = load i32*, i32** %6
%32 = getelementptr inbounds i32, i32* %31, i64 0
store i32 %30, i32* %32
store i32 0, i32* %7
br label %33
33: ; preds = %48, %29
%34 = load i32, i32* %7
%35 = load i32, i32* %5
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %51
37: ; preds = %33
%38 = load i32*, i32** %4
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = load i32*, i32** %6
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %43, i64 %46
store i32 %42, i32* %47
br label %48
48: ; preds = %37
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %33
51: ; preds = %33
%52 = load i32*, i32** %6
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
%55 = call i32 @write_byte_array(i32 104, i32* %52, i32 %54)
%56 = load i32*, i32** %6
%57 = call i32 @free(i32* %56)
br label %58
58: ; preds = %51, %25
br label %61
59: ; preds = %13
%60 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %61
61: ; preds = %59, %58
br label %64
62: ; preds = %10, %2
%63 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %61
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@ENOMEM = global i64 0
@.str = [68 x i8] c"memory allocation error: not enough system memory to allocate array\00"
@.str.1 = [53 x i8] c"memory overflow error: address + length exceeds 0x3F\00"
@.str.2 = [21 x i8] c"address out of range\00"
define void @rtc_write_memory(i32 %0, i32* nocapture %1) {
%3 = bitcast i32* %1 to i8*
%4 = add i32 %0, -8
%5 = icmp ult i32 %4, 56
br i1 %5, label %6, label %23
6: ; preds = %2
%7 = icmp ult i32 %0, 56
br i1 %7, label %8, label %21
8: ; preds = %6
%9 = tail call i32* @malloc(i32 9)
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOMEM
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @free(i32* %9)
%15 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0))
br label %25
16: ; preds = %8
store i32 %0, i32* %9
%17 = getelementptr i32, i32* %9, i64 1
%18 = bitcast i32* %17 to i8*
%19 = tail call i32 @write_byte_array(i32 104, i32* %9, i32 9)
%20 = tail call i32 @free(i32* %9)
br label %25
21: ; preds = %6
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %25
23: ; preds = %2
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %13, %16, %21, %23
ret void
}