#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*/ flipBit(void *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ writeValue(char *, void *);
void ex3(double *A, int n, int ch) {
  srand(time(NULL));
  double *addr = A;
  int tidx = n / 2;
  for (int i = 1; i < n; i++) {
    if (ch == 1 && tidx == i) {
      flipBit((void *)&i, 4);
    }
    int id = 2 * i - 2;
    if (ch == 2 && tidx == i) {
      flipBit((void *)&id, 4);
    }
    addr = &A[i];
    if (ch == 3 && tidx == i) {
      flipBit((void *)&addr, 8);
    }
    if (ch == 1)
      writeValue("ex2_var_addr_fi_1.csv", (void *)&addr);
    if (ch == 2)
      writeValue("ex2_var_addr_fi_2.csv", (void *)&addr);
    if (ch == 3)
      writeValue("ex2_var_addr_fi_3.csv", (void *)&addr);
    if (ch == 0)
      writeValue("ex2_var_addr.csv", (void *)&addr);
    *addr = i;
  }
}


x86 assembly (clang, -O0):
<ex3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1299 <ex3+0x159>
cmpl   $0x1,-0x10(%rbp)
jne    11af <ex3+0x6f>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    11af <ex3+0x6f>
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1060 <flipBit@plt>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
sub    $0x2,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x2,-0x10(%rbp)
jne    11df <ex3+0x9f>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    11df <ex3+0x9f>
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
call   1060 <flipBit@plt>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x3,-0x10(%rbp)
jne    1216 <ex3+0xd6>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1216 <ex3+0xd6>
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1060 <flipBit@plt>
cmpl   $0x1,-0x10(%rbp)
jne    1230 <ex3+0xf0>
lea    -0x18(%rbp),%rsi
lea    0xdd5(%rip),%rdi
call   1030 <writeValue@plt>
cmpl   $0x2,-0x10(%rbp)
jne    124a <ex3+0x10a>
lea    -0x18(%rbp),%rsi
lea    0xdd1(%rip),%rdi
call   1030 <writeValue@plt>
cmpl   $0x3,-0x10(%rbp)
jne    1264 <ex3+0x124>
lea    -0x18(%rbp),%rsi
lea    0xdcd(%rip),%rdi
call   1030 <writeValue@plt>
cmpl   $0x0,-0x10(%rbp)
jne    127e <ex3+0x13e>
lea    -0x18(%rbp),%rsi
lea    0xdc9(%rip),%rdi
call   1030 <writeValue@plt>
cvtsi2sdl -0x20(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    117f <ex3+0x3f>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ex3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%r13d
mov    %rdi,%r14
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    %r14,0x8(%rsp)
movl   $0x1,0x4(%rsp)
cmp    $0x2,%r13d
jl     12ca <ex3+0x18a>
mov    %r13d,%ebp
shr    $0x1f,%ebp
add    %r13d,%ebp
sar    %ebp
cmp    $0x3,%ebx
sete   0x3(%rsp)
mov    $0x1,%eax
lea    0x8(%rsp),%r15
jmp    11cc <ex3+0x8c>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rsi
call   1030 <writeValue@plt>
mov    0x4(%rsp),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mov    0x8(%rsp),%rcx
movsd  %xmm0,(%rcx)
add    $0x1,%eax
mov    %eax,0x4(%rsp)
cmp    %r13d,%eax
jge    12ca <ex3+0x18a>
cmp    $0x1,%ebx
jne    11e8 <ex3+0xa8>
cmp    %eax,%ebp
jne    11e8 <ex3+0xa8>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
call   1060 <flipBit@plt>
mov    0x4(%rsp),%eax
lea    (%rax,%rax,1),%ecx
add    $0xfffffffe,%ecx
mov    %ecx,0x14(%rsp)
cmp    $0x2,%ebx
jne    1220 <ex3+0xe0>
cmp    %eax,%ebp
jne    1220 <ex3+0xe0>
lea    0x14(%rsp),%rdi
mov    $0x4,%esi
call   1060 <flipBit@plt>
movslq 0x4(%rsp),%rax
lea    (%r14,%rax,8),%rax
mov    %rax,0x8(%rsp)
xor    %r12d,%r12d
jmp    1255 <ex3+0x115>
nopl   (%rax)
movslq %eax,%rcx
lea    (%r14,%rcx,8),%rcx
mov    %rcx,0x8(%rsp)
movzbl 0x3(%rsp),%ecx
mov    %ecx,%r12d
cmp    $0x3,%ebx
jne    1255 <ex3+0x115>
movzbl 0x3(%rsp),%ecx
mov    %ecx,%r12d
cmp    %eax,%ebp
jne    1255 <ex3+0x115>
mov    %r15,%rdi
mov    $0x8,%esi
call   1060 <flipBit@plt>
mov    $0x1,%r12b
cmp    $0x1,%ebx
jne    1269 <ex3+0x129>
lea    0xd9f(%rip),%rdi
mov    %r15,%rsi
call   1030 <writeValue@plt>
cmp    $0x2,%ebx
jne    12a0 <ex3+0x160>
lea    0xda1(%rip),%rdi
mov    %r15,%rsi
call   1030 <writeValue@plt>
lea    0xda8(%rip),%rdi
test   %r12b,%r12b
jne    11a0 <ex3+0x60>
jmp    11a8 <ex3+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
sete   %al
or     %r12b,%al
test   %r12b,%r12b
lea    0xd90(%rip),%rdi
lea    0xd73(%rip),%rcx
cmovne %rcx,%rdi
test   %al,%al
jne    11a0 <ex3+0x60>
jmp    11a8 <ex3+0x68>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"ex2_var_addr_fi_1.csv\00"
@.str.1 = [22 x i8] c"ex2_var_addr_fi_2.csv\00"
@.str.2 = [22 x i8] c"ex2_var_addr_fi_3.csv\00"
@.str.3 = [17 x i8] c"ex2_var_addr.csv\00"
define void @ex3(double* %0, i32 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store double* %0, double** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = call i32 @time(i32* null)
%12 = call i32 @srand(i32 %11)
%13 = load double*, double** %4
store double* %13, double** %7
%14 = load i32, i32* %5
%15 = sdiv i32 %14, 2
store i32 %15, i32* %8
store i32 1, i32* %9
br label %16
16: ; preds = %85, %3
%17 = load i32, i32* %9
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %88
20: ; preds = %16
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i32, i32* %8
%25 = load i32, i32* %9
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = bitcast i32* %9 to i8*
%29 = call i32 @flipBit(i8* %28, i32 4)
br label %30
30: ; preds = %27, %23, %20
%31 = load i32, i32* %9
%32 = mul nsw i32 2, %31
%33 = sub nsw i32 %32, 2
store i32 %33, i32* %10
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = bitcast i32* %10 to i8*
%42 = call i32 @flipBit(i8* %41, i32 4)
br label %43
43: ; preds = %40, %36, %30
%44 = load double*, double** %4
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %44, i64 %46
store double* %47, double** %7
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 3
br i1 %49, label %50, label %57
50: ; preds = %43
%51 = load i32, i32* %8
%52 = load i32, i32* %9
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = bitcast double** %7 to i8*
%56 = call i32 @flipBit(i8* %55, i32 8)
br label %57
57: ; preds = %54, %50, %43
%58 = load i32, i32* %6
%59 = icmp eq i32 %58, 1
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = bitcast double** %7 to i8*
%62 = call i32 @writeValue(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %60, %57
%64 = load i32, i32* %6
%65 = icmp eq i32 %64, 2
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = bitcast double** %7 to i8*
%68 = call i32 @writeValue(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %63
%70 = load i32, i32* %6
%71 = icmp eq i32 %70, 3
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = bitcast double** %7 to i8*
%74 = call i32 @writeValue(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %72, %69
%76 = load i32, i32* %6
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = bitcast double** %7 to i8*
%80 = call i32 @writeValue(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %78, %75
%82 = load i32, i32* %9
%83 = sitofp i32 %82 to double
%84 = load double*, double** %7
store double %83, double* %84
br label %85
85: ; preds = %81
%86 = load i32, i32* %9
%87 = add nsw i32 %86, 1
store i32 %87, i32* %9
br label %16
88: ; preds = %16
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"ex2_var_addr_fi_1.csv\00"
@.str.1 = [22 x i8] c"ex2_var_addr_fi_2.csv\00"
@.str.2 = [22 x i8] c"ex2_var_addr_fi_3.csv\00"
@.str.3 = [17 x i8] c"ex2_var_addr.csv\00"
define void @ex3(double* %0, i32 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = tail call i32 @time(i32* null)
%8 = tail call i32 @srand(i32 %7)
%9 = bitcast double** %4 to i8*
store double* %0, double** %4
%10 = sdiv i32 %1, 2
%11 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%12 = icmp sgt i32 %1, 1
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = icmp eq i32 %2, 1
%15 = bitcast i32* %6 to i8*
%16 = icmp eq i32 %2, 2
%17 = icmp eq i32 %2, 3
%18 = icmp eq i32 %2, 0
br label %20
19: ; preds = %57, %3
ret void
20: ; preds = %13, %57
%21 = phi i32 [ 1, %13 ], [ %61, %57 ]
%22 = icmp eq i32 %10, %21
%23 = select i1 %14, i1 %22, i1 false
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @flipBit(i8* %11, i32 4)
%26 = load i32, i32* %5
br label %27
27: ; preds = %24, %20
%28 = phi i32 [ %26, %24 ], [ %21, %20 ]
%29 = shl nsw i32 %28, 1
%30 = add nsw i32 %29, -2
store i32 %30, i32* %6
%31 = icmp eq i32 %10, %28
%32 = select i1 %16, i1 %31, i1 false
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = call i32 @flipBit(i8* %15, i32 4)
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds double, double* %0, i64 %36
store double* %37, double** %4
br label %44
38: ; preds = %27
%39 = sext i32 %28 to i64
%40 = getelementptr inbounds double, double* %0, i64 %39
store double* %40, double** %4
%41 = select i1 %17, i1 %31, i1 false
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @flipBit(i8* %9, i32 8)
br label %44
44: ; preds = %33, %42, %38
%45 = phi i1 [ false, %33 ], [ true, %42 ], [ %17, %38 ]
br i1 %14, label %46, label %48
46: ; preds = %44
%47 = call i32 @writeValue(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %48
48: ; preds = %46, %44
br i1 %16, label %52, label %49
49: ; preds = %48
%50 = or i1 %45, %18
%51 = select i1 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0)
br i1 %50, label %54, label %57
52: ; preds = %48
%53 = call i32 @writeValue(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br i1 %45, label %54, label %57
54: ; preds = %49, %52
%55 = phi i8* [ %51, %49 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %52 ]
%56 = call i32 @writeValue(i8* %55, i8* %9)
br label %57
57: ; preds = %49, %54, %52
%58 = load i32, i32* %5
%59 = sitofp i32 %58 to double
%60 = load double*, double** %4
store double %59, double* %60
%61 = add nsw i32 %58, 1
store i32 %61, i32* %5
%62 = icmp slt i32 %61, %1
br i1 %62, label %20, label %19
}