#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ printf(char *, ...);
void ptr_arithmetic() {
  printf("POINTER ARITHMETIC \n");
  printf("------------\n");
  int n_elems = 11;
  int *arr = (int *)calloc(n_elems, sizeof(int));
  for (int i = 0; i < n_elems; i++) {
    arr[i] = i * 10;
  }
  for (int *i = arr; i < arr + n_elems; i++) {
    printf("arr[%d]	%d\n", i - arr, *i);
  }
}


x86 assembly (clang, -O0):
<ptr_arithmetic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xed1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xb,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    118c <ptr_arithmetic+0x6c>
imul   $0xa,-0x14(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1163 <ptr_arithmetic+0x43>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x4(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    11e4 <ptr_arithmetic+0xc4>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
sub    %rax,%rsi
sar    $0x2,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    1194 <ptr_arithmetic+0x74>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptr_arithmetic>:
push   %r14
push   %rbx
push   %rax
lea    0xef1(%rip),%rdi
call   1030 <puts@plt>
lea    0xef9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xb,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
movaps 0xe9b(%rip),%xmm0
movups %xmm0,(%rax)
movaps 0xea1(%rip),%xmm0
movups %xmm0,0x10(%rax)
movabs $0x5a00000050,%rax
mov    %rax,0x20(%rbx)
movl   $0x64,0x28(%rbx)
lea    0xe91(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rbx),%edx
mov    $0x1,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%edx
mov    $0x2,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rbx),%edx
mov    $0x3,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%edx
mov    $0x4,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x14(%rbx),%edx
mov    $0x5,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x18(%rbx),%edx
mov    $0x6,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x1c(%rbx),%edx
mov    $0x7,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x20(%rbx),%edx
mov    $0x8,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x24(%rbx),%edx
mov    $0x9,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x28(%rbx),%edx
mov    $0xa,%esi
mov    %r14,%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"POINTER ARITHMETIC \0A\00"
@.str.1 = [14 x i8] c"------------\0A\00"
@.str.2 = [12 x i8] c"arr[%d]\09%d\0A\00"
define void @ptr_arithmetic() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 11, i32* %1
%7 = load i32, i32* %1
%8 = call i64 @calloc(i32 %7, i32 4)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %2
store i32 0, i32* %3
br label %10
10: ; preds = %21, %0
%11 = load i32, i32* %3
%12 = load i32, i32* %1
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %24
14: ; preds = %10
%15 = load i32, i32* %3
%16 = mul nsw i32 %15, 10
%17 = load i32*, i32** %2
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
store i32 %16, i32* %20
br label %21
21: ; preds = %14
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %10
24: ; preds = %10
%25 = load i32*, i32** %2
store i32* %25, i32** %4
br label %26
26: ; preds = %43, %24
%27 = load i32*, i32** %4
%28 = load i32*, i32** %2
%29 = load i32, i32* %1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = icmp ult i32* %27, %31
br i1 %32, label %33, label %46
33: ; preds = %26
%34 = load i32*, i32** %4
%35 = load i32*, i32** %2
%36 = ptrtoint i32* %34 to i64
%37 = ptrtoint i32* %35 to i64
%38 = sub i64 %36, %37
%39 = sdiv exact i64 %38, 4
%40 = load i32*, i32** %4
%41 = load i32, i32* %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %39, i32 %41)
br label %43
43: ; preds = %33
%44 = load i32*, i32** %4
%45 = getelementptr inbounds i32, i32* %44, i32 1
store i32* %45, i32** %4
br label %26
46: ; preds = %26
ret void
}

LLVM IR (-O2):
@.str.2 = [12 x i8] c"arr[%d]\09%d\0A\00"
@str = [20 x i8] c"POINTER ARITHMETIC \00"
@str.3 = [13 x i8] c"------------\00"
define void @ptr_arithmetic() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.3, i64 0, i64 0))
%3 = tail call i64 @calloc(i32 11, i32 4)
%4 = inttoptr i64 %3 to i32*
%5 = getelementptr inbounds i32, i32* %4, i64 1
%6 = getelementptr inbounds i32, i32* %4, i64 2
%7 = getelementptr inbounds i32, i32* %4, i64 3
%8 = inttoptr i64 %3 to <4 x i32>*
store <4 x i32> <i32 0, i32 10, i32 20, i32 30>, <4 x i32>* %8
%9 = getelementptr inbounds i32, i32* %4, i64 4
%10 = getelementptr inbounds i32, i32* %4, i64 5
%11 = getelementptr inbounds i32, i32* %4, i64 6
%12 = getelementptr inbounds i32, i32* %4, i64 7
%13 = bitcast i32* %9 to <4 x i32>*
store <4 x i32> <i32 40, i32 50, i32 60, i32 70>, <4 x i32>* %13
%14 = getelementptr inbounds i32, i32* %4, i64 8
store i32 80, i32* %14
%15 = getelementptr inbounds i32, i32* %4, i64 9
store i32 90, i32* %15
%16 = getelementptr inbounds i32, i32* %4, i64 10
store i32 100, i32* %16
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 0, i32 0)
%18 = ptrtoint i32* %5 to i64
%19 = sub i64 %18, %3
%20 = ashr exact i64 %19, 2
%21 = load i32, i32* %5
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %20, i32 %21)
%23 = ptrtoint i32* %6 to i64
%24 = sub i64 %23, %3
%25 = ashr exact i64 %24, 2
%26 = load i32, i32* %6
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %25, i32 %26)
%28 = ptrtoint i32* %7 to i64
%29 = sub i64 %28, %3
%30 = ashr exact i64 %29, 2
%31 = load i32, i32* %7
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %30, i32 %31)
%33 = ptrtoint i32* %9 to i64
%34 = sub i64 %33, %3
%35 = ashr exact i64 %34, 2
%36 = load i32, i32* %9
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %35, i32 %36)
%38 = ptrtoint i32* %10 to i64
%39 = sub i64 %38, %3
%40 = ashr exact i64 %39, 2
%41 = load i32, i32* %10
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %40, i32 %41)
%43 = ptrtoint i32* %11 to i64
%44 = sub i64 %43, %3
%45 = ashr exact i64 %44, 2
%46 = load i32, i32* %11
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %45, i32 %46)
%48 = ptrtoint i32* %12 to i64
%49 = sub i64 %48, %3
%50 = ashr exact i64 %49, 2
%51 = load i32, i32* %12
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %50, i32 %51)
%53 = ptrtoint i32* %14 to i64
%54 = sub i64 %53, %3
%55 = ashr exact i64 %54, 2
%56 = load i32, i32* %14
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %55, i32 %56)
%58 = ptrtoint i32* %15 to i64
%59 = sub i64 %58, %3
%60 = ashr exact i64 %59, 2
%61 = load i32, i32* %15
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %60, i32 %61)
%63 = ptrtoint i32* %16 to i64
%64 = sub i64 %63, %3
%65 = ashr exact i64 %64, 2
%66 = load i32, i32* %16
%67 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %65, i32 %66)
ret void
}