; LLVM IR from niessner/Matterport :: code/gaps/pkgs/png/trees.ll (tr_stored_block)

; 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.internal_state = type { ptr, i32, ptr, i64, ptr, i32, i32, ptr, i32, i8, i32, i32, i32, i32, ptr, i64, ptr, ptr, i32, i32, i32, i32, i32, i64, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [573 x %struct.ct_data_s], [61 x %struct.ct_data_s], [39 x %struct.ct_data_s], %struct.tree_desc_s, %struct.tree_desc_s, %struct.tree_desc_s, [16 x i16], [573 x i32], i32, i32, [573 x i8], ptr, i32, i32, ptr, i64, i64, i32, i32, i16, i32, i64 }
%struct.ct_data_s = type { %union.anon, %union.anon.0 }
%union.anon = type { i16 }
%union.anon.0 = type { i16 }
%struct.tree_desc_s = type { ptr, i32, ptr }

; Function Attrs: nounwind uwtable
define dso_local void @_tr_stored_block(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 3, ptr %9, align 4, !tbaa !11
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.internal_state, ptr %11, i32 0, i32 57
  %13 = load i32, ptr %12, align 4, !tbaa !13
  %14 = load i32, ptr %9, align 4, !tbaa !11
  %15 = sub nsw i32 16, %14
  %16 = icmp sgt i32 %13, %15
  br i1 %16, label %17, label %85

17:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %18 = load i32, ptr %8, align 4, !tbaa !11
  %19 = add nsw i32 0, %18
  store i32 %19, ptr %10, align 4, !tbaa !11
  %20 = load i32, ptr %10, align 4, !tbaa !11
  %21 = trunc i32 %20 to i16
  %22 = zext i16 %21 to i32
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.internal_state, ptr %23, i32 0, i32 57
  %25 = load i32, ptr %24, align 4, !tbaa !13
  %26 = shl i32 %22, %25
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.internal_state, ptr %27, i32 0, i32 56
  %29 = load i16, ptr %28, align 8, !tbaa !17
  %30 = zext i16 %29 to i32
  %31 = or i32 %30, %26
  %32 = trunc i32 %31 to i16
  store i16 %32, ptr %28, align 8, !tbaa !17
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.internal_state, ptr %33, i32 0, i32 56
  %35 = load i16, ptr %34, align 8, !tbaa !17
  %36 = zext i16 %35 to i32
  %37 = and i32 %36, 255
  %38 = trunc i32 %37 to i8
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.internal_state, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !18
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.internal_state, ptr %42, i32 0, i32 5
  %44 = load i32, ptr %43, align 8, !tbaa !19
  %45 = add i32 %44, 1
  store i32 %45, ptr %43, align 8, !tbaa !19
  %46 = zext i32 %44 to i64
  %47 = getelementptr inbounds i8, ptr %41, i64 %46
  store i8 %38, ptr %47, align 1, !tbaa !20
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.internal_state, ptr %48, i32 0, i32 56
  %50 = load i16, ptr %49, align 8, !tbaa !17
  %51 = zext i16 %50 to i32
  %52 = ashr i32 %51, 8
  %53 = trunc i32 %52 to i8
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.internal_state, ptr %54, i32 0, i32 2
  %56 = load ptr, ptr %55, align 8, !tbaa !18
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.internal_state, ptr %57, i32 0, i32 5
  %59 = load i32, ptr %58, align 8, !tbaa !19
  %60 = add i32 %59, 1
  store i32 %60, ptr %58, align 8, !tbaa !19
  %61 = zext i32 %59 to i64
  %62 = getelementptr inbounds i8, ptr %56, i64 %61
  store i8 %53, ptr %62, align 1, !tbaa !20
  %63 = load i32, ptr %10, align 4, !tbaa !11
  %64 = trunc i32 %63 to i16
  %65 = zext i16 %64 to i32
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.internal_state, ptr %66, i32 0, i32 57
  %68 = load i32, ptr %67, align 4, !tbaa !13
  %69 = sext i32 %68 to i64
  %70 = sub i64 16, %69
  %71 = trunc i64 %70 to i32
  %72 = ashr i32 %65, %71
  %73 = trunc i32 %72 to i16
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.internal_state, ptr %74, i32 0, i32 56
  store i16 %73, ptr %75, align 8, !tbaa !17
  %76 = load i32, ptr %9, align 4, !tbaa !11
  %77 = sext i32 %76 to i64
  %78 = sub i64 %77, 16
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.internal_state, ptr %79, i32 0, i32 57
  %81 = load i32, ptr %80, align 4, !tbaa !13
  %82 = sext i32 %81 to i64
  %83 = add i64 %82, %78
  %84 = trunc i64 %83 to i32
  store i32 %84, ptr %80, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %105

85:                                               ; preds = %4
  %86 = load i32, ptr %8, align 4, !tbaa !11
  %87 = add nsw i32 0, %86
  %88 = trunc i32 %87 to i16
  %89 = zext i16 %88 to i32
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.internal_state, ptr %90, i32 0, i32 57
  %92 = load i32, ptr %91, align 4, !tbaa !13
  %93 = shl i32 %89, %92
  %94 = load ptr, ptr %5, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.internal_state, ptr %94, i32 0, i32 56
  %96 = load i16, ptr %95, align 8, !tbaa !17
  %97 = zext i16 %96 to i32
  %98 = or i32 %97, %93
  %99 = trunc i32 %98 to i16
  store i16 %99, ptr %95, align 8, !tbaa !17
  %100 = load i32, ptr %9, align 4, !tbaa !11
  %101 = load ptr, ptr %5, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.internal_state, ptr %101, i32 0, i32 57
  %103 = load i32, ptr %102, align 4, !tbaa !13
  %104 = add nsw i32 %103, %100
  store i32 %104, ptr %102, align 4, !tbaa !13
  br label %105

105:                                              ; preds = %85, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  %106 = load ptr, ptr %5, align 8, !tbaa !5
  %107 = load ptr, ptr %6, align 8, !tbaa !5
  %108 = load i64, ptr %7, align 8, !tbaa !9
  %109 = trunc i64 %108 to i32
  call void @copy_block(ptr noundef %106, ptr noundef %107, i32 noundef %109, i32 noundef 1)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden void @copy_block(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #0

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 5924}
!14 = !{!"internal_state", !6, i64 0, !12, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !12, i64 40, !12, i64 44, !6, i64 48, !12, i64 56, !7, i64 60, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !6, i64 80, !10, i64 88, !6, i64 96, !6, i64 104, !12, i64 112, !12, i64 116, !12, i64 120, !12, i64 124, !12, i64 128, !10, i64 136, !12, i64 144, !12, i64 148, !12, i64 152, !12, i64 156, !12, i64 160, !12, i64 164, !12, i64 168, !12, i64 172, !12, i64 176, !12, i64 180, !12, i64 184, !12, i64 188, !12, i64 192, !7, i64 196, !7, i64 2488, !7, i64 2732, !15, i64 2888, !15, i64 2912, !15, i64 2936, !7, i64 2960, !7, i64 2992, !12, i64 5284, !12, i64 5288, !7, i64 5292, !6, i64 5872, !12, i64 5880, !12, i64 5884, !6, i64 5888, !10, i64 5896, !10, i64 5904, !12, i64 5912, !12, i64 5916, !16, i64 5920, !12, i64 5924, !10, i64 5928}
!15 = !{!"tree_desc_s", !6, i64 0, !12, i64 8, !6, i64 16}
!16 = !{!"short", !7, i64 0}
!17 = !{!14, !16, i64 5920}
!18 = !{!14, !6, i64 16}
!19 = !{!14, !12, i64 40}
!20 = !{!7, !7, i64 0}


; 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: nounwind uwtable
define dso_local void @_tr_stored_block(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 5924
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 13
  br i1 %7, label %8, label %36

8:                                                ; preds = %4
  %9 = and i32 %3, 65535
  %10 = shl i32 %3, %6
  %11 = getelementptr inbounds i8, ptr %0, i64 5920
  %12 = load i16, ptr %11, align 8, !tbaa !14
  %13 = trunc i32 %10 to i16
  %14 = or i16 %12, %13
  store i16 %14, ptr %11, align 8, !tbaa !14
  %15 = trunc i16 %14 to i8
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = getelementptr inbounds i8, ptr %0, i64 40
  %19 = load i32, ptr %18, align 8, !tbaa !16
  %20 = add i32 %19, 1
  store i32 %20, ptr %18, align 8, !tbaa !16
  %21 = zext i32 %19 to i64
  %22 = getelementptr inbounds i8, ptr %17, i64 %21
  store i8 %15, ptr %22, align 1, !tbaa !17
  %23 = load i16, ptr %11, align 8, !tbaa !14
  %24 = lshr i16 %23, 8
  %25 = trunc nuw i16 %24 to i8
  %26 = load ptr, ptr %16, align 8, !tbaa !15
  %27 = load i32, ptr %18, align 8, !tbaa !16
  %28 = add i32 %27, 1
  store i32 %28, ptr %18, align 8, !tbaa !16
  %29 = zext i32 %27 to i64
  %30 = getelementptr inbounds i8, ptr %26, i64 %29
  store i8 %25, ptr %30, align 1, !tbaa !17
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sub i32 16, %31
  %33 = lshr i32 %9, %32
  %34 = trunc nuw i32 %33 to i16
  store i16 %34, ptr %11, align 8, !tbaa !14
  %35 = add i32 %31, -13
  br label %43

36:                                               ; preds = %4
  %37 = shl i32 %3, %6
  %38 = getelementptr inbounds i8, ptr %0, i64 5920
  %39 = load i16, ptr %38, align 8, !tbaa !14
  %40 = trunc i32 %37 to i16
  %41 = or i16 %39, %40
  store i16 %41, ptr %38, align 8, !tbaa !14
  %42 = add nsw i32 %6, 3
  br label %43

43:                                               ; preds = %36, %8
  %storemerge = phi i32 [ %42, %36 ], [ %35, %8 ]
  store i32 %storemerge, ptr %5, align 4, !tbaa !5
  %44 = trunc i64 %2 to i32
  tail call void @copy_block(ptr noundef nonnull %0, ptr noundef %1, i32 noundef %44, i32 noundef 1)
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @copy_block(ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

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

!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, !10, i64 5924}
!6 = !{!"internal_state", !7, i64 0, !10, i64 8, !7, i64 16, !11, i64 24, !7, i64 32, !10, i64 40, !10, i64 44, !7, i64 48, !10, i64 56, !8, i64 60, !10, i64 64, !10, i64 68, !10, i64 72, !10, i64 76, !7, i64 80, !11, i64 88, !7, i64 96, !7, i64 104, !10, i64 112, !10, i64 116, !10, i64 120, !10, i64 124, !10, i64 128, !11, i64 136, !10, i64 144, !10, i64 148, !10, i64 152, !10, i64 156, !10, i64 160, !10, i64 164, !10, i64 168, !10, i64 172, !10, i64 176, !10, i64 180, !10, i64 184, !10, i64 188, !10, i64 192, !8, i64 196, !8, i64 2488, !8, i64 2732, !12, i64 2888, !12, i64 2912, !12, i64 2936, !8, i64 2960, !8, i64 2992, !10, i64 5284, !10, i64 5288, !8, i64 5292, !7, i64 5872, !10, i64 5880, !10, i64 5884, !7, i64 5888, !11, i64 5896, !11, i64 5904, !10, i64 5912, !10, i64 5916, !13, i64 5920, !10, i64 5924, !11, i64 5928}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"tree_desc_s", !7, i64 0, !10, i64 8, !7, i64 16}
!13 = !{!"short", !8, i64 0}
!14 = !{!6, !13, i64 5920}
!15 = !{!6, !7, i64 16}
!16 = !{!6, !10, i64 40}
!17 = !{!8, !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass