#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 */
struct sparse_array {
  int dummy;
};
typedef int /*<<< orphan*/ i;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sparse_array_free(struct sparse_array *);
scalar_t__ sparse_array_get(struct sparse_array *, int, size_t *);
struct sparse_array *sparse_array_init(int, int const);
int /*<<< orphan*/ sparse_array_set(struct sparse_array *, int, int *, int);
int test_array_set_backwards() {
  int i;
  const int array_size = 120;
  struct sparse_array *arr = NULL;
  arr = sparse_array_init(sizeof(int), array_size);
  if (!(arr)) {
    printf("%i: ", 78);
    return 0;
  };
  for (i = array_size - 1; i >= 0; i--) {
    int *returned = NULL;
    size_t siz = 0;
    if (!(sparse_array_set(arr, i, &i, sizeof(i)))) {
      printf("%i: ", 83);
      return 0;
    };
    returned = (int *)sparse_array_get(arr, i, &siz);
    if (!(returned)) {
      printf("%i: ", 85);
      return 0;
    };
    if (!(*returned == i)) {
      printf("%i: ", 86);
      return 0;
    };
    if (!(siz == sizeof(int))) {
      printf("%i: ", 87);
      return 0;
    };
  }
  for (i = array_size - 1; i >= 0; i--) {
    int *returned = NULL;
    size_t siz = 0;
    returned = (int *)sparse_array_get(arr, i, &siz);
    if (!(*returned == i)) {
      printf("%i: ", 94);
      return 0;
    };
    if (!(siz == sizeof(int))) {
      printf("%i: ", 95);
      return 0;
    };
  }
  if (!(sparse_array_free(arr))) {
    printf("%i: ", 98);
    return 0;
  };
  return 1;
}


