; LLVM IR from niessner/Matterport :: code/gaps/pkgs/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 }

@__const.png_write_zTXt.png_zTXt = external hidden unnamed_addr constant [5 x i8], align 1

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; 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
declare dso_local void @png_write_chunk_start(ptr noundef, ptr noundef, i32 noundef) #2

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

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

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

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

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

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

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

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

; 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) #2 {
  %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 [5 x i8], align 1
  %12 = alloca i64, align 8
  %13 = alloca [1 x i8], align 1
  %14 = alloca ptr, align 8
  %15 = alloca %struct.compression_state, align 8
  %16 = 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 5, ptr %11) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %11, ptr align 1 @__const.png_write_zTXt.png_zTXt, i64 5, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #5
  %17 = getelementptr inbounds %struct.compression_state, ptr %15, i32 0, i32 2
  store i32 0, ptr %17, align 4, !tbaa !13
  %18 = getelementptr inbounds %struct.compression_state, ptr %15, i32 0, i32 3
  store i32 0, ptr %18, align 8, !tbaa !15
  %19 = getelementptr inbounds %struct.compression_state, ptr %15, i32 0, i32 4
  store ptr null, ptr %19, align 8, !tbaa !16
  %20 = getelementptr inbounds %struct.compression_state, ptr %15, i32 0, i32 0
  store ptr null, ptr %20, align 8, !tbaa !17
  %21 = getelementptr inbounds %struct.compression_state, ptr %15, i32 0, i32 1
  store i32 0, ptr %21, align 8, !tbaa !18
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = call i64 @png_check_keyword(ptr noundef %22, ptr noundef %23, ptr noundef %14)
  store i64 %24, ptr %12, align 8, !tbaa !9
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %29

26:                                               ; preds = %5
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load ptr, ptr %14, align 8, !tbaa !5
  call void @png_free(ptr noundef %27, ptr noundef %28)
  store i32 1, ptr %16, align 4
  br label %75

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

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

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

40:                                               ; preds = %37, %32, %29
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %14, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  call void @png_write_tEXt(ptr noundef %41, ptr noundef %42, ptr noundef %43, i64 noundef 0)
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %14, align 8, !tbaa !5
  call void @png_free(ptr noundef %44, ptr noundef %45)
  store i32 1, ptr %16, align 4
  br label %75

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

75:                                               ; preds = %46, %40, %26
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 5, ptr %11) #5
  %76 = load i32, ptr %16, align 4
  switch i32 %76, label %78 [
    i32 0, label %77
    i32 1, label %77
  ]

77:                                               ; preds = %75, %75
  ret void

78:                                               ; preds = %75
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 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 #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, !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 }

@__const.png_write_zTXt.png_zTXt = external hidden unnamed_addr constant [5 x i8], align 1

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @png_write_chunk_end(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 #2

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

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

; Function Attrs: nounwind uwtable
declare hidden void @png_write_compressed_data_out(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 #4

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

; 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 #2 {
  %6 = alloca [5 x i8], align 1
  %7 = alloca [1 x i8], align 1
  %8 = alloca ptr, align 8
  %9 = alloca %struct.compression_state, align 8
  call void @llvm.lifetime.start.p0(i64 5, ptr nonnull %6) #6
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(5) %6, ptr noundef nonnull align 1 dereferenceable(5) @__const.png_write_zTXt.png_zTXt, i64 5, i1 false)
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %7) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #6
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %9) #6
  %10 = getelementptr inbounds i8, ptr %9, i64 24
  store ptr null, ptr %10, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(20) %9, i8 0, i64 20, i1 false)
  %11 = call i64 @png_check_keyword(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %8)
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %5
  %14 = load ptr, ptr %8, align 8, !tbaa !11
  call void @png_free(ptr noundef %0, ptr noundef %14) #6
  br label %34

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

17:                                               ; preds = %15
  %18 = load i8, ptr %2, align 1, !tbaa !12
  %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 %8, align 8, !tbaa !11
  call void @png_write_tEXt(ptr noundef %0, ptr noundef %22, ptr noundef %2, i64 noundef 0)
  %23 = load ptr, ptr %8, align 8, !tbaa !11
  call void @png_free(ptr noundef %0, ptr noundef %23) #6
  br label %34

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

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { 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 #5 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!7, !7, i64 0}
!12 = !{!8, !8, i64 0}


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