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

; 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.R2Image = type { i32, i32, i32, i32, ptr }

@.str = external hidden unnamed_addr constant [41 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [89 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: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #1

declare void @_Z7RNAbortPKcz(ptr noundef, ...) #2

; 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
define dso_local void @_ZN7R2ImageC2EiiiPh(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef %4) unnamed_addr #3 align 2 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !5
  %13 = load ptr, ptr %6, align 8
  %14 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %15, ptr %14, align 8, !tbaa !11
  %16 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 1
  %17 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %17, ptr %16, align 4, !tbaa !13
  %18 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 2
  %19 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %19, ptr %18, align 8, !tbaa !14
  %20 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 3
  store i32 0, ptr %20, align 4, !tbaa !15
  %21 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 4
  store ptr null, ptr %21, align 8, !tbaa !16
  %22 = load i32, ptr %9, align 4, !tbaa !9
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = mul nsw i32 %22, %23
  %25 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 3
  store i32 %24, ptr %25, align 4, !tbaa !15
  %26 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 3
  %27 = load i32, ptr %26, align 4, !tbaa !15
  %28 = srem i32 %27, 4
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %37

30:                                               ; preds = %5
  %31 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 3
  %32 = load i32, ptr %31, align 4, !tbaa !15
  %33 = sdiv i32 %32, 4
  %34 = add nsw i32 %33, 1
  %35 = mul nsw i32 %34, 4
  %36 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 3
  store i32 %35, ptr %36, align 4, !tbaa !15
  br label %37

37:                                               ; preds = %30, %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %38 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 3
  %39 = load i32, ptr %38, align 4, !tbaa !15
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = mul nsw i32 %39, %40
  store i32 %41, ptr %11, align 4, !tbaa !9
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = icmp sgt i32 %42, 0
  br i1 %43, label %44, label %67

44:                                               ; preds = %37
  %45 = load i32, ptr %11, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = call noalias noundef nonnull ptr @_Znam(i64 noundef %46) #5
  %48 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 4
  store ptr %47, ptr %48, align 8, !tbaa !16
  %49 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 4
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  %51 = icmp ne ptr %50, null
  br i1 %51, label %53, label %52

52:                                               ; preds = %44
  call void (ptr, ...) @_Z7RNAbortPKcz(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 74, ptr noundef @.str.2)
  br label %53

53:                                               ; preds = %52, %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %54 = getelementptr inbounds %class.R2Image, ptr %13, i32 0, i32 4
  %55 = load ptr, ptr %54, align 8, !tbaa !16
  store ptr %55, ptr %12, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %60, %53
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = add nsw i32 %57, -1
  store i32 %58, ptr %11, align 4, !tbaa !9
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %66

60:                                               ; preds = %56
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %61, i32 1
  store ptr %62, ptr %10, align 8, !tbaa !5
  %63 = load i8, ptr %61, align 1, !tbaa !17
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = getelementptr inbounds i8, ptr %64, i32 1
  store ptr %65, ptr %12, align 8, !tbaa !5
  store i8 %63, ptr %64, align 1, !tbaa !17
  br label %56, !llvm.loop !18

66:                                               ; preds = %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %67

67:                                               ; preds = %66, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nobuiltin 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 #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 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 = { nounwind }
attributes #5 = { builtin 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 = !{!12, !10, i64 0}
!12 = !{!"_ZTS7R2Image", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !6, i64 16}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !10, i64 12}
!16 = !{!12, !6, i64 16}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7R2ImageC2EiiiPh(ptr nocapture noundef nonnull writeonly align 8 dereferenceable(24) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, ptr nocapture noundef readonly %4) unnamed_addr #1 align 2 {
  store i32 %1, ptr %0, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  store i32 %2, ptr %6, align 4, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %3, ptr %7, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %0, i64 12
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr null, ptr %9, align 8, !tbaa !13
  %10 = mul nsw i32 %3, %1
  %11 = and i32 %10, 3
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %16, label %12

12:                                               ; preds = %5
  %13 = sdiv i32 %10, 4
  %14 = shl nsw i32 %13, 2
  %15 = add i32 %14, 4
  br label %16

16:                                               ; preds = %12, %5
  %storemerge = phi i32 [ %15, %12 ], [ %10, %5 ]
  store i32 %storemerge, ptr %8, align 4, !tbaa !14
  %17 = mul nsw i32 %storemerge, %2
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %19, label %.loopexit

19:                                               ; preds = %16
  %20 = zext nneg i32 %17 to i64
  %21 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %20) #3
  store ptr %21, ptr %9, align 8, !tbaa !13
  %22 = add nsw i32 %17, -1
  %.not1516 = icmp eq i32 %22, 0
  br i1 %.not1516, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %19
  %23 = zext nneg i32 %22 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %21, ptr align 1 %4, i64 %23, i1 false), !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.preheader, %19, %16
  ret void
}

; 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) #2

attributes #0 = { nobuiltin 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 #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { builtin 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, !7, i64 0}
!6 = !{!"_ZTS7R2Image", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !10, i64 16}
!14 = !{!6, !7, i64 12}
!15 = !{!8, !8, i64 0}


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