x86 assembly (clang, -O0):
<test_array_set_backwards>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x78,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x4,%edi
mov    $0x78,%esi
call   1070 <sparse_array_init@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a2 <test_array_set_backwards+0x52>
lea    0xe74(%rip),%rdi
mov    $0x4e,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
movl   $0x77,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     12a1 <test_array_set_backwards+0x151>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
lea    -0x8(%rbp),%rdx
mov    $0x4,%ecx
call   1030 <sparse_array_set@plt>
cmp    $0x0,%eax
jne    11fe <test_array_set_backwards+0xae>
lea    0xe18(%rip),%rdi
mov    $0x53,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1060 <sparse_array_get@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123a <test_array_set_backwards+0xea>
lea    0xddc(%rip),%rdi
mov    $0x55,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
je     1266 <test_array_set_backwards+0x116>
lea    0xdb0(%rip),%rdi
mov    $0x56,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
cmpq   $0x4,-0x28(%rbp)
je     128e <test_array_set_backwards+0x13e>
lea    0xd88(%rip),%rdi
mov    $0x57,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
jmp    1293 <test_array_set_backwards+0x143>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    11a9 <test_array_set_backwards+0x59>
movl   $0x77,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     133d <test_array_set_backwards+0x1ed>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
lea    -0x38(%rbp),%rdx
call   1060 <sparse_array_get@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
je     1302 <test_array_set_backwards+0x1b2>
lea    0xd14(%rip),%rdi
mov    $0x5e,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
cmpq   $0x4,-0x38(%rbp)
je     132a <test_array_set_backwards+0x1da>
lea    0xcec(%rip),%rdi
mov    $0x5f,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
jmp    132f <test_array_set_backwards+0x1df>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    12a8 <test_array_set_backwards+0x158>
mov    -0x18(%rbp),%rdi
call   1050 <sparse_array_free@plt>
cmp    $0x0,%eax
jne    136c <test_array_set_backwards+0x21c>
lea    0xcaa(%rip),%rdi
mov    $0x62,%esi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <test_array_set_backwards+0x223>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_array_set_backwards>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x4,%edi
mov    $0x78,%esi
call   1070 <sparse_array_init@plt>
test   %rax,%rax
je     1254 <test_array_set_backwards+0x104>
mov    %rax,%rbx
movl   $0x77,0x4(%rsp)
mov    $0x77,%esi
lea    0x4(%rsp),%r14
lea    0x8(%rsp),%r15
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r14,%rdx
mov    $0x4,%ecx
call   1030 <sparse_array_set@plt>
test   %eax,%eax
je     1262 <test_array_set_backwards+0x112>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1060 <sparse_array_get@plt>
test   %rax,%rax
je     1270 <test_array_set_backwards+0x120>
mov    (%rax),%eax
cmp    0x4(%rsp),%eax
jne    127e <test_array_set_backwards+0x12e>
cmpq   $0x4,0x8(%rsp)
jne    128c <test_array_set_backwards+0x13c>
lea    -0x1(%rax),%esi
mov    %esi,0x4(%rsp)
test   %eax,%eax
jg     1190 <test_array_set_backwards+0x40>
movl   $0x77,0x4(%rsp)
mov    $0x77,%esi
lea    0x8(%rsp),%r14
xchg   %ax,%ax
movq   $0x0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <sparse_array_get@plt>
mov    (%rax),%eax
cmp    0x4(%rsp),%eax
jne    129a <test_array_set_backwards+0x14a>
cmpq   $0x4,0x8(%rsp)
jne    12a8 <test_array_set_backwards+0x158>
lea    -0x1(%rax),%esi
mov    %esi,0x4(%rsp)
test   %eax,%eax
jg     1200 <test_array_set_backwards+0xb0>
mov    %rbx,%rdi
call   1050 <sparse_array_free@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    12bb <test_array_set_backwards+0x16b>
lea    0xdb3(%rip),%rdi
mov    $0x62,%esi
jmp    12b4 <test_array_set_backwards+0x164>
lea    0xda5(%rip),%rdi
mov    $0x4e,%esi
jmp    12b4 <test_array_set_backwards+0x164>
lea    0xd97(%rip),%rdi
mov    $0x53,%esi
jmp    12b4 <test_array_set_backwards+0x164>
lea    0xd89(%rip),%rdi
mov    $0x55,%esi
jmp    12b4 <test_array_set_backwards+0x164>
lea    0xd7b(%rip),%rdi
mov    $0x56,%esi
jmp    12b4 <test_array_set_backwards+0x164>
lea    0xd6d(%rip),%rdi
mov    $0x57,%esi
jmp    12b4 <test_array_set_backwards+0x164>
lea    0xd5f(%rip),%rdi
mov    $0x5e,%esi
jmp    12b4 <test_array_set_backwards+0x164>
lea    0xd51(%rip),%rdi
mov    $0x5f,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sparse_array = type { i32 }
@.str = [5 x i8] c"%i: \00"
define i32 @test_array_set_backwards() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sparse_array*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
store i32 120, i32* %3
store %struct.sparse_array* null, %struct.sparse_array** %4
%9 = call %struct.sparse_array* @sparse_array_init(i32 4, i32 120)
store %struct.sparse_array* %9, %struct.sparse_array** %4
%10 = load %struct.sparse_array*, %struct.sparse_array** %4
%11 = icmp ne %struct.sparse_array* %10, null
br i1 %11, label %14, label %12
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 78)
store i32 0, i32* %1
br label %81
14: ; preds = %0
store i32 119, i32* %2
br label %15
15: ; preds = %47, %14
%16 = load i32, i32* %2
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %50
18: ; preds = %15
store i32* null, i32** %5
store i64 0, i64* %6
%19 = load %struct.sparse_array*, %struct.sparse_array** %4
%20 = load i32, i32* %2
%21 = call i32 @sparse_array_set(%struct.sparse_array* %19, i32 %20, i32* %2, i32 4)
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 83)
store i32 0, i32* %1
br label %81
25: ; preds = %18
%26 = load %struct.sparse_array*, %struct.sparse_array** %4
%27 = load i32, i32* %2
%28 = call i64 @sparse_array_get(%struct.sparse_array* %26, i32 %27, i64* %6)
%29 = inttoptr i64 %28 to i32*
store i32* %29, i32** %5
%30 = load i32*, i32** %5
%31 = icmp ne i32* %30, null
br i1 %31, label %34, label %32
32: ; preds = %25
%33 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 85)
store i32 0, i32* %1
br label %81
34: ; preds = %25
%35 = load i32*, i32** %5
%36 = load i32, i32* %35
%37 = load i32, i32* %2
%38 = icmp eq i32 %36, %37
br i1 %38, label %41, label %39
39: ; preds = %34
%40 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 86)
store i32 0, i32* %1
br label %81
41: ; preds = %34
%42 = load i64, i64* %6
%43 = icmp eq i64 %42, 4
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 87)
store i32 0, i32* %1
br label %81
46: ; preds = %41
br label %47
47: ; preds = %46
%48 = load i32, i32* %2
%49 = add nsw i32 %48, -1
store i32 %49, i32* %2
br label %15
50: ; preds = %15
store i32 119, i32* %2
br label %51
51: ; preds = %71, %50
%52 = load i32, i32* %2
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %74
54: ; preds = %51
store i32* null, i32** %7
store i64 0, i64* %8
%55 = load %struct.sparse_array*, %struct.sparse_array** %4
%56 = load i32, i32* %2
%57 = call i64 @sparse_array_get(%struct.sparse_array* %55, i32 %56, i64* %8)
%58 = inttoptr i64 %57 to i32*
store i32* %58, i32** %7
%59 = load i32*, i32** %7
%60 = load i32, i32* %59
%61 = load i32, i32* %2
%62 = icmp eq i32 %60, %61
br i1 %62, label %65, label %63
63: ; preds = %54
%64 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 94)
store i32 0, i32* %1
br label %81
65: ; preds = %54
%66 = load i64, i64* %8
%67 = icmp eq i64 %66, 4
br i1 %67, label %70, label %68
68: ; preds = %65
%69 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 95)
store i32 0, i32* %1
br label %81
70: ; preds = %65
br label %71
71: ; preds = %70
%72 = load i32, i32* %2
%73 = add nsw i32 %72, -1
store i32 %73, i32* %2
br label %51
74: ; preds = %51
%75 = load %struct.sparse_array*, %struct.sparse_array** %4
%76 = call i32 @sparse_array_free(%struct.sparse_array* %75)
%77 = icmp ne i32 %76, 0
br i1 %77, label %80, label %78
78: ; preds = %74
%79 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 98)
store i32 0, i32* %1
br label %81
80: ; preds = %74
store i32 1, i32* %1
br label %81
81: ; preds = %80, %78, %68, %63, %44, %39, %32, %23, %12
%82 = load i32, i32* %1
ret i32 %82
}

LLVM IR (-O2):
%struct.sparse_array = type { i32 }
@.str = [5 x i8] c"%i: \00"
define i32 @test_array_set_backwards() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i32* %1 to i8*
%5 = tail call %struct.sparse_array* @sparse_array_init(i32 4, i32 120)
%6 = icmp eq %struct.sparse_array* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = bitcast i64* %2 to i8*
store i32 119, i32* %1
br label %13
9: ; preds = %0
%10 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 78)
br label %62
11: ; preds = %37
%12 = bitcast i64* %3 to i8*
store i32 119, i32* %1
br label %40
13: ; preds = %7, %37
%14 = phi i32 [ 119, %7 ], [ %38, %37 ]
store i64 0, i64* %2
%15 = call i32 @sparse_array_set(%struct.sparse_array* %5, i32 %14, i32* %1, i32 4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 83)
br label %62
19: ; preds = %13
%20 = load i32, i32* %1
%21 = call i64 @sparse_array_get(%struct.sparse_array* %5, i32 %20, i64* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 85)
br label %62
25: ; preds = %19
%26 = inttoptr i64 %21 to i32*
%27 = load i32, i32* %26
%28 = load i32, i32* %1
%29 = icmp eq i32 %27, %28
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 86)
br label %62
32: ; preds = %25
%33 = load i64, i64* %2
%34 = icmp eq i64 %33, 4
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 87)
br label %62
37: ; preds = %32
%38 = add nsw i32 %27, -1
store i32 %38, i32* %1
%39 = icmp sgt i32 %27, 0
br i1 %39, label %13, label %11
40: ; preds = %11, %54
%41 = phi i32 [ 119, %11 ], [ %55, %54 ]
store i64 0, i64* %3
%42 = call i64 @sparse_array_get(%struct.sparse_array* %5, i32 %41, i64* %3)
%43 = inttoptr i64 %42 to i32*
%44 = load i32, i32* %43
%45 = load i32, i32* %1
%46 = icmp eq i32 %44, %45
br i1 %46, label %49, label %47
47: ; preds = %40
%48 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 94)
br label %62
49: ; preds = %40
%50 = load i64, i64* %3
%51 = icmp eq i64 %50, 4
br i1 %51, label %54, label %52
52: ; preds = %49
%53 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 95)
br label %62
54: ; preds = %49
%55 = add nsw i32 %44, -1
store i32 %55, i32* %1
%56 = icmp sgt i32 %44, 0
br i1 %56, label %40, label %57
57: ; preds = %54
%58 = call i32 @sparse_array_free(%struct.sparse_array* %5)
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 98)
br label %62
62: ; preds = %57, %47, %52, %17, %23, %30, %35, %60, %9
%63 = phi i32 [ 0, %60 ], [ 0, %9 ], [ 0, %35 ], [ 0, %30 ], [ 0, %23 ], [ 0, %17 ], [ 0, %52 ], [ 0, %47 ], [ 1, %57 ]
ret i32 %63
}