; LLVM IR from yudi-matsuzake/libgenerics :: doc/examples (map0)

; 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.map_t = type { i64, i64, %struct.redblacktree_t }
%struct.redblacktree_t = type { i64, i64, ptr, ptr, i64, ptr }

@.str = external hidden unnamed_addr constant [20 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [9 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @randomize(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #1 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.map_t, align 8
  %3 = alloca [10 x i64], align 16
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  %6 = call i64 @time(ptr noundef null) #4
  %7 = trunc i64 %6 to i32
  call void @srand(i32 noundef %7) #4
  call void @llvm.lifetime.start.p0(i64 64, ptr %2) #4
  %8 = call i32 @map_create(ptr noundef %2, i64 noundef 8, i64 noundef 8)
  call void @llvm.lifetime.start.p0(i64 80, ptr %3) #4
  %9 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 0
  call void @randomize(ptr noundef %9)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %22, %0
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = icmp slt i64 %11, 10
  br i1 %12, label %13, label %25

13:                                               ; preds = %10
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %16, i64 noundef %17)
  %19 = load i64, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %19
  %21 = call i32 @map_insert(ptr noundef %2, ptr noundef %20, ptr noundef %4)
  br label %22

22:                                               ; preds = %13
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = add nsw i64 %23, 1
  store i64 %24, ptr %4, align 8, !tbaa !5
  br label %10, !llvm.loop !9

25:                                               ; preds = %10
  %26 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 0
  call void @randomize(ptr noundef %26)
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %39, %25
  %28 = load i64, ptr %4, align 8, !tbaa !5
  %29 = icmp slt i64 %28, 5
  br i1 %29, label %30, label %42

30:                                               ; preds = %27
  %31 = load i64, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %33, i64 noundef %34)
  %36 = load i64, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %36
  %38 = call i32 @map_insert(ptr noundef %2, ptr noundef %37, ptr noundef %4)
  br label %39

39:                                               ; preds = %30
  %40 = load i64, ptr %4, align 8, !tbaa !5
  %41 = add nsw i64 %40, 1
  store i64 %41, ptr %4, align 8, !tbaa !5
  br label %27, !llvm.loop !11

42:                                               ; preds = %27
  %43 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 0
  call void @randomize(ptr noundef %43)
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %55, %42
  %45 = load i64, ptr %4, align 8, !tbaa !5
  %46 = icmp slt i64 %45, 5
  br i1 %46, label %47, label %58

47:                                               ; preds = %44
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %50)
  %52 = load i64, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %52
  %54 = call i32 @map_remove(ptr noundef %2, ptr noundef %53, ptr noundef null)
  br label %55

55:                                               ; preds = %47
  %56 = load i64, ptr %4, align 8, !tbaa !5
  %57 = add nsw i64 %56, 1
  store i64 %57, ptr %4, align 8, !tbaa !5
  br label %44, !llvm.loop !12

58:                                               ; preds = %44
  %59 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 0
  call void @randomize(ptr noundef %59)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %60

60:                                               ; preds = %78, %58
  %61 = load i64, ptr %4, align 8, !tbaa !5
  %62 = icmp slt i64 %61, 10
  br i1 %62, label %63, label %81

63:                                               ; preds = %60
  %64 = load i64, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %64
  %66 = load i64, ptr %65, align 8, !tbaa !5
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i64 noundef %66)
  %68 = load i64, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds [10 x i64], ptr %3, i64 0, i64 %68
  %70 = call i32 @map_at(ptr noundef %2, ptr noundef %69, ptr noundef %5)
  %71 = icmp eq i32 %70, 0
  br i1 %71, label %72, label %75

72:                                               ; preds = %63
  %73 = load i64, ptr %5, align 8, !tbaa !5
  %74 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i64 noundef %73)
  br label %77

75:                                               ; preds = %63
  %76 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  br label %77

77:                                               ; preds = %75, %72
  br label %78

78:                                               ; preds = %77
  %79 = load i64, ptr %4, align 8, !tbaa !5
  %80 = add nsw i64 %79, 1
  store i64 %80, ptr %4, align 8, !tbaa !5
  br label %60, !llvm.loop !13

81:                                               ; preds = %60
  %82 = call i32 @map_destroy(ptr noundef %2)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 80, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr %2) #4
  ret i32 0
}

; Function Attrs: nounwind
declare void @srand(i32 noundef) #2

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #2

declare i32 @map_create(ptr noundef, i64 noundef, i64 noundef) #3

declare i32 @printf(ptr noundef, ...) #3

declare i32 @map_insert(ptr noundef, ptr noundef, ptr noundef) #3

declare i32 @map_remove(ptr noundef, ptr noundef, ptr noundef) #3

declare i32 @map_at(ptr noundef, ptr noundef, ptr noundef) #3

declare i32 @map_destroy(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 "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 = { "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 }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


; 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.map_t = type { i64, i64, %struct.redblacktree_t }
%struct.redblacktree_t = type { i64, i64, ptr, ptr, i64, ptr }

@.str = external hidden unnamed_addr constant [20 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [9 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @randomize(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = alloca %struct.map_t, align 8
  %2 = alloca [10 x i64], align 16
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = tail call i64 @time(ptr noundef null) #5
  %6 = trunc i64 %5 to i32
  tail call void @srand(i32 noundef %6) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %1) #5
  %7 = call i32 @map_create(ptr noundef nonnull %1, i64 noundef 8, i64 noundef 8) #5
  call void @llvm.lifetime.start.p0(i64 80, ptr nonnull %2) #5
  call void @randomize(ptr noundef nonnull %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %0, %8
  %storemerge7 = phi i64 [ 0, %0 ], [ %16, %8 ]
  %9 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %storemerge7
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %10, i64 noundef %storemerge7)
  %12 = load i64, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %12
  %14 = call i32 @map_insert(ptr noundef nonnull %1, ptr noundef nonnull %13, ptr noundef nonnull %3) #5
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = add nsw i64 %15, 1
  store i64 %16, ptr %3, align 8, !tbaa !5
  %17 = icmp slt i64 %15, 9
  br i1 %17, label %8, label %18, !llvm.loop !9

18:                                               ; preds = %8
  call void @randomize(ptr noundef nonnull %2)
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %18, %19
  %storemerge48 = phi i64 [ 0, %18 ], [ %27, %19 ]
  %20 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %storemerge48
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %21, i64 noundef %storemerge48)
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %23
  %25 = call i32 @map_insert(ptr noundef nonnull %1, ptr noundef nonnull %24, ptr noundef nonnull %3) #5
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = add nsw i64 %26, 1
  store i64 %27, ptr %3, align 8, !tbaa !5
  %28 = icmp slt i64 %26, 4
  br i1 %28, label %19, label %29, !llvm.loop !11

29:                                               ; preds = %19
  call void @randomize(ptr noundef nonnull %2)
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %29, %30
  %storemerge59 = phi i64 [ 0, %29 ], [ %38, %30 ]
  %31 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %storemerge59
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %32)
  %34 = load i64, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %34
  %36 = call i32 @map_remove(ptr noundef nonnull %1, ptr noundef nonnull %35, ptr noundef null) #5
  %37 = load i64, ptr %3, align 8, !tbaa !5
  %38 = add nsw i64 %37, 1
  store i64 %38, ptr %3, align 8, !tbaa !5
  %39 = icmp slt i64 %37, 4
  br i1 %39, label %30, label %40, !llvm.loop !12

40:                                               ; preds = %30
  call void @randomize(ptr noundef nonnull %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #5
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %40, %54
  %storemerge610 = phi i64 [ 0, %40 ], [ %56, %54 ]
  %42 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %storemerge610
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i64 noundef %43)
  %45 = load i64, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %45
  %47 = call i32 @map_at(ptr noundef nonnull %1, ptr noundef nonnull %46, ptr noundef nonnull %4) #5
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %52

49:                                               ; preds = %41
  %50 = load i64, ptr %4, align 8, !tbaa !5
  %51 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i64 noundef %50)
  br label %54

52:                                               ; preds = %41
  %53 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  br label %54

54:                                               ; preds = %49, %52
  %55 = load i64, ptr %3, align 8, !tbaa !5
  %56 = add nsw i64 %55, 1
  store i64 %56, ptr %3, align 8, !tbaa !5
  %57 = icmp slt i64 %55, 9
  br i1 %57, label %41, label %58, !llvm.loop !13

58:                                               ; preds = %54
  %59 = call i32 @map_destroy(ptr noundef nonnull %1) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 80, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %1) #5
  ret i32 0
}

; Function Attrs: nounwind
declare void @srand(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #2

declare i32 @map_create(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #4

declare i32 @map_insert(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @map_remove(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @map_at(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @map_destroy(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 "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 = { "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 = { nofree 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 #5 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass