; 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 }
%struct.list_fval = type { ptr, ptr, ptr, ptr, 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @l_copy_of(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.list, ptr %7, i32 0, i32 3
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.list, ptr %10, i32 0, i32 4
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = call ptr @l_new_custom(ptr noundef %9, ptr noundef %12, ptr noundef null)
  store ptr %13, ptr %4, align 8, !tbaa !5
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %19, label %16

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

19:                                               ; preds = %1
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.list, ptr %20, i32 0, i32 3
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %59

24:                                               ; preds = %19
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.list, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.list_fval, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %59

31:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !17
  br label %32

32:                                               ; preds = %55, %31
  %33 = load i64, ptr %6, align 8, !tbaa !17
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.list, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !18
  %37 = icmp ult i64 %33, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %32
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %58

39:                                               ; preds = %32
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.list, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.list_fval, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.list, ptr %45, i32 0, i32 0
  %47 = load i64, ptr %6, align 8, !tbaa !17
  %48 = getelementptr inbounds [100 x i32], ptr %46, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !19
  %50 = call i32 %44(i32 noundef %49)
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.list, ptr %51, i32 0, i32 0
  %53 = load i64, ptr %6, align 8, !tbaa !17
  %54 = getelementptr inbounds [100 x i32], ptr %52, i64 0, i64 %53
  store i32 %50, ptr %54, align 4, !tbaa !19
  br label %55

55:                                               ; preds = %39
  %56 = load i64, ptr %6, align 8, !tbaa !17
  %57 = add i64 %56, 1
  store i64 %57, ptr %6, align 8, !tbaa !17
  br label %32, !llvm.loop !20

58:                                               ; preds = %38
  br label %70

59:                                               ; preds = %24, %19
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.list, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds [100 x i32], ptr %61, i64 0, i64 0
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.list, ptr %63, i32 0, i32 0
  %65 = getelementptr inbounds [100 x i32], ptr %64, i64 0, i64 0
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.list, ptr %66, i32 0, i32 1
  %68 = load i64, ptr %67, align 8, !tbaa !18
  %69 = mul i64 4, %68
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %62, ptr align 8 %65, i64 %69, i1 false)
  br label %70

70:                                               ; preds = %59, %58
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.list, ptr %71, i32 0, i32 1
  %73 = load i64, ptr %72, align 8, !tbaa !18
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.list, ptr %74, i32 0, i32 1
  store i64 %73, ptr %75, align 8, !tbaa !18
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.list, ptr %76, i32 0, i32 2
  store i32 0, ptr %77, align 8, !tbaa !14
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %78, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %79

79:                                               ; preds = %70, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %80 = load ptr, ptr %2, align 8
  ret ptr %80
}

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

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: readwrite) }
attributes #3 = { 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, !6, i64 416}
!10 = !{!"list", !7, i64 0, !11, i64 400, !12, i64 408, !6, i64 416, !6, i64 424}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 424}
!14 = !{!10, !12, i64 408}
!15 = !{!16, !6, i64 8}
!16 = !{!"list_fval", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!17 = !{!11, !11, i64 0}
!18 = !{!10, !11, i64 400}
!19 = !{!12, !12, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; 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: nounwind uwtable
define dso_local ptr @l_copy_of(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 416
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 424
  %5 = load ptr, ptr %4, align 8, !tbaa !12
  %6 = tail call ptr @l_new_custom(ptr noundef %3, ptr noundef %5, ptr noundef null)
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %30, label %7

7:                                                ; preds = %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %.not24 = icmp eq ptr %8, null
  br i1 %.not24, label %24, label %9

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %8, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %.not25 = icmp eq ptr %11, null
  br i1 %.not25, label %24, label %.preheader

.preheader:                                       ; preds = %9
  %12 = getelementptr inbounds i8, ptr %0, i64 400
  %13 = load i64, ptr %12, align 8, !tbaa !15
  %.not27 = icmp eq i64 %13, 0
  br i1 %.not27, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.026 = phi i64 [ %21, %.lr.ph ], [ 0, %.preheader ]
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = getelementptr inbounds [100 x i32], ptr %0, i64 0, i64 %.026
  %18 = load i32, ptr %17, align 4, !tbaa !16
  %19 = tail call i32 %16(i32 noundef %18) #2
  %20 = getelementptr inbounds [100 x i32], ptr %6, i64 0, i64 %.026
  store i32 %19, ptr %20, align 4, !tbaa !16
  %21 = add nuw i64 %.026, 1
  %22 = load i64, ptr %12, align 8, !tbaa !15
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %.lr.ph, label %.loopexit, !llvm.loop !17

24:                                               ; preds = %9, %7
  %25 = getelementptr inbounds i8, ptr %0, i64 400
  %26 = load i64, ptr %25, align 8, !tbaa !15
  %27 = shl i64 %26, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %6, ptr nonnull align 8 %0, i64 %27, i1 false)
  %.pre = load i64, ptr %25, align 8, !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %24
  %28 = phi i64 [ 0, %.preheader ], [ %.pre, %24 ], [ %22, %.lr.ph ]
  %29 = getelementptr inbounds i8, ptr %6, i64 400
  store i64 %28, ptr %29, align 8, !tbaa !15
  br label %30

30:                                               ; preds = %1, %.loopexit
  %.sink = phi i32 [ 0, %.loopexit ], [ 1, %1 ]
  %31 = getelementptr inbounds i8, ptr %0, i64 408
  store i32 %.sink, ptr %31, align 8, !tbaa !19
  ret ptr %6
}

; 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) #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: readwrite) }
attributes #2 = { 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, !11, i64 416}
!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 424}
!13 = !{!14, !11, i64 8}
!14 = !{!"list_fval", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40}
!15 = !{!6, !9, i64 400}
!16 = !{!10, !10, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!6, !10, i64 408}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass