; LLVM IR from wuzhouhui/leetcode :: 138_copy_list_with_rand (copyRandomList)

; 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.RandomListNode = type { i32, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @copyRandomList(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %13, label %11

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %12, ptr %2, align 8
  br label %98

13:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store ptr null, ptr %5, align 8, !tbaa !5
  store ptr null, ptr %4, align 8, !tbaa !5
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %14, ptr %6, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %39, %13
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %43

18:                                               ; preds = %15
  %19 = call noalias ptr @malloc(i64 noundef 24) #4
  store ptr %19, ptr %7, align 8, !tbaa !5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.RandomListNode, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !9
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.RandomListNode, ptr %23, i32 0, i32 0
  store i32 %22, ptr %24, align 8, !tbaa !9
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.RandomListNode, ptr %25, i32 0, i32 1
  store ptr null, ptr %26, align 8, !tbaa !12
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.RandomListNode, ptr %27, i32 0, i32 2
  store ptr null, ptr %28, align 8, !tbaa !13
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = icmp ne ptr %29, null
  br i1 %30, label %33, label %31

31:                                               ; preds = %18
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %32, ptr %5, align 8, !tbaa !5
  store ptr %32, ptr %4, align 8, !tbaa !5
  br label %38

33:                                               ; preds = %18
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.RandomListNode, ptr %35, i32 0, i32 1
  store ptr %34, ptr %36, align 8, !tbaa !12
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %37, ptr %5, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %33, %31
  br label %39

39:                                               ; preds = %38
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.RandomListNode, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !12
  store ptr %42, ptr %6, align 8, !tbaa !5
  br label %15, !llvm.loop !14

43:                                               ; preds = %15
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %44, ptr %5, align 8, !tbaa !5
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %45, ptr %6, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %89, %43
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %96

49:                                               ; preds = %46
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.RandomListNode, ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !13
  %53 = icmp ne ptr %52, null
  br i1 %53, label %55, label %54

54:                                               ; preds = %49
  br label %89

55:                                               ; preds = %49
  store i32 0, ptr %8, align 4, !tbaa !16
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %56, ptr %7, align 8, !tbaa !5
  br label %57

57:                                               ; preds = %64, %55
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.RandomListNode, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = icmp ne ptr %58, %61
  br i1 %62, label %63, label %70

63:                                               ; preds = %57
  br label %64

64:                                               ; preds = %63
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.RandomListNode, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !12
  store ptr %67, ptr %7, align 8, !tbaa !5
  %68 = load i32, ptr %8, align 4, !tbaa !16
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %8, align 4, !tbaa !16
  br label %57, !llvm.loop !17

70:                                               ; preds = %57
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.RandomListNode, ptr %72, i32 0, i32 2
  store ptr %71, ptr %73, align 8, !tbaa !13
  br label %74

74:                                               ; preds = %85, %70
  %75 = load i32, ptr %8, align 4, !tbaa !16
  %76 = icmp sgt i32 %75, 0
  br i1 %76, label %77, label %88

77:                                               ; preds = %74
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.RandomListNode, ptr %78, i32 0, i32 2
  %80 = load ptr, ptr %79, align 8, !tbaa !13
  %81 = getelementptr inbounds %struct.RandomListNode, ptr %80, i32 0, i32 1
  %82 = load ptr, ptr %81, align 8, !tbaa !12
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.RandomListNode, ptr %83, i32 0, i32 2
  store ptr %82, ptr %84, align 8, !tbaa !13
  br label %85

85:                                               ; preds = %77
  %86 = load i32, ptr %8, align 4, !tbaa !16
  %87 = add nsw i32 %86, -1
  store i32 %87, ptr %8, align 4, !tbaa !16
  br label %74, !llvm.loop !18

88:                                               ; preds = %74
  br label %89

89:                                               ; preds = %88, %54
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.RandomListNode, ptr %90, i32 0, i32 1
  %92 = load ptr, ptr %91, align 8, !tbaa !12
  store ptr %92, ptr %5, align 8, !tbaa !5
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.RandomListNode, ptr %93, i32 0, i32 1
  %95 = load ptr, ptr %94, align 8, !tbaa !12
  store ptr %95, ptr %6, align 8, !tbaa !5
  br label %46, !llvm.loop !19

96:                                               ; preds = %46
  %97 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %97, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %98

98:                                               ; preds = %96, %11
  %99 = load ptr, ptr %2, align 8
  ret ptr %99
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 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 allocsize(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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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, !11, i64 0}
!10 = !{!"RandomListNode", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!11, !11, i64 0}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !15}
!19 = distinct !{!19, !15}


; 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: nofree nounwind uwtable
define dso_local ptr @copyRandomList(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit48, label %.preheader49

.preheader49:                                     ; preds = %1, %7
  %.03352 = phi ptr [ %9, %7 ], [ %0, %1 ]
  %.03551 = phi ptr [ %2, %7 ], [ null, %1 ]
  %.03750 = phi ptr [ %.promoted, %7 ], [ null, %1 ]
  %2 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  %3 = load i32, ptr %.03352, align 8, !tbaa !5
  store i32 %3, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  %.not46 = icmp eq ptr %.03750, null
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  br i1 %.not46, label %7, label %5

5:                                                ; preds = %.preheader49
  %6 = getelementptr inbounds i8, ptr %.03551, i64 8
  store ptr %2, ptr %6, align 8, !tbaa !11
  br label %7

7:                                                ; preds = %.preheader49, %5
  %.promoted = phi ptr [ %.03750, %5 ], [ %2, %.preheader49 ]
  %8 = getelementptr inbounds i8, ptr %.03352, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %.not42 = icmp eq ptr %9, null
  br i1 %.not42, label %.preheader47, label %.preheader49, !llvm.loop !12

.preheader47:                                     ; preds = %7, %.loopexit
  %.13460 = phi ptr [ %26, %.loopexit ], [ %.promoted, %7 ]
  %.259 = phi ptr [ %24, %.loopexit ], [ %0, %7 ]
  %10 = getelementptr inbounds i8, ptr %.259, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %.not44 = icmp eq ptr %11, null
  br i1 %.not44, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %.preheader47
  %.not4553 = icmp eq ptr %11, %0
  br i1 %.not4553, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.055 = phi i32 [ %14, %.lr.ph ], [ 0, %.preheader ]
  %.03254 = phi ptr [ %13, %.lr.ph ], [ %0, %.preheader ]
  %12 = getelementptr inbounds i8, ptr %.03254, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = add nuw nsw i32 %.055, 1
  %.not45 = icmp eq ptr %13, %11
  br i1 %.not45, label %.lr.ph58, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.preheader
  %15 = getelementptr inbounds i8, ptr %.13460, i64 16
  store ptr %.promoted, ptr %15, align 8, !tbaa !14
  br label %.loopexit

.lr.ph58:                                         ; preds = %.lr.ph
  %16 = getelementptr inbounds i8, ptr %.13460, i64 16
  br label %17

17:                                               ; preds = %.lr.ph58, %17
  %18 = phi ptr [ %.promoted, %.lr.ph58 ], [ %20, %17 ]
  %.156 = phi i32 [ %14, %.lr.ph58 ], [ %21, %17 ]
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = add nsw i32 %.156, -1
  %22 = icmp sgt i32 %.156, 1
  br i1 %22, label %17, label %..loopexit_crit_edge, !llvm.loop !16

..loopexit_crit_edge:                             ; preds = %17
  store ptr %20, ptr %16, align 8, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %._crit_edge, %..loopexit_crit_edge, %.preheader47
  %23 = getelementptr inbounds i8, ptr %.259, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = getelementptr inbounds i8, ptr %.13460, i64 8
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %.not43 = icmp eq ptr %24, null
  br i1 %.not43, label %.loopexit48, label %.preheader47, !llvm.loop !17

.loopexit48:                                      ; preds = %.loopexit, %1
  %.039 = phi ptr [ null, %1 ], [ %.promoted, %.loopexit ]
  ret ptr %.039
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

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

attributes #0 = { nofree 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nounwind allocsize(0) }

!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, !7, i64 0}
!6 = !{!"RandomListNode", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !10, i64 16}
!15 = distinct !{!15, !13}
!16 = distinct !{!16, !13}
!17 = distinct !{!17, !13}


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