; LLVM IR from niessner/Matterport :: code/gaps/pkgs/R2Shapes/R2Grid.ll (ZNK6R2Grid9WriteGridEP8_IO_FILE)

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

%class.R2Grid = type { %class.R2Affine, %class.R2Affine, double, ptr, [2 x i32], i32, i32 }
%class.R2Affine = type <{ %class.R2Transformation, %class.R3Matrix, i32, [4 x i8] }>
%class.R2Transformation = type { ptr }
%class.R3Matrix = type { [3 x [3 x double]] }

@.str.73 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.74 = external hidden unnamed_addr constant [46 x i8], align 1
@.str.75 = external hidden unnamed_addr constant [37 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

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #1

declare void @_Z6RNFailPKcz(ptr noundef, ...) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZNK6R2Grid9WriteGridEP8_IO_FILE(ptr noundef nonnull align 8 dereferenceable(208) %0, ptr noundef %1) #2 align 2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8
  %11 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 4
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = call i64 @fwrite(ptr noundef %11, i64 noundef 4, i64 noundef 2, ptr noundef %12)
  %14 = icmp ne i64 %13, 2
  br i1 %14, label %15, label %16

15:                                               ; preds = %2
  call void (ptr, ...) @_Z6RNFailPKcz(ptr noundef @.str.73)
  store i32 0, ptr %3, align 4
  br label %70

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %17 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 1
  %18 = call noundef nonnull align 8 dereferenceable(72) ptr @_ZNK8R2Affine6MatrixEv(ptr noundef nonnull align 8 dereferenceable(84) %17)
  %19 = call noundef ptr @_ZNK8R3MatrixixEi(ptr noundef nonnull align 8 dereferenceable(72) %18, i32 noundef 0)
  %20 = getelementptr inbounds double, ptr %19, i64 0
  store ptr %20, ptr %6, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i64 @fwrite(ptr noundef %21, i64 noundef 8, i64 noundef 9, ptr noundef %22)
  %24 = icmp ne i64 %23, 9
  br i1 %24, label %25, label %26

25:                                               ; preds = %16
  call void (ptr, ...) @_Z6RNFailPKcz(ptr noundef @.str.74)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

26:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %61, %26
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 4
  %30 = getelementptr inbounds [2 x i32], ptr %29, i64 0, i64 1
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = icmp slt i32 %28, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %27
  store i32 2, ptr %7, align 4
  br label %64

34:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %35 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 3
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 4
  %39 = getelementptr inbounds [2 x i32], ptr %38, i64 0, i64 0
  %40 = load i32, ptr %39, align 8, !tbaa !9
  %41 = mul nsw i32 %37, %40
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds double, ptr %36, i64 %42
  store ptr %43, ptr %9, align 8, !tbaa !5
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 4
  %46 = getelementptr inbounds [2 x i32], ptr %45, i64 0, i64 0
  %47 = load i32, ptr %46, align 8, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = call i64 @fwrite(ptr noundef %44, i64 noundef 8, i64 noundef %48, ptr noundef %49)
  %51 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 4
  %52 = getelementptr inbounds [2 x i32], ptr %51, i64 0, i64 0
  %53 = load i32, ptr %52, align 8, !tbaa !9
  %54 = zext i32 %53 to i64
  %55 = icmp ne i64 %50, %54
  br i1 %55, label %56, label %57

56:                                               ; preds = %34
  call void (ptr, ...) @_Z6RNFailPKcz(ptr noundef @.str.75)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %58

57:                                               ; preds = %34
  store i32 0, ptr %7, align 4
  br label %58

58:                                               ; preds = %57, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  %59 = load i32, ptr %7, align 4
  switch i32 %59, label %64 [
    i32 0, label %60
  ]

60:                                               ; preds = %58
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %8, align 4, !tbaa !9
  br label %27, !llvm.loop !17

64:                                               ; preds = %58, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  %65 = load i32, ptr %7, align 4
  switch i32 %65, label %69 [
    i32 2, label %66
  ]

66:                                               ; preds = %64
  %67 = getelementptr inbounds %class.R2Grid, ptr %10, i32 0, i32 6
  %68 = load i32, ptr %67, align 4, !tbaa !19
  store i32 %68, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

69:                                               ; preds = %66, %64, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %70

70:                                               ; preds = %69, %15
  %71 = load i32, ptr %3, align 4
  ret i32 %71
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(72) ptr @_ZNK8R2Affine6MatrixEv(ptr noundef nonnull align 8 dereferenceable(84)) #3 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZNK8R3MatrixixEi(ptr noundef nonnull align 8 dereferenceable(72), i32 noundef) #4 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { inlinehint mustprogress 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 = { inlinehint 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 #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 = !{!"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 = !{!12, !6, i64 184}
!12 = !{!"_ZTS6R2Grid", !13, i64 0, !13, i64 88, !16, i64 176, !6, i64 184, !7, i64 192, !10, i64 200, !10, i64 204}
!13 = !{!"_ZTS8R2Affine", !14, i64 0, !15, i64 8, !10, i64 80}
!14 = !{!"_ZTS16R2Transformation"}
!15 = !{!"_ZTS8R3Matrix", !7, i64 0}
!16 = !{!"double", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!12, !10, i64 204}


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

@.str.73 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.74 = external hidden unnamed_addr constant [46 x i8], align 1
@.str.75 = external hidden unnamed_addr constant [37 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #0

declare void @_Z6RNFailPKcz(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZNK6R2Grid9WriteGridEP8_IO_FILE(ptr noundef nonnull align 8 dereferenceable(208) %0, ptr nocapture noundef %1) local_unnamed_addr #2 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 192
  %4 = tail call i64 @fwrite(ptr noundef nonnull %3, i64 noundef 4, i64 noundef 2, ptr noundef %1)
  %.not = icmp eq i64 %4, 2
  br i1 %.not, label %6, label %5

5:                                                ; preds = %2
  tail call void (ptr, ...) @_Z6RNFailPKcz(ptr noundef nonnull @.str.73)
  br label %30

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 88
  %8 = tail call noundef nonnull align 8 dereferenceable(72) ptr @_ZNK8R2Affine6MatrixEv(ptr noundef nonnull align 8 dereferenceable(84) %7)
  %9 = tail call noundef ptr @_ZNK8R3MatrixixEi(ptr noundef nonnull align 8 dereferenceable(72) %8, i32 noundef 0)
  %10 = tail call i64 @fwrite(ptr noundef %9, i64 noundef 8, i64 noundef 9, ptr noundef %1)
  %.not15 = icmp eq i64 %10, 9
  br i1 %.not15, label %.preheader, label %14

.preheader:                                       ; preds = %6
  %11 = getelementptr inbounds i8, ptr %0, i64 196
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %.not1720 = icmp sgt i32 %12, 0
  br i1 %.not1720, label %.critedge.lr.ph, label %.critedge19

.critedge.lr.ph:                                  ; preds = %.preheader
  %13 = getelementptr inbounds i8, ptr %0, i64 184
  %.pre = load i32, ptr %3, align 8, !tbaa !5
  br label %.critedge

14:                                               ; preds = %6
  tail call void (ptr, ...) @_Z6RNFailPKcz(ptr noundef nonnull @.str.74)
  br label %30

15:                                               ; preds = %.critedge
  %16 = add nuw nsw i32 %.01221, 1
  %17 = load i32, ptr %11, align 4, !tbaa !5
  %.not17 = icmp slt i32 %16, %17
  br i1 %.not17, label %.critedge, label %.critedge19, !llvm.loop !9

.critedge:                                        ; preds = %.critedge.lr.ph, %15
  %18 = phi i32 [ %.pre, %.critedge.lr.ph ], [ %25, %15 ]
  %.01221 = phi i32 [ 0, %.critedge.lr.ph ], [ %16, %15 ]
  %19 = load ptr, ptr %13, align 8, !tbaa !11
  %20 = mul nsw i32 %18, %.01221
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds double, ptr %19, i64 %21
  %23 = sext i32 %18 to i64
  %24 = tail call i64 @fwrite(ptr noundef %22, i64 noundef 8, i64 noundef %23, ptr noundef %1)
  %25 = load i32, ptr %3, align 8, !tbaa !5
  %26 = zext i32 %25 to i64
  %.not16 = icmp eq i64 %24, %26
  br i1 %.not16, label %15, label %27

27:                                               ; preds = %.critedge
  tail call void (ptr, ...) @_Z6RNFailPKcz(ptr noundef nonnull @.str.75)
  br label %30

.critedge19:                                      ; preds = %15, %.preheader
  %28 = getelementptr inbounds i8, ptr %0, i64 204
  %29 = load i32, ptr %28, align 4, !tbaa !18
  br label %30

30:                                               ; preds = %27, %14, %.critedge19, %5
  %.4 = phi i32 [ 0, %5 ], [ 0, %14 ], [ %29, %.critedge19 ], [ 0, %27 ]
  ret i32 %.4
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(72) ptr @_ZNK8R2Affine6MatrixEv(ptr noundef nonnull align 8 dereferenceable(84)) local_unnamed_addr #3 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZNK8R3MatrixixEi(ptr noundef nonnull align 8 dereferenceable(72), i32 noundef) local_unnamed_addr #4 align 2

attributes #0 = { 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 #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 = { 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 #3 = { inlinehint mustprogress 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 = { inlinehint 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" }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !17, i64 184}
!12 = !{!"_ZTS6R2Grid", !13, i64 0, !13, i64 88, !16, i64 176, !17, i64 184, !7, i64 192, !6, i64 200, !6, i64 204}
!13 = !{!"_ZTS8R2Affine", !14, i64 0, !15, i64 8, !6, i64 80}
!14 = !{!"_ZTS16R2Transformation"}
!15 = !{!"_ZTS8R3Matrix", !7, i64 0}
!16 = !{!"double", !7, i64 0}
!17 = !{!"any pointer", !7, i64 0}
!18 = !{!12, !6, i64 204}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass