; LLVM IR from begeekmyfriend/leetcode :: 0146_lru_cache (lru_cache)

; 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.LRUCache = type { i32, i32, %struct.list_head, [0 x %struct.list_head] }
%struct.list_head = type { ptr, ptr }
%struct.LRUNode = type { i32, i32, %struct.list_head, %struct.list_head }

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_del(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_move(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @lRUCachePut(ptr noundef %0, i32 noundef %1, i32 noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.LRUCache, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !9
  %14 = srem i32 %10, %13
  store i32 %14, ptr %8, align 4, !tbaa !9
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.LRUCache, ptr %15, i32 0, i32 3
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [0 x %struct.list_head], ptr %16, i64 0, i64 %18
  %20 = getelementptr inbounds %struct.list_head, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = getelementptr inbounds i8, ptr %21, i64 sub (i64 0, i64 ptrtoint (ptr getelementptr inbounds (%struct.LRUNode, ptr null, i32 0, i32 2) to i64))
  store ptr %22, ptr %7, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %47, %3
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.LRUNode, ptr %24, i32 0, i32 2
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.LRUCache, ptr %26, i32 0, i32 3
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [0 x %struct.list_head], ptr %27, i64 0, i64 %29
  %31 = icmp ne ptr %25, %30
  br i1 %31, label %32, label %53

32:                                               ; preds = %23
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.LRUNode, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !13
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp eq i32 %35, %36
  br i1 %37, label %38, label %46

38:                                               ; preds = %32
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.LRUNode, ptr %39, i32 0, i32 3
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.LRUCache, ptr %41, i32 0, i32 2
  call void @list_move(ptr noundef %40, ptr noundef %42)
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.LRUNode, ptr %44, i32 0, i32 1
  store i32 %43, ptr %45, align 4, !tbaa !15
  store i32 1, ptr %9, align 4
  br label %95

46:                                               ; preds = %32
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.LRUNode, ptr %48, i32 0, i32 2
  %50 = getelementptr inbounds %struct.list_head, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !16
  %52 = getelementptr inbounds i8, ptr %51, i64 sub (i64 0, i64 ptrtoint (ptr getelementptr inbounds (%struct.LRUNode, ptr null, i32 0, i32 2) to i64))
  store ptr %52, ptr %7, align 8, !tbaa !5
  br label %23, !llvm.loop !17

53:                                               ; preds = %23
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.LRUCache, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.LRUCache, ptr %57, i32 0, i32 0
  %59 = load i32, ptr %58, align 8, !tbaa !9
  %60 = icmp eq i32 %56, %59
  br i1 %60, label %61, label %71

61:                                               ; preds = %53
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.LRUCache, ptr %62, i32 0, i32 2
  %64 = getelementptr inbounds %struct.list_head, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !19
  %66 = getelementptr inbounds i8, ptr %65, i64 sub (i64 0, i64 ptrtoint (ptr getelementptr inbounds (%struct.LRUNode, ptr null, i32 0, i32 3) to i64))
  store ptr %66, ptr %7, align 8, !tbaa !5
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.LRUNode, ptr %67, i32 0, i32 3
  call void @list_del(ptr noundef %68)
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.LRUNode, ptr %69, i32 0, i32 2
  call void @list_del(ptr noundef %70)
  br label %77

71:                                               ; preds = %53
  %72 = call noalias ptr @malloc(i64 noundef 40) #5
  store ptr %72, ptr %7, align 8, !tbaa !5
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.LRUCache, ptr %73, i32 0, i32 1
  %75 = load i32, ptr %74, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %74, align 4, !tbaa !9
  br label %77

77:                                               ; preds = %71, %61
  %78 = load i32, ptr %5, align 4, !tbaa !9
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.LRUNode, ptr %79, i32 0, i32 0
  store i32 %78, ptr %80, align 8, !tbaa !13
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.LRUNode, ptr %81, i32 0, i32 3
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.LRUCache, ptr %83, i32 0, i32 2
  call void @list_add(ptr noundef %82, ptr noundef %84)
  %85 = load ptr, ptr %7, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.LRUNode, ptr %85, i32 0, i32 2
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.LRUCache, ptr %87, i32 0, i32 3
  %89 = load i32, ptr %8, align 4, !tbaa !9
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [0 x %struct.list_head], ptr %88, i64 0, i64 %90
  call void @list_add(ptr noundef %86, ptr noundef %91)
  %92 = load i32, ptr %6, align 4, !tbaa !9
  %93 = load ptr, ptr %7, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.LRUNode, ptr %93, i32 0, i32 1
  store i32 %92, ptr %94, align 4, !tbaa !15
  store i32 0, ptr %9, align 4
  br label %95

95:                                               ; preds = %77, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  %96 = load i32, ptr %9, align 4
  switch i32 %96, label %98 [
    i32 0, label %97
    i32 1, label %97
  ]

97:                                               ; preds = %95, %95
  ret void

98:                                               ; preds = %95
  unreachable
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_add(ptr noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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 #3 = { 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 #4 = { nounwind }
attributes #5 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"list_head", !6, i64 0, !6, i64 8}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 4, !12, i64 8, !12, i64 24}
!15 = !{!14, !10, i64 4}
!16 = !{!14, !6, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!12, !6, i64 8}


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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_del(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_move(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @lRUCachePut(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = load i32, ptr %0, align 8, !tbaa !5
  %5 = srem i32 %1, %4
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = sext i32 %5 to i64
  %8 = getelementptr inbounds [0 x %struct.list_head], ptr %6, i64 0, i64 %7
  br label %9

9:                                                ; preds = %10, %3
  %.pn.in = phi ptr [ %8, %3 ], [ %.pn, %10 ]
  %.pn = load ptr, ptr %.pn.in, align 8, !tbaa !9
  %.not = icmp eq ptr %.pn, %8
  br i1 %.not, label %17, label %10

10:                                               ; preds = %9
  %.0 = getelementptr inbounds i8, ptr %.pn, i64 -8
  %11 = load i32, ptr %.0, align 8, !tbaa !12
  %12 = icmp eq i32 %11, %1
  br i1 %12, label %13, label %9, !llvm.loop !14

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %.pn, i64 16
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  tail call void @list_move(ptr noundef nonnull %14, ptr noundef nonnull %15)
  %16 = getelementptr inbounds i8, ptr %.pn, i64 -4
  br label %34

17:                                               ; preds = %9
  %18 = getelementptr inbounds i8, ptr %0, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp eq i32 %19, %4
  br i1 %20, label %21, label %26

21:                                               ; preds = %17
  %22 = getelementptr inbounds i8, ptr %0, i64 16
  %23 = load ptr, ptr %22, align 8, !tbaa !16
  %24 = getelementptr inbounds i8, ptr %23, i64 -24
  tail call void @list_del(ptr noundef %23)
  %25 = getelementptr inbounds i8, ptr %23, i64 -16
  tail call void @list_del(ptr noundef nonnull %25)
  br label %29

26:                                               ; preds = %17
  %27 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #3
  %28 = add nsw i32 %19, 1
  store i32 %28, ptr %18, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %26, %21
  %.1 = phi ptr [ %24, %21 ], [ %27, %26 ]
  store i32 %1, ptr %.1, align 8, !tbaa !12
  %30 = getelementptr inbounds i8, ptr %.1, i64 24
  %31 = getelementptr inbounds i8, ptr %0, i64 8
  tail call void @list_add(ptr noundef nonnull %30, ptr noundef nonnull %31)
  %32 = getelementptr inbounds i8, ptr %.1, i64 8
  tail call void @list_add(ptr noundef nonnull %32, ptr noundef nonnull %8)
  %33 = getelementptr inbounds i8, ptr %.1, i64 4
  br label %34

34:                                               ; preds = %29, %13
  %.sink = phi ptr [ %33, %29 ], [ %16, %13 ]
  store i32 %2, ptr %.sink, align 4, !tbaa !17
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_add(ptr noundef, ptr noundef) local_unnamed_addr #1

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass