; LLVM IR from devantler/software-engineering-f22-shared :: IOT/BudgetManagementExcercise-03-28/ESP-Project/components/components/nghttp (ref)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.mrb_value = type { %union.anon, i32 }
%union.anon = type { double }
%struct.RArray = type { i32, ptr, ptr, %union.anon.0 }
%union.anon.0 = type { %struct.anon }
%struct.anon = type { i64, %union.anon.1, ptr }
%union.anon.1 = type { i64 }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden { i64, i32 } @mrb_nil_value() #2

; Function Attrs: nounwind uwtable
define dso_local { i64, i32 } @mrb_ary_ref(ptr noundef %0, i64 %1, i32 %2, i64 noundef %3) #3 {
  %5 = alloca %struct.mrb_value, align 8
  %6 = alloca %struct.mrb_value, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = getelementptr inbounds { i64, i32 }, ptr %6, i32 0, i32 0
  store i64 %1, ptr %12, align 8
  %13 = getelementptr inbounds { i64, i32 }, ptr %6, i32 0, i32 1
  store i32 %2, ptr %13, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %14 = getelementptr inbounds %struct.mrb_value, ptr %6, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  store ptr %15, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = load i32, ptr %16, align 8
  %18 = lshr i32 %17, 11
  %19 = and i32 %18, 7
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %28

21:                                               ; preds = %4
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = load i32, ptr %22, align 8
  %24 = lshr i32 %23, 11
  %25 = and i32 %24, 7
  %26 = sub nsw i32 %25, 1
  %27 = sext i32 %26 to i64
  br label %33

28:                                               ; preds = %4
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.RArray, ptr %29, i32 0, i32 3
  %31 = getelementptr inbounds %struct.anon, ptr %30, i32 0, i32 0
  %32 = load i64, ptr %31, align 8, !tbaa !11
  br label %33

33:                                               ; preds = %28, %21
  %34 = phi i64 [ %27, %21 ], [ %32, %28 ]
  store i64 %34, ptr %10, align 8, !tbaa !9
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = icmp slt i64 %35, 0
  br i1 %36, label %37, label %41

37:                                               ; preds = %33
  %38 = load i64, ptr %10, align 8, !tbaa !9
  %39 = load i64, ptr %8, align 8, !tbaa !9
  %40 = add nsw i64 %39, %38
  store i64 %40, ptr %8, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %37, %33
  %42 = load i64, ptr %8, align 8, !tbaa !9
  %43 = icmp slt i64 %42, 0
  br i1 %43, label %48, label %44

44:                                               ; preds = %41
  %45 = load i64, ptr %10, align 8, !tbaa !9
  %46 = load i64, ptr %8, align 8, !tbaa !9
  %47 = icmp sle i64 %45, %46
  br i1 %47, label %48, label %54

48:                                               ; preds = %44, %41
  %49 = call { i64, i32 } @mrb_nil_value()
  %50 = getelementptr inbounds { i64, i32 }, ptr %5, i32 0, i32 0
  %51 = extractvalue { i64, i32 } %49, 0
  store i64 %51, ptr %50, align 8
  %52 = getelementptr inbounds { i64, i32 }, ptr %5, i32 0, i32 1
  %53 = extractvalue { i64, i32 } %49, 1
  store i32 %53, ptr %52, align 8
  store i32 1, ptr %11, align 4
  br label %73

54:                                               ; preds = %44
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = load i32, ptr %55, align 8
  %57 = lshr i32 %56, 11
  %58 = and i32 %57, 7
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %64

60:                                               ; preds = %54
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.RArray, ptr %61, i32 0, i32 3
  %63 = getelementptr inbounds [1 x %struct.mrb_value], ptr %62, i64 0, i64 0
  br label %69

64:                                               ; preds = %54
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.RArray, ptr %65, i32 0, i32 3
  %67 = getelementptr inbounds %struct.anon, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !11
  br label %69

69:                                               ; preds = %64, %60
  %70 = phi ptr [ %63, %60 ], [ %68, %64 ]
  %71 = load i64, ptr %8, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.mrb_value, ptr %70, i64 %71
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %5, ptr align 8 %72, i64 16, i1 false), !tbaa.struct !12
  store i32 1, ptr %11, align 4
  br label %73

73:                                               ; preds = %69, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %74 = load { i64, i32 }, ptr %5, align 8
  ret { i64, i32 } %74
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{i64 0, i64 8, !11, i64 8, i64 4, !13}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.mrb_value = type { %union.anon, i32 }
%union.anon = type { double }

; Function Attrs: inlinehint nounwind uwtable
declare hidden { i64, i32 } @mrb_nil_value() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local { i64, i32 } @mrb_ary_ref(ptr nocapture noundef readnone %0, i64 %1, i32 %2, i64 noundef %3) local_unnamed_addr #1 {
  %.sroa.0.0..sroa.0.0..cast = inttoptr i64 %1 to ptr
  %5 = load i32, ptr %.sroa.0.0..sroa.0.0..cast, align 8
  %6 = lshr i32 %5, 11
  %7 = and i32 %6, 7
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %11, label %8

8:                                                ; preds = %4
  %9 = add nsw i32 %7, -1
  %10 = zext nneg i32 %9 to i64
  br label %14

11:                                               ; preds = %4
  %12 = getelementptr inbounds i8, ptr %.sroa.0.0..sroa.0.0..cast, i64 24
  %13 = load i64, ptr %12, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %11, %8
  %15 = phi i64 [ %10, %8 ], [ %13, %11 ]
  %16 = icmp slt i64 %3, 0
  %17 = select i1 %16, i64 %15, i64 0
  %spec.select = add nsw i64 %17, %3
  %18 = icmp sgt i64 %spec.select, -1
  %.not15 = icmp sgt i64 %15, %spec.select
  %or.cond = select i1 %18, i1 %.not15, i1 false
  br i1 %or.cond, label %23, label %19

19:                                               ; preds = %14
  %20 = tail call { i64, i32 } @mrb_nil_value()
  %21 = extractvalue { i64, i32 } %20, 0
  %22 = extractvalue { i64, i32 } %20, 1
  br label %33

23:                                               ; preds = %14
  %24 = and i32 %5, 14336
  %.not16 = icmp eq i32 %24, 0
  br i1 %.not16, label %27, label %25

25:                                               ; preds = %23
  %26 = getelementptr inbounds i8, ptr %.sroa.0.0..sroa.0.0..cast, i64 24
  br label %30

27:                                               ; preds = %23
  %28 = getelementptr inbounds i8, ptr %.sroa.0.0..sroa.0.0..cast, i64 40
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %27, %25
  %31 = phi ptr [ %26, %25 ], [ %29, %27 ]
  %32 = getelementptr inbounds %struct.mrb_value, ptr %31, i64 %spec.select
  %.sroa.012.0.copyload = load i64, ptr %32, align 8, !tbaa !5
  %.sroa.3.0..sroa_idx = getelementptr inbounds i8, ptr %32, i64 8
  %.sroa.3.0.copyload = load i32, ptr %.sroa.3.0..sroa_idx, align 8, !tbaa !8
  br label %33

33:                                               ; preds = %30, %19
  %.sroa.012.0 = phi i64 [ %21, %19 ], [ %.sroa.012.0.copyload, %30 ]
  %.sroa.3.0 = phi i32 [ %22, %19 ], [ %.sroa.3.0.copyload, %30 ]
  %.fca.0.insert = insertvalue { i64, i32 } poison, i64 %.sroa.012.0, 0
  %.fca.1.insert = insertvalue { i64, i32 } %.fca.0.insert, i32 %.sroa.3.0, 1
  ret { i64, i32 } %.fca.1.insert
}

attributes #0 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass