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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ printf(char *, ...);
void test_pointer_arrays() {
  printf("\n===== test_pointer_arrays\n");
  int values[] = {10, 20, 30};
  int size = sizeof(values) / sizeof(values[0]);
  assert(size == 3);
  printf("Initializing an array of int with %d elements: {%d, %d, %d}`\n", size,
         values[0], values[1], values[2]);
  int *p[size];
  for (int i = 0; i < size; ++i) {
    p[i] = &values[size - 1 - i];
  }
  printf("Setting pointers to the addresses of array elements (but reversed "
         "order): `p[i] = &values[size - 1 - i]`\n");
  assert(*p[0] == 30);
  assert(*p[1] == 20);
  assert(*p[2] == 10);
  printf("Dereferencing elements from array of int pointers: `*p[i]`. Returns "
         "{%d, %d, %d}`\n",
         *p[0], *p[1], *p[2]);
  assert(**p == 30);
  assert(**(p + 1) == 20);
  assert(**(p + 2) == 10);
  printf("Double dereferencing elements from array of int pointers: `**p`, "
         "`**(p + 1)`, `**(p + 2)`. Returns {%d, %d, %d}`\n",
         **p, **(p + 1), **(p + 2));
}


x86 assembly (clang, -O0):
<test_pointer_arrays>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xed1(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x39(%rbp)
call   1030 <printf@plt>
mov    0x1055(%rip),%eax
mov    %eax,-0x8(%rbp)
mov    0x1043(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
sete   %al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x39(%rbp),%al
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%r8d
lea    0xea0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ec <test_pointer_arrays+0xcc>
mov    -0x38(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
lea    -0x10(%rbp),%rdx
shl    $0x2,%rcx
add    %rcx,%rdx
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11af <test_pointer_arrays+0x8f>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
cmpl   $0x1e,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
cmpl   $0x14,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%rax
cmpl   $0xa,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rcx
mov    (%rcx),%esi
mov    0x8(%rax),%rcx
mov    (%rcx),%edx
mov    0x10(%rax),%rax
mov    (%rax),%ecx
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
cmpl   $0x1e,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
cmpl   $0x14,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%rax
cmpl   $0xa,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rcx
mov    (%rcx),%esi
mov    0x8(%rax),%rcx
mov    (%rcx),%edx
mov    0x10(%rax),%rax
mov    (%rax),%ecx
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_pointer_arrays>:
push   %rax
lea    0xfcb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
lea    0xeb2(%rip),%rdi
mov    $0x3,%esi
mov    $0xa,%edx
mov    $0x14,%ecx
mov    $0x1e,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xfad(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
lea    0xea3(%rip),%rdi
mov    $0x1e,%esi
mov    $0x14,%edx
mov    $0xa,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
lea    0xebb(%rip),%rdi
mov    $0x1e,%esi
mov    $0x14,%edx
mov    $0xa,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
pop    %rax
ret

LLVM IR (-O0):
@.str = [28 x i8] c"\0A===== test_pointer_arrays\0A\00"
@__const.test_pointer_arrays.values = [3 x i32] [i32 10, i32 20, i32 30]
@.str.1 = [62 x i8] c"Initializing an array of int with %d elements: {%d, %d, %d}`\0A\00"
@.str.2 = [106 x i8] c"Setting pointers to the addresses of array elements (but reversed order): `p[i] = &values[size - 1 - i]`\0A\00"
@.str.3 = [83 x i8] c"Dereferencing elements from array of int pointers: `*p[i]`. Returns {%d, %d, %d}`\0A\00"
@.str.4 = [114 x i8] c"Double dereferencing elements from array of int pointers: `**p`, `**(p + 1)`, `**(p + 2)`. Returns {%d, %d, %d}`\0A\00"
define void @test_pointer_arrays() {
%1 = alloca [3 x i32]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%7 = bitcast [3 x i32]* %1 to i8*
store i32 3, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 3
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32, i32* %2
%13 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 2
%18 = load i32, i32* %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %14, i32 %16, i32 %18)
%20 = load i32, i32* %2
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %3
%23 = alloca i32*, i64 %21
store i64 %21, i64* %4
store i32 0, i32* %5
br label %24
24: ; preds = %38, %0
%25 = load i32, i32* %5
%26 = load i32, i32* %2
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %41
28: ; preds = %24
%29 = load i32, i32* %2
%30 = sub nsw i32 %29, 1
%31 = load i32, i32* %5
%32 = sub nsw i32 %30, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 %33
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32*, i32** %23, i64 %36
store i32* %34, i32** %37
br label %38
38: ; preds = %28
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
br label %24
41: ; preds = %24
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.2, i64 0, i64 0))
%43 = getelementptr inbounds i32*, i32** %23, i64 0
%44 = load i32*, i32** %43
%45 = load i32, i32* %44
%46 = icmp eq i32 %45, 30
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = getelementptr inbounds i32*, i32** %23, i64 1
%50 = load i32*, i32** %49
%51 = load i32, i32* %50
%52 = icmp eq i32 %51, 20
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = getelementptr inbounds i32*, i32** %23, i64 2
%56 = load i32*, i32** %55
%57 = load i32, i32* %56
%58 = icmp eq i32 %57, 10
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = getelementptr inbounds i32*, i32** %23, i64 0
%62 = load i32*, i32** %61
%63 = load i32, i32* %62
%64 = getelementptr inbounds i32*, i32** %23, i64 1
%65 = load i32*, i32** %64
%66 = load i32, i32* %65
%67 = getelementptr inbounds i32*, i32** %23, i64 2
%68 = load i32*, i32** %67
%69 = load i32, i32* %68
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i32 %63, i32 %66, i32 %69)
%71 = load i32*, i32** %23
%72 = load i32, i32* %71
%73 = icmp eq i32 %72, 30
%74 = zext i1 %73 to i32
%75 = call i32 @assert(i32 %74)
%76 = getelementptr inbounds i32*, i32** %23, i64 1
%77 = load i32*, i32** %76
%78 = load i32, i32* %77
%79 = icmp eq i32 %78, 20
%80 = zext i1 %79 to i32
%81 = call i32 @assert(i32 %80)
%82 = getelementptr inbounds i32*, i32** %23, i64 2
%83 = load i32*, i32** %82
%84 = load i32, i32* %83
%85 = icmp eq i32 %84, 10
%86 = zext i1 %85 to i32
%87 = call i32 @assert(i32 %86)
%88 = load i32*, i32** %23
%89 = load i32, i32* %88
%90 = getelementptr inbounds i32*, i32** %23, i64 1
%91 = load i32*, i32** %90
%92 = load i32, i32* %91
%93 = getelementptr inbounds i32*, i32** %23, i64 2
%94 = load i32*, i32** %93
%95 = load i32, i32* %94
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([114 x i8], [114 x i8]* @.str.4, i64 0, i64 0), i32 %89, i32 %92, i32 %95)
%97 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@.str.1 = [62 x i8] c"Initializing an array of int with %d elements: {%d, %d, %d}`\0A\00"
@.str.3 = [83 x i8] c"Dereferencing elements from array of int pointers: `*p[i]`. Returns {%d, %d, %d}`\0A\00"
@.str.4 = [114 x i8] c"Double dereferencing elements from array of int pointers: `**p`, `**(p + 1)`, `**(p + 2)`. Returns {%d, %d, %d}`\0A\00"
@str = [27 x i8] c"\0A===== test_pointer_arrays\00"
@str.5 = [105 x i8] c"Setting pointers to the addresses of array elements (but reversed order): `p[i] = &values[size - 1 - i]`\00"
define void @test_pointer_arrays() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @assert(i32 1)
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 10, i32 20, i32 30)
%4 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([105 x i8], [105 x i8]* @str.5, i64 0, i64 0))
%5 = call i32 @assert(i32 1)
%6 = call i32 @assert(i32 1)
%7 = call i32 @assert(i32 1)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i32 30, i32 20, i32 10)
%9 = call i32 @assert(i32 1)
%10 = call i32 @assert(i32 1)
%11 = call i32 @assert(i32 1)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([114 x i8], [114 x i8]* @.str.4, i64 0, i64 0), i32 30, i32 20, i32 10)
ret void
}