; LLVM IR from fcimeson/cbTSP :: cbLKH/SRC (FreeStructures)

; 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.Segment = type { i8, ptr, ptr, ptr, ptr, i32, i32, ptr }
%struct.SSegment = type { i8, ptr, ptr, ptr, ptr, i32, i32 }

@FirstSegment = external dso_local global ptr, align 8
@FirstSSegment = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @FreeSegments() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = load ptr, ptr @FirstSegment, align 8, !tbaa !5
  %6 = icmp ne ptr %5, null
  br i1 %6, label %7, label %19

7:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %8 = load ptr, ptr @FirstSegment, align 8, !tbaa !5
  store ptr %8, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  br label %9

9:                                                ; preds = %14, %7
  %10 = load ptr, ptr %1, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %12, ptr %2, align 8, !tbaa !5
  %13 = load ptr, ptr %1, align 8, !tbaa !5
  call void @free(ptr noundef %13) #3
  store ptr null, ptr %1, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %9
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %15, ptr %1, align 8, !tbaa !5
  %16 = load ptr, ptr @FirstSegment, align 8, !tbaa !5
  %17 = icmp ne ptr %15, %16
  br i1 %17, label %9, label %18, !llvm.loop !12

18:                                               ; preds = %14
  store ptr null, ptr @FirstSegment, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %19

19:                                               ; preds = %18, %0
  %20 = load ptr, ptr @FirstSSegment, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %34

22:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %23 = load ptr, ptr @FirstSSegment, align 8, !tbaa !5
  store ptr %23, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  br label %24

24:                                               ; preds = %29, %22
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.SSegment, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  store ptr %27, ptr %4, align 8, !tbaa !5
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %28) #3
  store ptr null, ptr %3, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %24
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %30, ptr %3, align 8, !tbaa !5
  %31 = load ptr, ptr @FirstSSegment, align 8, !tbaa !5
  %32 = icmp ne ptr %30, %31
  br i1 %32, label %24, label %33, !llvm.loop !16

33:                                               ; preds = %29
  store ptr null, ptr @FirstSSegment, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %34

34:                                               ; preds = %33, %19
  ret void
}

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind "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 }

!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 24}
!10 = !{!"Segment", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 44, !6, i64 48}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 24}
!15 = !{!"SSegment", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 44}
!16 = distinct !{!16, !13}


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

@FirstSegment = external dso_local local_unnamed_addr global ptr, align 8
@FirstSSegment = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @FreeSegments() local_unnamed_addr #0 {
  %1 = load ptr, ptr @FirstSegment, align 8, !tbaa !5
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %6, label %.preheader12

.preheader12:                                     ; preds = %0, %.preheader12
  %.0 = phi ptr [ %3, %.preheader12 ], [ %1, %0 ]
  %2 = getelementptr inbounds i8, ptr %.0, i64 24
  %3 = load ptr, ptr %2, align 8, !tbaa !9
  tail call void @free(ptr noundef %.0) #2
  %4 = load ptr, ptr @FirstSegment, align 8, !tbaa !5
  %.not9 = icmp eq ptr %3, %4
  br i1 %.not9, label %5, label %.preheader12, !llvm.loop !12

5:                                                ; preds = %.preheader12
  store ptr null, ptr @FirstSegment, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %5, %0
  %7 = load ptr, ptr @FirstSSegment, align 8, !tbaa !5
  %.not10 = icmp eq ptr %7, null
  br i1 %.not10, label %12, label %.preheader

.preheader:                                       ; preds = %6, %.preheader
  %.06 = phi ptr [ %9, %.preheader ], [ %7, %6 ]
  %8 = getelementptr inbounds i8, ptr %.06, i64 24
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  tail call void @free(ptr noundef %.06) #2
  %10 = load ptr, ptr @FirstSSegment, align 8, !tbaa !5
  %.not11 = icmp eq ptr %9, %10
  br i1 %.not11, label %11, label %.preheader, !llvm.loop !16

11:                                               ; preds = %.preheader
  store ptr null, ptr @FirstSSegment, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %11, %6
  ret void
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 24}
!10 = !{!"Segment", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 44, !6, i64 48}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 24}
!15 = !{!"SSegment", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 44}
!16 = distinct !{!16, !13}


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