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


x86 assembly (clang, -O0):
<peek_r_s_unsafe>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     112a <peek_r_s_unsafe+0x2a>
cmpq   $0x0,-0x18(%rbp)
jne    1137 <peek_r_s_unsafe+0x37>
movq   $0x0,-0x8(%rbp)
jmp    118b <peek_r_s_unsafe+0x8b>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1152 <peek_r_s_unsafe+0x52>
movq   $0x0,-0x8(%rbp)
jmp    118b <peek_r_s_unsafe+0x8b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jbe    1170 <peek_r_s_unsafe+0x70>
movq   $0x0,-0x8(%rbp)
jmp    118b <peek_r_s_unsafe+0x8b>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
sub    $0x1,%rcx
imul   -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek_r_s_unsafe>:
xor    %eax,%eax
test   %rdi,%rdi
je     1122 <peek_r_s_unsafe+0x22>
test   %rsi,%rsi
je     1122 <peek_r_s_unsafe+0x22>
mov    (%rsi),%rax
add    $0xffffffffffffffff,%rax
cmp    %rcx,%rax
jae    1123 <peek_r_s_unsafe+0x23>
imul   %rdx,%rax
add    %rax,%rdi
mov    %rdi,%rax
ret
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i8* @peek_r_s_unsafe(i8* readnone %0, i64* %1, i64 %2, i64 %3) {
%5 = icmp eq i8* %0, null
%6 = icmp eq i64* %1, null
%7 = or i1 %5, %6
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = load i64, i64* %1
%10 = add i64 %9, -1
%11 = icmp ult i64 %10, %3
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = mul i64 %10, %2
%14 = getelementptr i8, i8* %0, i64 %13
br label %15
15: ; preds = %8, %4, %12
%16 = phi i8* [ %14, %12 ], [ null, %4 ], [ null, %8 ]
ret i8* %16
}