; LLVM IR from niessner/Matterport :: code/gaps/pkgs/png (pngrtran)

; 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.png_color_struct = type { i8, i8, i8 }

; 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 void @png_build_grayscale_palette(i32 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %10 = load ptr, ptr %4, align 8, !tbaa !9
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i32 1, ptr %9, align 4
  br label %54

13:                                               ; preds = %2
  %14 = load i32, ptr %3, align 4, !tbaa !5
  switch i32 %14, label %19 [
    i32 1, label %15
    i32 2, label %16
    i32 4, label %17
    i32 8, label %18
  ]

15:                                               ; preds = %13
  store i32 2, ptr %5, align 4, !tbaa !5
  store i32 255, ptr %6, align 4, !tbaa !5
  br label %20

16:                                               ; preds = %13
  store i32 4, ptr %5, align 4, !tbaa !5
  store i32 85, ptr %6, align 4, !tbaa !5
  br label %20

17:                                               ; preds = %13
  store i32 16, ptr %5, align 4, !tbaa !5
  store i32 17, ptr %6, align 4, !tbaa !5
  br label %20

18:                                               ; preds = %13
  store i32 256, ptr %5, align 4, !tbaa !5
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %20

19:                                               ; preds = %13
  store i32 0, ptr %5, align 4, !tbaa !5
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %19, %18, %17, %16, %15
  store i32 0, ptr %7, align 4, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %47, %20
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %25, label %53

25:                                               ; preds = %21
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = trunc i32 %26 to i8
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds %struct.png_color_struct, ptr %28, i64 %30
  %32 = getelementptr inbounds %struct.png_color_struct, ptr %31, i32 0, i32 0
  store i8 %27, ptr %32, align 1, !tbaa !11
  %33 = load i32, ptr %8, align 4, !tbaa !5
  %34 = trunc i32 %33 to i8
  %35 = load ptr, ptr %4, align 8, !tbaa !9
  %36 = load i32, ptr %7, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.png_color_struct, ptr %35, i64 %37
  %39 = getelementptr inbounds %struct.png_color_struct, ptr %38, i32 0, i32 1
  store i8 %34, ptr %39, align 1, !tbaa !13
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = trunc i32 %40 to i8
  %42 = load ptr, ptr %4, align 8, !tbaa !9
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds %struct.png_color_struct, ptr %42, i64 %44
  %46 = getelementptr inbounds %struct.png_color_struct, ptr %45, i32 0, i32 2
  store i8 %41, ptr %46, align 1, !tbaa !14
  br label %47

47:                                               ; preds = %25
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %7, align 4, !tbaa !5
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = load i32, ptr %8, align 4, !tbaa !5
  %52 = add nsw i32 %51, %50
  store i32 %52, ptr %8, align 4, !tbaa !5
  br label %21, !llvm.loop !15

53:                                               ; preds = %21
  store i32 0, ptr %9, align 4
  br label %54

54:                                               ; preds = %53, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  %55 = load i32, ptr %9, align 4
  switch i32 %55, label %57 [
    i32 0, label %56
    i32 1, label %56
  ]

56:                                               ; preds = %54, %54
  ret void

57:                                               ; preds = %54
  unreachable
}

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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"png_color_struct", !7, i64 0, !7, i64 1, !7, i64 2}
!13 = !{!12, !7, i64 1}
!14 = !{!12, !7, i64 2}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%struct.png_color_struct = type { i8, i8, i8 }

@switch.table.png_build_grayscale_palette = private unnamed_addr constant [8 x i64] [i64 2, i64 4, i64 2, i64 16, i64 2, i64 2, i64 2, i64 256], align 8

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: write) uwtable
define dso_local void @png_build_grayscale_palette(i32 noundef %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %1, null
  br i1 %3, label %.loopexit, label %4

4:                                                ; preds = %2
  %switch.tableidx = add i32 %0, -1
  %5 = icmp ult i32 %switch.tableidx, 8
  br i1 %5, label %switch.hole_check, label %.loopexit

switch.hole_check:                                ; preds = %4
  %switch.maskindex = trunc nuw i32 %switch.tableidx to i8
  %switch.shifted = lshr i8 -117, %switch.maskindex
  %switch.lobit = trunc i8 %switch.shifted to i1
  br i1 %switch.lobit, label %switch.lookup, label %.loopexit

switch.lookup:                                    ; preds = %switch.hole_check
  %6 = shl nuw nsw i32 %switch.tableidx, 3
  %switch.shiftamt = zext nneg i32 %6 to i64
  %switch.downshift = lshr i64 144115184082834943, %switch.shiftamt
  %switch.masked = trunc i64 %switch.downshift to i8
  %7 = zext nneg i32 %switch.tableidx to i64
  %switch.gep = getelementptr inbounds [8 x i64], ptr @switch.table.png_build_grayscale_palette, i64 0, i64 %7
  %switch.load = load i64, ptr %switch.gep, align 8
  %unroll_iter = and i64 %switch.load, -2
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %switch.lookup
  %indvars.iv = phi i64 [ 0, %switch.lookup ], [ %indvars.iv.next.1, %.lr.ph ]
  %.020 = phi i8 [ 0, %switch.lookup ], [ %15, %.lr.ph ]
  %niter = phi i64 [ 0, %switch.lookup ], [ %niter.next.1, %.lr.ph ]
  %8 = getelementptr inbounds %struct.png_color_struct, ptr %1, i64 %indvars.iv
  store i8 %.020, ptr %8, align 1, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 1
  store i8 %.020, ptr %9, align 1, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %8, i64 2
  store i8 %.020, ptr %10, align 1, !tbaa !10
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %11 = add i8 %.020, %switch.masked
  %12 = getelementptr inbounds %struct.png_color_struct, ptr %1, i64 %indvars.iv.next
  store i8 %11, ptr %12, align 1, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 1
  store i8 %11, ptr %13, align 1, !tbaa !9
  %14 = getelementptr inbounds i8, ptr %12, i64 2
  store i8 %11, ptr %14, align 1, !tbaa !10
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %15 = add i8 %11, %switch.masked
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %switch.hole_check, %4, %2
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: write) 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, !7, i64 0}
!6 = !{!"png_color_struct", !7, i64 0, !7, i64 1, !7, i64 2}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!6, !7, i64 1}
!10 = !{!6, !7, i64 2}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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