; LLVM IR from LeoVen/C-Macro-Collections :: examples (sac_example)

; 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.list = type { [100 x i32], i64, i32, ptr, ptr }

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; 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.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @l_seq_pop_at(ptr noundef %0, i64 noundef %1, i64 noundef %2) #3 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %9 = load i64, ptr %6, align 8, !tbaa !9
  %10 = load i64, ptr %7, align 8, !tbaa !9
  %11 = icmp ugt i64 %9, %10
  br i1 %11, label %12, label %15

12:                                               ; preds = %3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.list, ptr %13, i32 0, i32 2
  store i32 4, ptr %14, align 8, !tbaa !11
  store i1 false, ptr %4, align 1
  br label %69

15:                                               ; preds = %3
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.list, ptr %17, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = icmp uge i64 %16, %19
  br i1 %20, label %21, label %24

21:                                               ; preds = %15
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.list, ptr %22, i32 0, i32 2
  store i32 5, ptr %23, align 8, !tbaa !11
  store i1 false, ptr %4, align 1
  br label %69

24:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = sub i64 %25, %26
  %28 = add i64 %27, 1
  store i64 %28, ptr %8, align 8, !tbaa !9
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.list, ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds [100 x i32], ptr %30, i64 0, i64 0
  %32 = load i64, ptr %6, align 8, !tbaa !9
  %33 = getelementptr inbounds i32, ptr %31, i64 %32
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.list, ptr %34, i32 0, i32 0
  %36 = getelementptr inbounds [100 x i32], ptr %35, i64 0, i64 0
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = getelementptr inbounds i32, ptr %36, i64 %37
  %39 = getelementptr inbounds i32, ptr %38, i64 1
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.list, ptr %40, i32 0, i32 1
  %42 = load i64, ptr %41, align 8, !tbaa !14
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = sub i64 %42, %43
  %45 = sub i64 %44, 1
  %46 = mul i64 %45, 4
  call void @llvm.memmove.p0.p0.i64(ptr align 4 %33, ptr align 4 %39, i64 %46, i1 false)
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.list, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds [100 x i32], ptr %48, i64 0, i64 0
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.list, ptr %50, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !14
  %53 = getelementptr inbounds i32, ptr %49, i64 %52
  %54 = load i64, ptr %8, align 8, !tbaa !9
  %55 = sub i64 0, %54
  %56 = getelementptr inbounds i32, ptr %53, i64 %55
  %57 = load i64, ptr %8, align 8, !tbaa !9
  %58 = mul i64 %57, 4
  call void @llvm.memset.p0.i64(ptr align 4 %56, i8 0, i64 %58, i1 false)
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = load i64, ptr %6, align 8, !tbaa !9
  %61 = sub i64 %59, %60
  %62 = add i64 %61, 1
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.list, ptr %63, i32 0, i32 1
  %65 = load i64, ptr %64, align 8, !tbaa !14
  %66 = sub i64 %65, %62
  store i64 %66, ptr %64, align 8, !tbaa !14
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.list, ptr %67, i32 0, i32 2
  store i32 0, ptr %68, align 8, !tbaa !11
  store i1 true, ptr %4, align 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %69

69:                                               ; preds = %24, %21, %12
  %70 = load i1, ptr %4, align 1
  ret i1 %70
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!12, !13, i64 408}
!12 = !{!"list", !7, i64 0, !10, i64 400, !13, i64 408, !6, i64 416, !6, i64 424}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !10, i64 400}


; 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"

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local noundef zeroext i1 @l_seq_pop_at(ptr nocapture noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = icmp ugt i64 %1, %2
  br i1 %4, label %24, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 400
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %.not = icmp ugt i64 %7, %2
  br i1 %.not, label %8, label %24

8:                                                ; preds = %5
  %9 = sub i64 %2, %1
  %10 = add i64 %9, 1
  %11 = getelementptr inbounds i32, ptr %0, i64 %1
  %12 = getelementptr inbounds i32, ptr %0, i64 %2
  %13 = getelementptr inbounds i8, ptr %12, i64 4
  %14 = xor i64 %2, -1
  %15 = add i64 %7, %14
  %16 = shl i64 %15, 2
  tail call void @llvm.memmove.p0.p0.i64(ptr align 4 %11, ptr nonnull align 4 %13, i64 %16, i1 false)
  %17 = load i64, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds i32, ptr %0, i64 %17
  %19 = xor i64 %9, -1
  %20 = getelementptr inbounds i32, ptr %18, i64 %19
  %21 = shl i64 %10, 2
  tail call void @llvm.memset.p0.i64(ptr align 4 %20, i8 0, i64 %21, i1 false)
  %22 = load i64, ptr %6, align 8, !tbaa !5
  %23 = sub i64 %22, %10
  store i64 %23, ptr %6, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %5, %3, %8
  %.sink = phi i32 [ 0, %8 ], [ 4, %3 ], [ 5, %5 ]
  %.0 = phi i1 [ true, %8 ], [ false, %3 ], [ false, %5 ]
  %25 = getelementptr inbounds i8, ptr %0, i64 408
  store i32 %.sink, ptr %25, align 8, !tbaa !12
  ret i1 %.0
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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, !9, i64 400}
!6 = !{!"list", !7, i64 0, !9, i64 400, !10, i64 408, !11, i64 416, !11, i64 424}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!6, !10, i64 408}


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