#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*/ memcpy(void *, void const *, size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void peek_safe(const void *stack, const size_t *count, size_t size, void *data,
               size_t max_count) {
  if (data == NULL)
    return;
  memset(data, 0, size);
  if (stack == NULL || count == NULL)
    return;
  if (*count == 0)
    return;
  if (*count > max_count)
    return;
  memcpy(data, stack + (*count - 1) * size, size);
}


x86 assembly (clang, -O0):
<peek_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    114c <peek_safe+0x2c>
jmp    11c3 <peek_safe+0xa3>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
cmpq   $0x0,-0x8(%rbp)
je     1171 <peek_safe+0x51>
cmpq   $0x0,-0x10(%rbp)
jne    1176 <peek_safe+0x56>
jmp    11c3 <peek_safe+0xa3>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1189 <peek_safe+0x69>
jmp    11c3 <peek_safe+0xa3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jbe    119f <peek_safe+0x7f>
jmp    11c3 <peek_safe+0xa3>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
sub    $0x1,%rax
imul   -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek_safe>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rcx,%rcx
je     117c <peek_safe+0x5c>
mov    %r8,%r12
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rcx,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
test   %r15,%r15
je     117c <peek_safe+0x5c>
test   %r13,%r13
je     117c <peek_safe+0x5c>
mov    0x0(%r13),%rax
add    $0xffffffffffffffff,%rax
cmp    %r12,%rax
jae    117c <peek_safe+0x5c>
imul   %r14,%rax
add    %rax,%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <memcpy@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @peek_safe(i8* %0, i64* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %6
store i64* %1, i64** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%11 = load i8*, i8** %9
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %5
br label %46
14: ; preds = %5
%15 = load i8*, i8** %9
%16 = load i64, i64* %8
%17 = call i32 @memset(i8* %15, i32 0, i64 %16)
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %14
%21 = load i64*, i64** %7
%22 = icmp eq i64* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20, %14
br label %46
24: ; preds = %20
%25 = load i64*, i64** %7
%26 = load i64, i64* %25
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
br label %46
29: ; preds = %24
%30 = load i64*, i64** %7
%31 = load i64, i64* %30
%32 = load i64, i64* %10
%33 = icmp ugt i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %29
br label %46
35: ; preds = %29
%36 = load i8*, i8** %9
%37 = load i8*, i8** %6
%38 = load i64*, i64** %7
%39 = load i64, i64* %38
%40 = sub i64 %39, 1
%41 = load i64, i64* %8
%42 = mul i64 %40, %41
%43 = getelementptr i8, i8* %37, i64 %42
%44 = load i64, i64* %8
%45 = call i32 @memcpy(i8* %36, i8* %43, i64 %44)
br label %46
46: ; preds = %35, %34, %28, %23, %13
ret void
}

LLVM IR (-O2):
define void @peek_safe(i8* %0, i64* %1, i64 %2, i8* %3, i64 %4) {
%6 = icmp eq i8* %3, null
br i1 %6, label %20, label %7
7: ; preds = %5
%8 = tail call i32 @memset(i8* %3, i32 0, i64 %2)
%9 = icmp eq i8* %0, null
%10 = icmp eq i64* %1, null
%11 = or i1 %9, %10
br i1 %11, label %20, label %12
12: ; preds = %7
%13 = load i64, i64* %1
%14 = add i64 %13, -1
%15 = icmp ult i64 %14, %4
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = mul i64 %14, %2
%18 = getelementptr i8, i8* %0, i64 %17
%19 = tail call i32 @memcpy(i8* %3, i8* %18, i64 %2)
br label %20
20: ; preds = %12, %7, %5, %16
ret void
}