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

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

@Swaps = external dso_local global i32, align 4
@SwapStack = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @RestoreTour() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  br label %5

5:                                                ; preds = %8, %0
  %6 = load i32, ptr @Swaps, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %8, label %56

8:                                                ; preds = %5
  %9 = load i32, ptr @Swaps, align 4, !tbaa !5
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr @Swaps, align 4, !tbaa !5
  %11 = load ptr, ptr @SwapStack, align 8, !tbaa !9
  %12 = load i32, ptr @Swaps, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds %struct.SwapRecord, ptr %11, i64 %13
  %15 = getelementptr inbounds %struct.SwapRecord, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  store ptr %16, ptr %1, align 8, !tbaa !9
  %17 = load ptr, ptr @SwapStack, align 8, !tbaa !9
  %18 = load i32, ptr @Swaps, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds %struct.SwapRecord, ptr %17, i64 %19
  %21 = getelementptr inbounds %struct.SwapRecord, ptr %20, i32 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  store ptr %22, ptr %2, align 8, !tbaa !9
  %23 = load ptr, ptr @SwapStack, align 8, !tbaa !9
  %24 = load i32, ptr @Swaps, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.SwapRecord, ptr %23, i64 %25
  %27 = getelementptr inbounds %struct.SwapRecord, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  store ptr %28, ptr %3, align 8, !tbaa !9
  %29 = load ptr, ptr @SwapStack, align 8, !tbaa !9
  %30 = load i32, ptr @Swaps, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds %struct.SwapRecord, ptr %29, i64 %31
  %33 = getelementptr inbounds %struct.SwapRecord, ptr %32, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !15
  store ptr %34, ptr %4, align 8, !tbaa !9
  %35 = load ptr, ptr %3, align 8, !tbaa !9
  %36 = load ptr, ptr %2, align 8, !tbaa !9
  %37 = load ptr, ptr %1, align 8, !tbaa !9
  call void @Flip_SL(ptr noundef %35, ptr noundef %36, ptr noundef %37)
  %38 = load i32, ptr @Swaps, align 4, !tbaa !5
  %39 = add nsw i32 %38, -1
  store i32 %39, ptr @Swaps, align 4, !tbaa !5
  %40 = load ptr, ptr %1, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.Node, ptr %40, i32 0, i32 54
  store i8 0, ptr %41, align 2, !tbaa !16
  %42 = load ptr, ptr %1, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.Node, ptr %42, i32 0, i32 53
  store i8 0, ptr %43, align 1, !tbaa !19
  %44 = load ptr, ptr %2, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.Node, ptr %44, i32 0, i32 54
  store i8 0, ptr %45, align 2, !tbaa !16
  %46 = load ptr, ptr %2, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 53
  store i8 0, ptr %47, align 1, !tbaa !19
  %48 = load ptr, ptr %3, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.Node, ptr %48, i32 0, i32 54
  store i8 0, ptr %49, align 2, !tbaa !16
  %50 = load ptr, ptr %3, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.Node, ptr %50, i32 0, i32 53
  store i8 0, ptr %51, align 1, !tbaa !19
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.Node, ptr %52, i32 0, i32 54
  store i8 0, ptr %53, align 2, !tbaa !16
  %54 = load ptr, ptr %4, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.Node, ptr %54, i32 0, i32 53
  store i8 0, ptr %55, align 1, !tbaa !19
  br label %5, !llvm.loop !20

56:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

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

declare void @Flip_SL(ptr noundef, ptr noundef, 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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"SwapRecord", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !10, i64 24}
!16 = !{!17, !7, i64 362}
!17 = !{!"Node", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !6, i64 48, !6, i64 52, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !10, i64 128, !10, i64 136, !10, i64 144, !10, i64 152, !10, i64 160, !10, i64 168, !10, i64 176, !10, i64 184, !10, i64 192, !10, i64 200, !10, i64 208, !10, i64 216, !10, i64 224, !10, i64 232, !10, i64 240, !10, i64 248, !10, i64 256, !10, i64 264, !10, i64 272, !10, i64 280, !10, i64 288, !10, i64 296, !10, i64 304, !18, i64 312, !18, i64 320, !18, i64 328, !18, i64 336, !18, i64 344, !18, i64 352, !7, i64 360, !7, i64 361, !7, i64 362}
!18 = !{!"double", !7, i64 0}
!19 = !{!17, !7, i64 361}
!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"

%struct.SwapRecord = type { ptr, ptr, ptr, ptr }

@Swaps = external dso_local local_unnamed_addr global i32, align 4
@SwapStack = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @RestoreTour() local_unnamed_addr #0 {
  %1 = load i32, ptr @Swaps, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %3 = phi i32 [ %16, %.lr.ph ], [ %1, %0 ]
  %4 = add nsw i32 %3, -1
  store i32 %4, ptr @Swaps, align 4, !tbaa !5
  %5 = load ptr, ptr @SwapStack, align 8, !tbaa !9
  %6 = zext nneg i32 %4 to i64
  %7 = getelementptr inbounds %struct.SwapRecord, ptr %5, i64 %6
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %7, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %7, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %7, i64 24
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  tail call void @Flip_SL(ptr noundef %12, ptr noundef %10, ptr noundef %8) #2
  %15 = load i32, ptr @Swaps, align 4, !tbaa !5
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr @Swaps, align 4, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %8, i64 362
  store i8 0, ptr %17, align 2, !tbaa !16
  %18 = getelementptr inbounds i8, ptr %8, i64 361
  store i8 0, ptr %18, align 1, !tbaa !19
  %19 = getelementptr inbounds i8, ptr %10, i64 362
  store i8 0, ptr %19, align 2, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %10, i64 361
  store i8 0, ptr %20, align 1, !tbaa !19
  %21 = getelementptr inbounds i8, ptr %12, i64 362
  store i8 0, ptr %21, align 2, !tbaa !16
  %22 = getelementptr inbounds i8, ptr %12, i64 361
  store i8 0, ptr %22, align 1, !tbaa !19
  %23 = getelementptr inbounds i8, ptr %14, i64 362
  store i8 0, ptr %23, align 2, !tbaa !16
  %24 = getelementptr inbounds i8, ptr %14, i64 361
  store i8 0, ptr %24, align 1, !tbaa !19
  %25 = icmp sgt i32 %15, 1
  br i1 %25, label %.lr.ph, label %._crit_edge, !llvm.loop !20

._crit_edge:                                      ; preds = %.lr.ph, %0
  ret void
}

declare void @Flip_SL(ptr noundef, ptr noundef, ptr 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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"SwapRecord", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !10, i64 24}
!16 = !{!17, !7, i64 362}
!17 = !{!"Node", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !6, i64 48, !6, i64 52, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !10, i64 128, !10, i64 136, !10, i64 144, !10, i64 152, !10, i64 160, !10, i64 168, !10, i64 176, !10, i64 184, !10, i64 192, !10, i64 200, !10, i64 208, !10, i64 216, !10, i64 224, !10, i64 232, !10, i64 240, !10, i64 248, !10, i64 256, !10, i64 264, !10, i64 272, !10, i64 280, !10, i64 288, !10, i64 296, !10, i64 304, !18, i64 312, !18, i64 320, !18, i64 328, !18, i64 336, !18, i64 344, !18, i64 352, !7, i64 360, !7, i64 361, !7, i64 362}
!18 = !{!"double", !7, i64 0}
!19 = !{!17, !7, i64 361}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass