; LLVM IR from caryll/otfcc :: lib/table (hmtx)

; 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.table_hmtx = type { ptr, ptr }
%struct.horizontal_metric = type { double, double }

; 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
define dso_local ptr @otfcc_buildHmtx(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i16, align 2
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i16, align 2
  %13 = alloca i16, align 2
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i16 %1, ptr %7, align 2, !tbaa !9
  store i16 %2, ptr %8, align 2, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %14 = call ptr (...) @bufnew()
  store ptr %14, ptr %10, align 8, !tbaa !5
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %19, label %17

17:                                               ; preds = %4
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %18, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %86

19:                                               ; preds = %4
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.table_hmtx, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %57

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #3
  store i16 0, ptr %12, align 2, !tbaa !9
  br label %25

25:                                               ; preds = %53, %24
  %26 = load i16, ptr %12, align 2, !tbaa !9
  %27 = zext i16 %26 to i32
  %28 = load i16, ptr %7, align 2, !tbaa !9
  %29 = zext i16 %28 to i32
  %30 = icmp slt i32 %27, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %25
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #3
  br label %56

32:                                               ; preds = %25
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.table_hmtx, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  %37 = load i16, ptr %12, align 2, !tbaa !9
  %38 = zext i16 %37 to i64
  %39 = getelementptr inbounds %struct.horizontal_metric, ptr %36, i64 %38
  %40 = getelementptr inbounds %struct.horizontal_metric, ptr %39, i32 0, i32 0
  %41 = load double, ptr %40, align 8, !tbaa !13
  %42 = fptoui double %41 to i16
  call void @bufwrite16b(ptr noundef %33, i16 noundef zeroext %42)
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.table_hmtx, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = load i16, ptr %12, align 2, !tbaa !9
  %48 = zext i16 %47 to i64
  %49 = getelementptr inbounds %struct.horizontal_metric, ptr %46, i64 %48
  %50 = getelementptr inbounds %struct.horizontal_metric, ptr %49, i32 0, i32 1
  %51 = load double, ptr %50, align 8, !tbaa !16
  %52 = fptoui double %51 to i16
  call void @bufwrite16b(ptr noundef %43, i16 noundef zeroext %52)
  br label %53

53:                                               ; preds = %32
  %54 = load i16, ptr %12, align 2, !tbaa !9
  %55 = add i16 %54, 1
  store i16 %55, ptr %12, align 2, !tbaa !9
  br label %25, !llvm.loop !17

56:                                               ; preds = %31
  br label %57

57:                                               ; preds = %56, %19
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.table_hmtx, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !19
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %84

62:                                               ; preds = %57
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #3
  store i16 0, ptr %13, align 2, !tbaa !9
  br label %63

63:                                               ; preds = %80, %62
  %64 = load i16, ptr %13, align 2, !tbaa !9
  %65 = zext i16 %64 to i32
  %66 = load i16, ptr %8, align 2, !tbaa !9
  %67 = zext i16 %66 to i32
  %68 = icmp slt i32 %65, %67
  br i1 %68, label %70, label %69

69:                                               ; preds = %63
  store i32 5, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #3
  br label %83

70:                                               ; preds = %63
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.table_hmtx, ptr %72, i32 0, i32 1
  %74 = load ptr, ptr %73, align 8, !tbaa !19
  %75 = load i16, ptr %13, align 2, !tbaa !9
  %76 = zext i16 %75 to i64
  %77 = getelementptr inbounds double, ptr %74, i64 %76
  %78 = load double, ptr %77, align 8, !tbaa !20
  %79 = fptoui double %78 to i16
  call void @bufwrite16b(ptr noundef %71, i16 noundef zeroext %79)
  br label %80

80:                                               ; preds = %70
  %81 = load i16, ptr %13, align 2, !tbaa !9
  %82 = add i16 %81, 1
  store i16 %82, ptr %13, align 2, !tbaa !9
  br label %63, !llvm.loop !21

83:                                               ; preds = %69
  br label %84

84:                                               ; preds = %83, %57
  %85 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %85, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %86

86:                                               ; preds = %84, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %87 = load ptr, ptr %5, align 8
  ret ptr %87
}

declare ptr @bufnew(...) #2

declare void @bufwrite16b(ptr noundef, i16 noundef zeroext) #2

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 = { "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 }

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


; 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.horizontal_metric = type { double, double }

; Function Attrs: nounwind uwtable
define dso_local ptr @otfcc_buildHmtx(ptr noundef readonly %0, i16 noundef zeroext %1, i16 noundef zeroext %2, ptr nocapture noundef readnone %3) local_unnamed_addr #0 {
  %5 = tail call ptr (...) @bufnew() #2
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %6

6:                                                ; preds = %4
  %7 = load ptr, ptr %0, align 8, !tbaa !5
  %.not23 = icmp ne ptr %7, null
  %8 = icmp ne i16 %1, 0
  %or.cond = and i1 %.not23, %8
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit26

.lr.ph.preheader:                                 ; preds = %6
  %wide.trip.count = zext i16 %1 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %9 = load ptr, ptr %0, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.horizontal_metric, ptr %9, i64 %indvars.iv
  %11 = load double, ptr %10, align 8, !tbaa !10
  %12 = fptoui double %11 to i16
  tail call void @bufwrite16b(ptr noundef %5, i16 noundef zeroext %12) #2
  %13 = load ptr, ptr %0, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.horizontal_metric, ptr %13, i64 %indvars.iv, i32 1
  %15 = load double, ptr %14, align 8, !tbaa !13
  %16 = fptoui double %15 to i16
  tail call void @bufwrite16b(ptr noundef %5, i16 noundef zeroext %16) #2
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit26, label %.lr.ph, !llvm.loop !14

.loopexit26:                                      ; preds = %.lr.ph, %6
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %.not24 = icmp ne ptr %18, null
  %19 = icmp ne i16 %2, 0
  %or.cond30 = and i1 %.not24, %19
  br i1 %or.cond30, label %.lr.ph29.preheader, label %.loopexit

.lr.ph29.preheader:                               ; preds = %.loopexit26
  %wide.trip.count35 = zext i16 %2 to i64
  br label %.lr.ph29

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %.lr.ph29
  %indvars.iv32 = phi i64 [ 0, %.lr.ph29.preheader ], [ %indvars.iv.next33, %.lr.ph29 ]
  %20 = load ptr, ptr %17, align 8, !tbaa !16
  %21 = getelementptr inbounds double, ptr %20, i64 %indvars.iv32
  %22 = load double, ptr %21, align 8, !tbaa !17
  %23 = fptoui double %22 to i16
  tail call void @bufwrite16b(ptr noundef %5, i16 noundef zeroext %23) #2
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %.loopexit, label %.lr.ph29, !llvm.loop !18

.loopexit:                                        ; preds = %.lr.ph29, %.loopexit26, %4
  ret ptr %5
}

declare ptr @bufnew(...) local_unnamed_addr #1

declare void @bufwrite16b(ptr noundef, i16 noundef zeroext) local_unnamed_addr #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 = { "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 }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass