; LLVM IR from aagdev/mlimgsynth :: src/ccommon (image_io_png)

; 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.CodecPng = type { ptr, i32 }
%struct.CodecPngText = type { ptr, ptr }

@g_allocator_dopt = external global ptr, align 8

; 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: inlinehint nounwind uwtable
declare hidden i32 @dynbuf_count(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @imgio_png_value_set(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) #2 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  %14 = load i32, ptr %9, align 4, !tbaa !9
  switch i32 %14, label %68 [
    i32 2, label %15
    i32 7, label %24
  ]

15:                                               ; preds = %5
  %16 = load ptr, ptr %10, align 8, !tbaa !5
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = icmp ne i32 %17, 0
  %19 = xor i1 %18, true
  %20 = xor i1 %19, true
  %21 = zext i1 %20 to i32
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.CodecPng, ptr %22, i32 0, i32 1
  store i32 %21, ptr %23, align 8, !tbaa !11
  br label %69

24:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %25 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %25, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %26, ptr %13, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %32, %24
  %28 = load ptr, ptr %13, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i32 1
  store ptr %29, ptr %13, align 8, !tbaa !5
  %30 = load i8, ptr %28, align 1, !tbaa !13
  %31 = icmp ne i8 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %27
  br label %27, !llvm.loop !14

33:                                               ; preds = %27
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.CodecPng, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %37 = call ptr @dynbuf_append(ptr noundef %35, i32 noundef 1, ptr noundef null, i64 noundef 16, i32 noundef 2, ptr noundef %36)
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.CodecPng, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.CodecPng, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !16
  %44 = call i32 @dynbuf_count(ptr noundef %43)
  %45 = sub i32 %44, 1
  %46 = sub i32 %45, 0
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds %struct.CodecPngText, ptr %40, i64 %47
  %49 = getelementptr inbounds %struct.CodecPngText, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %12, align 8, !tbaa !5
  %51 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %52 = call i32 @dynstr_copyz(ptr noundef %49, ptr noundef %50, ptr noundef %51)
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.CodecPng, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !16
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.CodecPng, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !16
  %59 = call i32 @dynbuf_count(ptr noundef %58)
  %60 = sub i32 %59, 1
  %61 = sub i32 %60, 0
  %62 = zext i32 %61 to i64
  %63 = getelementptr inbounds %struct.CodecPngText, ptr %55, i64 %62
  %64 = getelementptr inbounds %struct.CodecPngText, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %13, align 8, !tbaa !5
  %66 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %67 = call i32 @dynstr_copyz(ptr noundef %64, ptr noundef %65, ptr noundef %66)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %69

68:                                               ; preds = %5
  store i32 -785, ptr %6, align 4
  br label %70

69:                                               ; preds = %33, %15
  store i32 0, ptr %6, align 4
  br label %70

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_append(ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @dynstr_copyz(ptr noundef, ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"CodecPng", !6, i64 0, !10, i64 8}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !6, 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"

%struct.CodecPngText = type { ptr, ptr }

@g_allocator_dopt = external local_unnamed_addr global ptr, align 8

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @dynbuf_count(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -785, 1) i32 @imgio_png_value_set(ptr noundef %0, ptr nocapture noundef readnone %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  switch i32 %2, label %30 [
    i32 2, label %6
    i32 7, label %.preheader
  ]

6:                                                ; preds = %5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = icmp ne i32 %7, 0
  %9 = zext i1 %8 to i32
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %9, ptr %10, align 8, !tbaa !9
  br label %30

.preheader:                                       ; preds = %5, %.preheader
  %.0 = phi ptr [ %11, %.preheader ], [ %3, %5 ]
  %11 = getelementptr inbounds i8, ptr %.0, i64 1
  %12 = load i8, ptr %.0, align 1, !tbaa !12
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %13, label %.preheader, !llvm.loop !13

13:                                               ; preds = %.preheader
  %14 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !15
  %15 = tail call ptr @dynbuf_append(ptr noundef %0, i32 noundef 1, ptr noundef null, i64 noundef 16, i32 noundef 2, ptr noundef %14)
  %16 = load ptr, ptr %0, align 8, !tbaa !16
  %17 = tail call i32 @dynbuf_count(ptr noundef %16)
  %18 = add i32 %17, -1
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds %struct.CodecPngText, ptr %16, i64 %19
  %21 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !15
  %22 = tail call i32 @dynstr_copyz(ptr noundef %20, ptr noundef %3, ptr noundef %21)
  %23 = load ptr, ptr %0, align 8, !tbaa !16
  %24 = tail call i32 @dynbuf_count(ptr noundef %23)
  %25 = add i32 %24, -1
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds %struct.CodecPngText, ptr %23, i64 %26, i32 1
  %28 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !15
  %29 = tail call i32 @dynstr_copyz(ptr noundef nonnull %27, ptr noundef nonnull %11, ptr noundef %28)
  br label %30

30:                                               ; preds = %6, %13, %5
  %.013 = phi i32 [ -785, %5 ], [ 0, %13 ], [ 0, %6 ]
  ret i32 %.013
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_append(ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

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

attributes #0 = { inlinehint 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 = { 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"CodecPng", !11, i64 0, !6, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass