; LLVM IR from erwincoumans/tiny-differentiable-simulator :: src/visualizer/opengl/tiny_font_stash.ll (Z24sth_add_font_from_memoryP9sth_stashPh)

; 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.sth_font = type { i32, i32, %struct.stbtt_fontinfo, ptr, ptr, [256 x i32], i32, float, float, float, ptr }
%struct.stbtt_fontinfo = type { ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.sth_stash = type { i32, i32, float, float, ptr, ptr, i32, ptr }

@_ZL3idx = external hidden global i32, align 4

; 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: mustprogress uwtable
declare dso_local i32 @stbtt_InitFont(ptr noundef, ptr noundef, i32 noundef) #1

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; Function Attrs: mustprogress uwtable
declare dso_local void @stbtt_GetFontVMetrics(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z24sth_add_font_from_memoryP9sth_stashPh(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  store ptr null, ptr %11, align 8, !tbaa !5
  %13 = call noalias ptr @malloc(i64 noundef 1128) #6
  store ptr %13, ptr %11, align 8, !tbaa !5
  %14 = load ptr, ptr %11, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %17

16:                                               ; preds = %2
  br label %87

17:                                               ; preds = %2
  %18 = load ptr, ptr %11, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %18, i8 0, i64 1128, i1 false)
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %28, %17
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = icmp slt i32 %20, 256
  br i1 %21, label %22, label %31

22:                                               ; preds = %19
  %23 = load ptr, ptr %11, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.sth_font, ptr %23, i32 0, i32 5
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [256 x i32], ptr %24, i64 0, i64 %26
  store i32 -1, ptr %27, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %22
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %6, align 4, !tbaa !9
  br label %19, !llvm.loop !11

31:                                               ; preds = %19
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load ptr, ptr %11, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.sth_font, ptr %33, i32 0, i32 3
  store ptr %32, ptr %34, align 8, !tbaa !13
  %35 = load ptr, ptr %11, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.sth_font, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.sth_font, ptr %37, i32 0, i32 3
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = call i32 @stbtt_InitFont(ptr noundef %36, ptr noundef %39, i32 noundef 0)
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %43, label %42

42:                                               ; preds = %31
  br label %87

43:                                               ; preds = %31
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.sth_font, ptr %44, i32 0, i32 2
  call void @stbtt_GetFontVMetrics(ptr noundef %45, ptr noundef %7, ptr noundef %8, ptr noundef %10)
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = sub nsw i32 %46, %47
  store i32 %48, ptr %9, align 4, !tbaa !9
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = sitofp i32 %49 to float
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = sitofp i32 %51 to float
  %53 = fdiv float %50, %52
  %54 = load ptr, ptr %11, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.sth_font, ptr %54, i32 0, i32 7
  store float %53, ptr %55, align 4, !tbaa !17
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = sitofp i32 %56 to float
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = sitofp i32 %58 to float
  %60 = fdiv float %57, %59
  %61 = load ptr, ptr %11, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.sth_font, ptr %61, i32 0, i32 8
  store float %60, ptr %62, align 8, !tbaa !18
  %63 = load i32, ptr %9, align 4, !tbaa !9
  %64 = load i32, ptr %10, align 4, !tbaa !9
  %65 = add nsw i32 %63, %64
  %66 = sitofp i32 %65 to float
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = sitofp i32 %67 to float
  %69 = fdiv float %66, %68
  %70 = load ptr, ptr %11, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.sth_font, ptr %70, i32 0, i32 9
  store float %69, ptr %71, align 4, !tbaa !19
  %72 = load i32, ptr @_ZL3idx, align 4, !tbaa !9
  %73 = load ptr, ptr %11, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.sth_font, ptr %73, i32 0, i32 0
  store i32 %72, ptr %74, align 8, !tbaa !20
  %75 = load ptr, ptr %11, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.sth_font, ptr %75, i32 0, i32 1
  store i32 2, ptr %76, align 4, !tbaa !21
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.sth_stash, ptr %77, i32 0, i32 5
  %79 = load ptr, ptr %78, align 8, !tbaa !22
  %80 = load ptr, ptr %11, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.sth_font, ptr %80, i32 0, i32 10
  store ptr %79, ptr %81, align 8, !tbaa !24
  %82 = load ptr, ptr %11, align 8, !tbaa !5
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.sth_stash, ptr %83, i32 0, i32 5
  store ptr %82, ptr %84, align 8, !tbaa !22
  %85 = load i32, ptr @_ZL3idx, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr @_ZL3idx, align 4, !tbaa !9
  store i32 %85, ptr %3, align 4
  store i32 1, ptr %12, align 4
  br label %102

87:                                               ; preds = %42, %16
  %88 = load ptr, ptr %11, align 8, !tbaa !5
  %89 = icmp ne ptr %88, null
  br i1 %89, label %90, label %101

90:                                               ; preds = %87
  %91 = load ptr, ptr %11, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.sth_font, ptr %91, i32 0, i32 4
  %93 = load ptr, ptr %92, align 8, !tbaa !25
  %94 = icmp ne ptr %93, null
  br i1 %94, label %95, label %99

95:                                               ; preds = %90
  %96 = load ptr, ptr %11, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.sth_font, ptr %96, i32 0, i32 4
  %98 = load ptr, ptr %97, align 8, !tbaa !25
  call void @free(ptr noundef %98) #5
  br label %99

99:                                               ; preds = %95, %90
  %100 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %100) #5
  br label %101

101:                                              ; preds = %99, %87
  store i32 0, ptr %3, align 4
  store i32 1, ptr %12, align 4
  br label %102

102:                                              ; preds = %101, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  %103 = load i32, ptr %3, align 4
  ret i32 %103
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 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 "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 64}
!14 = !{!"_ZTS8sth_font", !10, i64 0, !10, i64 4, !15, i64 8, !6, i64 64, !6, i64 72, !7, i64 80, !10, i64 1104, !16, i64 1108, !16, i64 1112, !16, i64 1116, !6, i64 1120}
!15 = !{!"_ZTS14stbtt_fontinfo", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52}
!16 = !{!"float", !7, i64 0}
!17 = !{!14, !16, i64 1108}
!18 = !{!14, !16, i64 1112}
!19 = !{!14, !16, i64 1116}
!20 = !{!14, !10, i64 0}
!21 = !{!14, !10, i64 4}
!22 = !{!23, !6, i64 24}
!23 = !{!"_ZTS9sth_stash", !10, i64 0, !10, i64 4, !16, i64 8, !16, i64 12, !6, i64 16, !6, i64 24, !10, i64 32, !6, i64 40}
!24 = !{!14, !6, i64 1120}
!25 = !{!14, !6, i64 72}


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

@_ZL3idx = external hidden local_unnamed_addr global i32, align 4

; 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: mustprogress uwtable
declare dso_local i32 @stbtt_InitFont(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @stbtt_GetFontVMetrics(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z24sth_add_font_from_memoryP9sth_stashPh(ptr nocapture noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #5
  %calloc = tail call dereferenceable_or_null(1128) ptr @calloc(i64 1, i64 1128)
  %cond = icmp eq ptr %calloc, null
  br i1 %cond, label %39, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %calloc, i64 80
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1024) %7, i8 -1, i64 1024, i1 false), !tbaa !5
  %8 = getelementptr inbounds i8, ptr %calloc, i64 64
  store ptr %1, ptr %8, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %calloc, i64 8
  %10 = tail call i32 @stbtt_InitFont(ptr noundef nonnull %9, ptr noundef %1, i32 noundef 0)
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %34, label %11

11:                                               ; preds = %6
  call void @stbtt_GetFontVMetrics(ptr noundef nonnull %9, ptr noundef nonnull %3, ptr noundef nonnull %4, ptr noundef nonnull %5)
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = sub nsw i32 %12, %13
  %15 = sitofp i32 %14 to float
  %16 = getelementptr inbounds i8, ptr %calloc, i64 1108
  %17 = insertelement <2 x i32> poison, i32 %12, i64 0
  %18 = insertelement <2 x i32> %17, i32 %13, i64 1
  %19 = sitofp <2 x i32> %18 to <2 x float>
  %20 = insertelement <2 x float> poison, float %15, i64 0
  %21 = shufflevector <2 x float> %20, <2 x float> poison, <2 x i32> zeroinitializer
  %22 = fdiv <2 x float> %19, %21
  store <2 x float> %22, ptr %16, align 4, !tbaa !14
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = add nsw i32 %23, %14
  %25 = sitofp i32 %24 to float
  %26 = fdiv float %25, %15
  %27 = getelementptr inbounds i8, ptr %calloc, i64 1116
  store float %26, ptr %27, align 4, !tbaa !15
  %28 = load i32, ptr @_ZL3idx, align 4, !tbaa !5
  store i32 %28, ptr %calloc, align 8, !tbaa !16
  %29 = getelementptr inbounds i8, ptr %calloc, i64 4
  store i32 2, ptr %29, align 4, !tbaa !17
  %30 = getelementptr inbounds i8, ptr %0, i64 24
  %31 = load ptr, ptr %30, align 8, !tbaa !18
  %32 = getelementptr inbounds i8, ptr %calloc, i64 1120
  store ptr %31, ptr %32, align 8, !tbaa !20
  store ptr %calloc, ptr %30, align 8, !tbaa !18
  %33 = add nsw i32 %28, 1
  store i32 %33, ptr @_ZL3idx, align 4, !tbaa !5
  br label %39

34:                                               ; preds = %6
  %35 = getelementptr inbounds i8, ptr %calloc, i64 72
  %36 = load ptr, ptr %35, align 8, !tbaa !21
  %.not32 = icmp eq ptr %36, null
  br i1 %.not32, label %38, label %37

37:                                               ; preds = %34
  tail call void @free(ptr noundef nonnull %36) #5
  br label %38

38:                                               ; preds = %37, %34
  tail call void @free(ptr noundef nonnull %calloc) #5
  br label %39

39:                                               ; preds = %2, %38, %11
  %.0 = phi i32 [ %28, %11 ], [ 0, %2 ], [ 0, %38 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  ret i32 %.0
}

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !12, i64 64}
!10 = !{!"_ZTS8sth_font", !6, i64 0, !6, i64 4, !11, i64 8, !12, i64 64, !12, i64 72, !7, i64 80, !6, i64 1104, !13, i64 1108, !13, i64 1112, !13, i64 1116, !12, i64 1120}
!11 = !{!"_ZTS14stbtt_fontinfo", !12, i64 0, !12, i64 8, !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}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"float", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!10, !13, i64 1116}
!16 = !{!10, !6, i64 0}
!17 = !{!10, !6, i64 4}
!18 = !{!19, !12, i64 24}
!19 = !{!"_ZTS9sth_stash", !6, i64 0, !6, i64 4, !13, i64 8, !13, i64 12, !12, i64 16, !12, i64 24, !6, i64 32, !12, i64 40}
!20 = !{!10, !12, i64 1120}
!21 = !{!10, !12, i64 72}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass