; LLVM IR from mkazhdan/DMG :: Util/PNG (pngwrite)

; 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_info_struct = type { i64, i64, i64, i64, ptr, i16, i16, i8, i8, i8, i8, i8, i8, i8, i8, [8 x i8], float, i8, i32, i32, ptr, %struct.png_time_struct, %struct.png_color_8_struct, ptr, %struct.png_color_16_struct, %struct.png_color_16_struct, i64, i64, i8, i64, i64, i8, ptr, float, float, float, float, float, float, float, float, ptr, i64, i64, ptr, ptr, i8, i8, i64, ptr, i64, ptr, ptr, i64, i8, ptr, i64, i8, double, double, ptr, ptr, ptr, i64, i64, i64, i64, i64, i64, i64, i64, i64 }
%struct.png_time_struct = type { i16, i8, i8, i8, i8, i8 }
%struct.png_color_8_struct = type { i8, i8, i8, i8, i8 }
%struct.png_color_16_struct = type { i8, i16, i16, i16, i16 }

; Function Attrs: nounwind uwtable
declare dso_local void @png_write_info(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @png_write_end(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @png_write_image(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @png_write_png(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %14, label %11

11:                                               ; preds = %4
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %11, %4
  br label %95

15:                                               ; preds = %11
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = and i32 %16, 1024
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %15
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_invert_alpha(ptr noundef %20)
  br label %21

21:                                               ; preds = %19, %15
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  call void @png_write_info(ptr noundef %22, ptr noundef %23)
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = and i32 %24, 32
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %29

27:                                               ; preds = %21
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_invert_mono(ptr noundef %28)
  br label %29

29:                                               ; preds = %27, %21
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = and i32 %30, 64
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %43

33:                                               ; preds = %29
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.png_info_struct, ptr %34, i32 0, i32 2
  %36 = load i64, ptr %35, align 8, !tbaa !11
  %37 = and i64 %36, 2
  %38 = icmp ne i64 %37, 0
  br i1 %38, label %39, label %43

39:                                               ; preds = %33
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.png_info_struct, ptr %41, i32 0, i32 22
  call void @png_set_shift(ptr noundef %40, ptr noundef %42)
  br label %43

43:                                               ; preds = %39, %33, %29
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = and i32 %44, 4
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %49

47:                                               ; preds = %43
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_packing(ptr noundef %48)
  br label %49

49:                                               ; preds = %47, %43
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = and i32 %50, 256
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %55

53:                                               ; preds = %49
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_swap_alpha(ptr noundef %54)
  br label %55

55:                                               ; preds = %53, %49
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = and i32 %56, 2048
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %61

59:                                               ; preds = %55
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_filler(ptr noundef %60, i64 noundef 0, i32 noundef 0)
  br label %61

61:                                               ; preds = %59, %55
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = and i32 %62, 128
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %67

65:                                               ; preds = %61
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_bgr(ptr noundef %66)
  br label %67

67:                                               ; preds = %65, %61
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = and i32 %68, 512
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %71, label %73

71:                                               ; preds = %67
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_swap(ptr noundef %72)
  br label %73

73:                                               ; preds = %71, %67
  %74 = load i32, ptr %7, align 4, !tbaa !9
  %75 = and i32 %74, 8
  %76 = icmp ne i32 %75, 0
  br i1 %76, label %77, label %79

77:                                               ; preds = %73
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  call void @png_set_packswap(ptr noundef %78)
  br label %79

79:                                               ; preds = %77, %73
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.png_info_struct, ptr %80, i32 0, i32 2
  %82 = load i64, ptr %81, align 8, !tbaa !11
  %83 = and i64 %82, 32768
  %84 = icmp ne i64 %83, 0
  br i1 %84, label %85, label %90

85:                                               ; preds = %79
  %86 = load ptr, ptr %5, align 8, !tbaa !5
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.png_info_struct, ptr %87, i32 0, i32 62
  %89 = load ptr, ptr %88, align 8, !tbaa !20
  call void @png_write_image(ptr noundef %86, ptr noundef %89)
  br label %90

90:                                               ; preds = %85, %79
  %91 = load ptr, ptr %5, align 8, !tbaa !5
  %92 = load ptr, ptr %6, align 8, !tbaa !5
  call void @png_write_end(ptr noundef %91, ptr noundef %92)
  %93 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %93, ptr %7, align 4, !tbaa !9
  %94 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %94, ptr %8, align 8, !tbaa !5
  br label %95

95:                                               ; preds = %90, %14
  ret void
}

declare void @png_set_invert_alpha(ptr noundef) #1

declare void @png_set_invert_mono(ptr noundef) #1

declare void @png_set_shift(ptr noundef, ptr noundef) #1

declare void @png_set_packing(ptr noundef) #1

declare void @png_set_swap_alpha(ptr noundef) #1

declare void @png_set_filler(ptr noundef, i64 noundef, i32 noundef) #1

declare void @png_set_bgr(ptr noundef) #1

declare void @png_set_swap(ptr noundef) #1

declare void @png_set_packswap(ptr noundef) #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" }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 16}
!12 = !{!"png_info_struct", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !6, i64 32, !14, i64 40, !14, i64 42, !7, i64 44, !7, i64 45, !7, i64 46, !7, i64 47, !7, i64 48, !7, i64 49, !7, i64 50, !7, i64 51, !7, i64 52, !15, i64 60, !7, i64 64, !10, i64 68, !10, i64 72, !6, i64 80, !16, i64 88, !17, i64 96, !6, i64 104, !18, i64 112, !18, i64 122, !13, i64 136, !13, i64 144, !7, i64 152, !13, i64 160, !13, i64 168, !7, i64 176, !6, i64 184, !15, i64 192, !15, i64 196, !15, i64 200, !15, i64 204, !15, i64 208, !15, i64 212, !15, i64 216, !15, i64 220, !6, i64 224, !13, i64 232, !13, i64 240, !6, i64 248, !6, i64 256, !7, i64 264, !7, i64 265, !13, i64 272, !6, i64 280, !13, i64 288, !6, i64 296, !6, i64 304, !13, i64 312, !7, i64 320, !6, i64 328, !13, i64 336, !7, i64 344, !19, i64 352, !19, i64 360, !6, i64 368, !6, i64 376, !6, i64 384, !13, i64 392, !13, i64 400, !13, i64 408, !13, i64 416, !13, i64 424, !13, i64 432, !13, i64 440, !13, i64 448, !13, i64 456}
!13 = !{!"long", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!"float", !7, i64 0}
!16 = !{!"png_time_struct", !14, i64 0, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !7, i64 6}
!17 = !{!"png_color_8_struct", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4}
!18 = !{!"png_color_16_struct", !7, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !14, i64 8}
!19 = !{!"double", !7, i64 0}
!20 = !{!12, !6, i64 384}


; 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
declare dso_local void @png_write_info(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @png_write_end(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @png_write_image(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @png_write_png(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr nocapture noundef readnone %3) local_unnamed_addr #0 {
  %5 = icmp eq ptr %0, null
  %6 = icmp eq ptr %1, null
  %or.cond = or i1 %5, %6
  br i1 %or.cond, label %47, label %7

7:                                                ; preds = %4
  %8 = and i32 %2, 1024
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %10, label %9

9:                                                ; preds = %7
  tail call void @png_set_invert_alpha(ptr noundef nonnull %0) #2
  br label %10

10:                                               ; preds = %9, %7
  tail call void @png_write_info(ptr noundef nonnull %0, ptr noundef nonnull %1)
  %11 = and i32 %2, 32
  %.not32 = icmp eq i32 %11, 0
  br i1 %.not32, label %13, label %12

12:                                               ; preds = %10
  tail call void @png_set_invert_mono(ptr noundef nonnull %0) #2
  br label %13

13:                                               ; preds = %12, %10
  %14 = and i32 %2, 64
  %.not33 = icmp eq i32 %14, 0
  br i1 %.not33, label %21, label %15

15:                                               ; preds = %13
  %16 = getelementptr inbounds i8, ptr %1, i64 16
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = and i64 %17, 2
  %.not34 = icmp eq i64 %18, 0
  br i1 %.not34, label %21, label %19

19:                                               ; preds = %15
  %20 = getelementptr inbounds i8, ptr %1, i64 96
  tail call void @png_set_shift(ptr noundef nonnull %0, ptr noundef nonnull %20) #2
  br label %21

21:                                               ; preds = %19, %15, %13
  %22 = and i32 %2, 4
  %.not35 = icmp eq i32 %22, 0
  br i1 %.not35, label %24, label %23

23:                                               ; preds = %21
  tail call void @png_set_packing(ptr noundef nonnull %0) #2
  br label %24

24:                                               ; preds = %23, %21
  %25 = and i32 %2, 256
  %.not36 = icmp eq i32 %25, 0
  br i1 %.not36, label %27, label %26

26:                                               ; preds = %24
  tail call void @png_set_swap_alpha(ptr noundef nonnull %0) #2
  br label %27

27:                                               ; preds = %26, %24
  %28 = and i32 %2, 2048
  %.not37 = icmp eq i32 %28, 0
  br i1 %.not37, label %30, label %29

29:                                               ; preds = %27
  tail call void @png_set_filler(ptr noundef nonnull %0, i64 noundef 0, i32 noundef 0) #2
  br label %30

30:                                               ; preds = %29, %27
  %31 = and i32 %2, 128
  %.not38 = icmp eq i32 %31, 0
  br i1 %.not38, label %33, label %32

32:                                               ; preds = %30
  tail call void @png_set_bgr(ptr noundef nonnull %0) #2
  br label %33

33:                                               ; preds = %32, %30
  %34 = and i32 %2, 512
  %.not39 = icmp eq i32 %34, 0
  br i1 %.not39, label %36, label %35

35:                                               ; preds = %33
  tail call void @png_set_swap(ptr noundef nonnull %0) #2
  br label %36

36:                                               ; preds = %35, %33
  %37 = and i32 %2, 8
  %.not40 = icmp eq i32 %37, 0
  br i1 %.not40, label %39, label %38

38:                                               ; preds = %36
  tail call void @png_set_packswap(ptr noundef nonnull %0) #2
  br label %39

39:                                               ; preds = %38, %36
  %40 = getelementptr inbounds i8, ptr %1, i64 16
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = and i64 %41, 32768
  %.not41 = icmp eq i64 %42, 0
  br i1 %.not41, label %46, label %43

43:                                               ; preds = %39
  %44 = getelementptr inbounds i8, ptr %1, i64 384
  %45 = load ptr, ptr %44, align 8, !tbaa !18
  tail call void @png_write_image(ptr noundef nonnull %0, ptr noundef %45)
  br label %46

46:                                               ; preds = %43, %39
  tail call void @png_write_end(ptr noundef nonnull %0, ptr noundef nonnull %1)
  br label %47

47:                                               ; preds = %4, %46
  ret void
}

declare void @png_set_invert_alpha(ptr noundef) local_unnamed_addr #1

declare void @png_set_invert_mono(ptr noundef) local_unnamed_addr #1

declare void @png_set_shift(ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @png_set_packing(ptr noundef) local_unnamed_addr #1

declare void @png_set_swap_alpha(ptr noundef) local_unnamed_addr #1

declare void @png_set_filler(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

declare void @png_set_bgr(ptr noundef) local_unnamed_addr #1

declare void @png_set_swap(ptr noundef) local_unnamed_addr #1

declare void @png_set_packswap(ptr noundef) 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 16}
!6 = !{!"png_info_struct", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !11, i64 40, !11, i64 42, !8, i64 44, !8, i64 45, !8, i64 46, !8, i64 47, !8, i64 48, !8, i64 49, !8, i64 50, !8, i64 51, !8, i64 52, !12, i64 60, !8, i64 64, !13, i64 68, !13, i64 72, !10, i64 80, !14, i64 88, !15, i64 96, !10, i64 104, !16, i64 112, !16, i64 122, !7, i64 136, !7, i64 144, !8, i64 152, !7, i64 160, !7, i64 168, !8, i64 176, !10, i64 184, !12, i64 192, !12, i64 196, !12, i64 200, !12, i64 204, !12, i64 208, !12, i64 212, !12, i64 216, !12, i64 220, !10, i64 224, !7, i64 232, !7, i64 240, !10, i64 248, !10, i64 256, !8, i64 264, !8, i64 265, !7, i64 272, !10, i64 280, !7, i64 288, !10, i64 296, !10, i64 304, !7, i64 312, !8, i64 320, !10, i64 328, !7, i64 336, !8, i64 344, !17, i64 352, !17, i64 360, !10, i64 368, !10, i64 376, !10, i64 384, !7, i64 392, !7, i64 400, !7, i64 408, !7, i64 416, !7, i64 424, !7, i64 432, !7, i64 440, !7, i64 448, !7, i64 456}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"float", !8, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!"png_time_struct", !11, i64 0, !8, i64 2, !8, i64 3, !8, i64 4, !8, i64 5, !8, i64 6}
!15 = !{!"png_color_8_struct", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !8, i64 4}
!16 = !{!"png_color_16_struct", !8, i64 0, !11, i64 2, !11, i64 4, !11, i64 6, !11, i64 8}
!17 = !{!"double", !8, i64 0}
!18 = !{!6, !10, i64 384}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass