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

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

@.str.51 = external hidden unnamed_addr constant [28 x i8], align 1
@png_zTXt = external global [5 x i8], align 1

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

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

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

; 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

declare void @png_warning(ptr noundef, ptr noundef) #2

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

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

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

declare void @png_free(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
define dso_local void @png_write_zTXt(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i32 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca [1 x i8], align 1
  %13 = alloca ptr, align 8
  %14 = alloca %struct.compression_state, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr %14) #4
  %16 = getelementptr inbounds %struct.compression_state, ptr %14, i32 0, i32 2
  store i32 0, ptr %16, align 4, !tbaa !13
  %17 = getelementptr inbounds %struct.compression_state, ptr %14, i32 0, i32 3
  store i32 0, ptr %17, align 8, !tbaa !15
  %18 = getelementptr inbounds %struct.compression_state, ptr %14, i32 0, i32 4
  store ptr null, ptr %18, align 8, !tbaa !16
  %19 = getelementptr inbounds %struct.compression_state, ptr %14, i32 0, i32 0
  store ptr null, ptr %19, align 8, !tbaa !17
  %20 = getelementptr inbounds %struct.compression_state, ptr %14, i32 0, i32 1
  store i32 0, ptr %20, align 8, !tbaa !18
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %28, label %23

23:                                               ; preds = %5
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = call i64 @png_check_keyword(ptr noundef %24, ptr noundef %25, ptr noundef %13)
  store i64 %26, ptr %11, align 8, !tbaa !9
  %27 = icmp eq i64 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %23, %5
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  call void @png_warning(ptr noundef %29, ptr noundef @.str.51)
  store i32 1, ptr %15, align 4
  br label %74

30:                                               ; preds = %23
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = icmp eq ptr %31, null
  br i1 %32, label %41, label %33

33:                                               ; preds = %30
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = load i8, ptr %34, align 1, !tbaa !19
  %36 = sext i8 %35 to i32
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %41, label %38

38:                                               ; preds = %33
  %39 = load i32, ptr %10, align 4, !tbaa !11
  %40 = icmp eq i32 %39, -1
  br i1 %40, label %41, label %47

41:                                               ; preds = %38, %33, %30
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %13, align 8, !tbaa !5
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  call void @png_write_tEXt(ptr noundef %42, ptr noundef %43, ptr noundef %44, i64 noundef 0)
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %13, align 8, !tbaa !5
  call void @png_free(ptr noundef %45, ptr noundef %46)
  store i32 1, ptr %15, align 4
  br label %74

47:                                               ; preds = %38
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = call i64 @strlen(ptr noundef %48) #5
  store i64 %49, ptr %9, align 8, !tbaa !9
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = load i64, ptr %9, align 8, !tbaa !9
  %53 = load i32, ptr %10, align 4, !tbaa !11
  %54 = call i32 @png_text_compress(ptr noundef %50, ptr noundef %51, i64 noundef %52, i32 noundef %53, ptr noundef %14)
  %55 = sext i32 %54 to i64
  store i64 %55, ptr %9, align 8, !tbaa !9
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load i64, ptr %11, align 8, !tbaa !9
  %58 = load i64, ptr %9, align 8, !tbaa !9
  %59 = add i64 %57, %58
  %60 = add i64 %59, 2
  call void @png_write_chunk_start(ptr noundef %56, ptr noundef @png_zTXt, i64 noundef %60)
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  %63 = load i64, ptr %11, align 8, !tbaa !9
  %64 = add i64 %63, 1
  call void @png_write_chunk_data(ptr noundef %61, ptr noundef %62, i64 noundef %64)
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = load ptr, ptr %13, align 8, !tbaa !5
  call void @png_free(ptr noundef %65, ptr noundef %66)
  %67 = load i32, ptr %10, align 4, !tbaa !11
  %68 = trunc i32 %67 to i8
  %69 = getelementptr inbounds [1 x i8], ptr %12, i64 0, i64 0
  store i8 %68, ptr %69, align 1, !tbaa !19
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds [1 x i8], ptr %12, i64 0, i64 0
  call void @png_write_chunk_data(ptr noundef %70, ptr noundef %71, i64 noundef 1)
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  call void @png_write_compressed_data_out(ptr noundef %72, ptr noundef %14)
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  call void @png_write_chunk_end(ptr noundef %73)
  store i32 0, ptr %15, align 4
  br label %74

74:                                               ; preds = %47, %41, %28
  call void @llvm.lifetime.end.p0(i64 32, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  %75 = load i32, ptr %15, align 4
  switch i32 %75, label %77 [
    i32 0, label %76
    i32 1, label %76
  ]

76:                                               ; preds = %74, %74
  ret void

77:                                               ; preds = %74
  unreachable
}

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 = { "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 willreturn memory(read) "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 = { nounwind willreturn memory(read) }

!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 12}
!14 = !{!"", !6, i64 0, !12, i64 8, !12, i64 12, !12, i64 16, !6, i64 24}
!15 = !{!14, !12, i64 16}
!16 = !{!14, !6, i64 24}
!17 = !{!14, !6, i64 0}
!18 = !{!14, !12, i64 8}
!19 = !{!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"

%struct.compression_state = type { ptr, i32, i32, i32, ptr }

@.str.51 = external hidden unnamed_addr constant [28 x i8], align 1
@png_zTXt = external global [5 x i8], align 1

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

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

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

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

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

declare void @png_warning(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

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

declare void @png_free(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #3

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

; Function Attrs: nounwind uwtable
define dso_local void @png_write_zTXt(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = alloca [1 x i8], align 1
  %7 = alloca ptr, align 8
  %8 = alloca %struct.compression_state, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %6) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %8) #5
  %9 = getelementptr inbounds i8, ptr %8, i64 24
  store ptr null, ptr %9, align 8, !tbaa !5
  %10 = icmp eq ptr %1, null
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(20) %8, i8 0, i64 20, i1 false)
  br i1 %10, label %14, label %11

11:                                               ; preds = %5
  %12 = call i64 @png_check_keyword(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %7)
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %11, %5
  call void @png_warning(ptr noundef %0, ptr noundef nonnull @.str.51) #5
  br label %34

15:                                               ; preds = %11
  %16 = icmp eq ptr %2, null
  br i1 %16, label %21, label %17

17:                                               ; preds = %15
  %18 = load i8, ptr %2, align 1, !tbaa !11
  %19 = icmp eq i8 %18, 0
  %20 = icmp eq i32 %4, -1
  %or.cond = or i1 %20, %19
  br i1 %or.cond, label %21, label %24

21:                                               ; preds = %17, %15
  %22 = load ptr, ptr %7, align 8, !tbaa !12
  call void @png_write_tEXt(ptr noundef %0, ptr noundef %22, ptr noundef %2, i64 noundef 0)
  %23 = load ptr, ptr %7, align 8, !tbaa !12
  call void @png_free(ptr noundef %0, ptr noundef %23) #5
  br label %34

24:                                               ; preds = %17
  %25 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %2) #6
  %26 = call i32 @png_text_compress(ptr noundef %0, ptr noundef nonnull %2, i64 noundef %25, i32 noundef %4, ptr noundef nonnull %8)
  %27 = sext i32 %26 to i64
  %28 = add i64 %12, 2
  %29 = add i64 %28, %27
  call void @png_write_chunk_start(ptr noundef %0, ptr noundef nonnull @png_zTXt, i64 noundef %29)
  %30 = load ptr, ptr %7, align 8, !tbaa !12
  %31 = add i64 %12, 1
  call void @png_write_chunk_data(ptr noundef %0, ptr noundef %30, i64 noundef %31)
  %32 = load ptr, ptr %7, align 8, !tbaa !12
  call void @png_free(ptr noundef %0, ptr noundef %32) #5
  %33 = trunc i32 %4 to i8
  store i8 %33, ptr %6, align 1, !tbaa !11
  call void @png_write_chunk_data(ptr noundef %0, ptr noundef nonnull %6, i64 noundef 1)
  call void @png_write_compressed_data_out(ptr noundef %0, ptr noundef nonnull %8)
  call void @png_write_chunk_end(ptr noundef %0)
  br label %34

34:                                               ; preds = %24, %21, %14
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %6) #5
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 nofree nounwind willreturn memory(argmem: read) "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 24}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!8, !8, i64 0}
!12 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,ReassociatePass,SROAPass,SimplifyCFGPass