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

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

@__const.main.keys = external hidden unnamed_addr constant [4 x ptr], align 16
@.str.4 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local double @drand() #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @compare_string_function(ptr noundef, ptr noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.map_t, align 8
  %3 = alloca [4 x ptr], align 16
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  store i32 0, ptr %1, align 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)
  %9 = call i32 @map_set_compare_function(ptr noundef %2, ptr noundef @compare_string_function, ptr noundef null)
  call void @llvm.lifetime.start.p0(i64 32, ptr %3) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %3, ptr align 16 @__const.main.keys, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 4, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %24, %0
  %11 = load i64, ptr %5, align 8, !tbaa !5
  %12 = load i64, ptr %4, align 8, !tbaa !5
  %13 = icmp ult i64 %11, %12
  br i1 %13, label %14, label %27

14:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %15 = call double @drand()
  store double %15, ptr %6, align 8, !tbaa !9
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds [4 x ptr], ptr %3, i64 0, i64 %16
  %18 = call i32 @map_insert(ptr noundef %2, ptr noundef %17, ptr noundef %6)
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds [4 x ptr], ptr %3, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = load double, ptr %6, align 8, !tbaa !9
  %23 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, ptr noundef %21, double noundef %22)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %24

24:                                               ; preds = %14
  %25 = load i64, ptr %5, align 8, !tbaa !5
  %26 = add i64 %25, 1
  store i64 %26, ptr %5, align 8, !tbaa !5
  br label %10, !llvm.loop !13

27:                                               ; preds = %10
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %41, %27
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = load i64, ptr %4, align 8, !tbaa !5
  %31 = icmp ult i64 %29, %30
  br i1 %31, label %32, label %44

32:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds [4 x ptr], ptr %3, i64 0, i64 %33
  %35 = call i32 @map_at(ptr noundef %2, ptr noundef %34, ptr noundef %7)
  %36 = load i64, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds [4 x ptr], ptr %3, i64 0, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = load double, ptr %7, align 8, !tbaa !9
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, ptr noundef %38, double noundef %39)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %41

41:                                               ; preds = %32
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = add i64 %42, 1
  store i64 %43, ptr %5, align 8, !tbaa !5
  br label %28, !llvm.loop !15

44:                                               ; preds = %28
  %45 = 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 32, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr %2) #4
  ret i32 0
}

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

declare i32 @map_set_compare_function(ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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

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

declare i32 @map_destroy(ptr noundef) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

@__const.main.keys = external hidden unnamed_addr constant [4 x ptr], align 16
@.str.4 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local double @drand() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @compare_string_function(ptr noundef, ptr noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
.preheader.preheader:
  %0 = alloca %struct.map_t, align 8
  %1 = alloca [4 x ptr], align 16
  %2 = alloca double, align 8
  %3 = alloca double, align 8
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %0) #5
  %4 = call i32 @map_create(ptr noundef nonnull %0, i64 noundef 8, i64 noundef 8) #5
  %5 = call i32 @map_set_compare_function(ptr noundef nonnull %0, ptr noundef nonnull @compare_string_function, ptr noundef null) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %1) #5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %1, ptr noundef nonnull align 16 dereferenceable(32) @__const.main.keys, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %6 = call double @drand()
  store double %6, ptr %2, align 8, !tbaa !5
  %7 = call i32 @map_insert(ptr noundef nonnull %0, ptr noundef nonnull %1, ptr noundef nonnull %2) #5
  %8 = load ptr, ptr %1, align 16, !tbaa !9
  %9 = load double, ptr %2, align 8, !tbaa !5
  %10 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef %8, double noundef %9)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %11 = call double @drand()
  store double %11, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %1, i64 8
  %13 = call i32 @map_insert(ptr noundef nonnull %0, ptr noundef nonnull %12, ptr noundef nonnull %2) #5
  %14 = load ptr, ptr %12, align 8, !tbaa !9
  %15 = load double, ptr %2, align 8, !tbaa !5
  %16 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef %14, double noundef %15)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %17 = call double @drand()
  store double %17, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %1, i64 16
  %19 = call i32 @map_insert(ptr noundef nonnull %0, ptr noundef nonnull %18, ptr noundef nonnull %2) #5
  %20 = load ptr, ptr %18, align 16, !tbaa !9
  %21 = load double, ptr %2, align 8, !tbaa !5
  %22 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef %20, double noundef %21)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %23 = call double @drand()
  store double %23, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %1, i64 24
  %25 = call i32 @map_insert(ptr noundef nonnull %0, ptr noundef nonnull %24, ptr noundef nonnull %2) #5
  %26 = load ptr, ptr %24, align 8, !tbaa !9
  %27 = load double, ptr %2, align 8, !tbaa !5
  %28 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef %26, double noundef %27)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  %29 = call i32 @map_at(ptr noundef nonnull %0, ptr noundef nonnull %1, ptr noundef nonnull %3) #5
  %30 = load ptr, ptr %1, align 16, !tbaa !9
  %31 = load double, ptr %3, align 8, !tbaa !5
  %32 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, ptr noundef %30, double noundef %31)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  %33 = call i32 @map_at(ptr noundef nonnull %0, ptr noundef nonnull %12, ptr noundef nonnull %3) #5
  %34 = load ptr, ptr %12, align 8, !tbaa !9
  %35 = load double, ptr %3, align 8, !tbaa !5
  %36 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, ptr noundef %34, double noundef %35)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  %37 = call i32 @map_at(ptr noundef nonnull %0, ptr noundef nonnull %18, ptr noundef nonnull %3) #5
  %38 = load ptr, ptr %18, align 16, !tbaa !9
  %39 = load double, ptr %3, align 8, !tbaa !5
  %40 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, ptr noundef %38, double noundef %39)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  %41 = call i32 @map_at(ptr noundef nonnull %0, ptr noundef nonnull %24, ptr noundef nonnull %3) #5
  %42 = load ptr, ptr %24, align 8, !tbaa !9
  %43 = load double, ptr %3, align 8, !tbaa !5
  %44 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, ptr noundef %42, double noundef %43)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  %45 = call i32 @map_destroy(ptr noundef nonnull %0) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %1) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %0) #5
  ret i32 0
}

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

declare i32 @map_set_compare_function(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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

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

declare i32 @map_destroy(ptr noundef) local_unnamed_addr #2

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 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"double", !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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass