; 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: nounwind uwtable
declare dso_local ptr @l_new_custom(ptr noundef, ptr noundef, ptr noundef) #0

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

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

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

; 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) #3

; 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) #3

; Function Attrs: nounwind uwtable
define dso_local ptr @l_seq_sublist(ptr noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  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
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = load i64, ptr %7, align 8, !tbaa !9
  %13 = icmp ugt i64 %11, %12
  br i1 %13, label %14, label %17

14:                                               ; preds = %3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.list, ptr %15, i32 0, i32 2
  store i32 4, ptr %16, align 8, !tbaa !11
  store ptr null, ptr %4, align 8
  br label %94

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

23:                                               ; preds = %17
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.list, ptr %24, i32 0, i32 2
  store i32 5, ptr %25, align 8, !tbaa !11
  store ptr null, ptr %4, align 8
  br label %94

26:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %27 = load i64, ptr %7, align 8, !tbaa !9
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = sub i64 %27, %28
  %30 = add i64 %29, 1
  store i64 %30, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.list, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.list, ptr %34, i32 0, i32 4
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  %37 = call ptr @l_new_custom(ptr noundef %33, ptr noundef %36, ptr noundef null)
  store ptr %37, ptr %9, align 8, !tbaa !5
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %43, label %40

40:                                               ; preds = %26
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.list, ptr %41, i32 0, i32 2
  store i32 1, ptr %42, align 8, !tbaa !11
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %93

43:                                               ; preds = %26
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.list, ptr %44, i32 0, i32 0
  %46 = getelementptr inbounds [100 x i32], ptr %45, i64 0, i64 0
  %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 i64, ptr %8, align 8, !tbaa !9
  %51 = mul i64 %50, 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %46, ptr align 8 %49, i64 %51, i1 false)
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.list, ptr %52, i32 0, i32 0
  %54 = getelementptr inbounds [100 x i32], ptr %53, i64 0, i64 0
  %55 = load i64, ptr %6, align 8, !tbaa !9
  %56 = getelementptr inbounds i32, ptr %54, i64 %55
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.list, ptr %57, i32 0, i32 0
  %59 = getelementptr inbounds [100 x i32], ptr %58, i64 0, i64 0
  %60 = load i64, ptr %7, align 8, !tbaa !9
  %61 = getelementptr inbounds i32, ptr %59, i64 %60
  %62 = getelementptr inbounds i32, ptr %61, i64 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 = load i64, ptr %7, align 8, !tbaa !9
  %67 = sub i64 %65, %66
  %68 = sub i64 %67, 1
  %69 = mul i64 %68, 4
  call void @llvm.memmove.p0.p0.i64(ptr align 4 %56, ptr align 4 %62, i64 %69, i1 false)
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.list, ptr %70, i32 0, i32 0
  %72 = getelementptr inbounds [100 x i32], ptr %71, i64 0, i64 0
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.list, ptr %73, i32 0, i32 1
  %75 = load i64, ptr %74, align 8, !tbaa !14
  %76 = getelementptr inbounds i32, ptr %72, i64 %75
  %77 = load i64, ptr %8, align 8, !tbaa !9
  %78 = sub i64 0, %77
  %79 = getelementptr inbounds i32, ptr %76, i64 %78
  %80 = load i64, ptr %8, align 8, !tbaa !9
  %81 = mul i64 %80, 4
  call void @llvm.memset.p0.i64(ptr align 4 %79, i8 0, i64 %81, i1 false)
  %82 = load i64, ptr %8, align 8, !tbaa !9
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.list, ptr %83, i32 0, i32 1
  %85 = load i64, ptr %84, align 8, !tbaa !14
  %86 = sub i64 %85, %82
  store i64 %86, ptr %84, align 8, !tbaa !14
  %87 = load i64, ptr %8, align 8, !tbaa !9
  %88 = load ptr, ptr %9, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.list, ptr %88, i32 0, i32 1
  store i64 %87, ptr %89, align 8, !tbaa !14
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.list, ptr %90, i32 0, i32 2
  store i32 0, ptr %91, align 8, !tbaa !11
  %92 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %92, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %93

93:                                               ; preds = %43, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %94

94:                                               ; preds = %93, %23, %14
  %95 = load ptr, ptr %4, align 8
  ret ptr %95
}

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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}
!15 = !{!12, !6, i64 416}
!16 = !{!12, !6, i64 424}


; 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: nounwind uwtable
declare dso_local ptr @l_new_custom(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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

; 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) #2

; Function Attrs: mustprogress 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) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @l_seq_sublist(ptr nocapture noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ugt i64 %1, %2
  br i1 %4, label %32, 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 %32

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 416
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %0, i64 424
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = tail call ptr @l_new_custom(ptr noundef %10, ptr noundef %12, ptr noundef null)
  %.not34 = icmp eq ptr %13, null
  br i1 %.not34, label %32, label %14

14:                                               ; preds = %8
  %15 = sub i64 %2, %1
  %16 = add i64 %15, 1
  %17 = shl i64 %16, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %13, ptr nonnull align 8 %0, i64 %17, i1 false)
  %18 = getelementptr inbounds i32, ptr %0, i64 %1
  %19 = getelementptr inbounds i32, ptr %0, i64 %2
  %20 = getelementptr inbounds i8, ptr %19, i64 4
  %21 = load i64, ptr %6, align 8, !tbaa !5
  %22 = xor i64 %2, -1
  %23 = add i64 %21, %22
  %24 = shl i64 %23, 2
  tail call void @llvm.memmove.p0.p0.i64(ptr align 4 %18, ptr nonnull align 4 %20, i64 %24, i1 false)
  %25 = load i64, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds i32, ptr %0, i64 %25
  %27 = xor i64 %15, -1
  %28 = getelementptr inbounds i32, ptr %26, i64 %27
  tail call void @llvm.memset.p0.i64(ptr align 4 %28, i8 0, i64 %17, i1 false)
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = sub i64 %29, %16
  store i64 %30, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %13, i64 400
  store i64 %16, ptr %31, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %8, %5, %3, %14
  %.sink = phi i32 [ 0, %14 ], [ 4, %3 ], [ 5, %5 ], [ 1, %8 ]
  %.1 = phi ptr [ %13, %14 ], [ null, %3 ], [ null, %5 ], [ null, %8 ]
  %33 = getelementptr inbounds i8, ptr %0, i64 408
  store i32 %.sink, ptr %33, align 8, !tbaa !14
  ret ptr %.1
}

attributes #0 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !11, i64 416}
!13 = !{!6, !11, i64 424}
!14 = !{!6, !10, i64 408}


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