; LLVM IR from Try/Tempest :: Engine/thirdparty/libpng (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_image = type { ptr, i32, i32, i32, i32, i32, i32, i32, [64 x i8] }

@.str.19 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [53 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare i32 @png_image_error(ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @png_image_write_to_file(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, ptr noundef %5) #2 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store i32 %2, ptr %10, align 4, !tbaa !9
  store ptr %3, ptr %11, align 8, !tbaa !5
  store i32 %4, ptr %12, align 4, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %88

19:                                               ; preds = %6
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.png_image, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = icmp eq i32 %22, 1
  br i1 %23, label %24, label %88

24:                                               ; preds = %19
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %85

27:                                               ; preds = %24
  %28 = load ptr, ptr %11, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %85

30:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = call noalias ptr @fopen(ptr noundef %31, ptr noundef @.str.19)
  store ptr %32, ptr %14, align 8, !tbaa !5
  %33 = load ptr, ptr %14, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %78

35:                                               ; preds = %30
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = load ptr, ptr %14, align 8, !tbaa !5
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = load i32, ptr %12, align 4, !tbaa !9
  %41 = load ptr, ptr %13, align 8, !tbaa !5
  %42 = call i32 @png_image_write_to_stdio(ptr noundef %36, ptr noundef %37, i32 noundef %38, ptr noundef %39, i32 noundef %40, ptr noundef %41)
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %73

44:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %45 = load ptr, ptr %14, align 8, !tbaa !5
  %46 = call i32 @fflush(ptr noundef %45)
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %60

48:                                               ; preds = %44
  %49 = load ptr, ptr %14, align 8, !tbaa !5
  %50 = call i32 @ferror(ptr noundef %49) #5
  %51 = icmp eq i32 %50, 0
  br i1 %51, label %52, label %60

52:                                               ; preds = %48
  %53 = load ptr, ptr %14, align 8, !tbaa !5
  %54 = call i32 @fclose(ptr noundef %53)
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %52
  store i32 1, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %72

57:                                               ; preds = %52
  %58 = call ptr @__errno_location() #6
  %59 = load i32, ptr %58, align 4, !tbaa !9
  store i32 %59, ptr %15, align 4, !tbaa !9
  br label %65

60:                                               ; preds = %48, %44
  %61 = call ptr @__errno_location() #6
  %62 = load i32, ptr %61, align 4, !tbaa !9
  store i32 %62, ptr %15, align 4, !tbaa !9
  %63 = load ptr, ptr %14, align 8, !tbaa !5
  %64 = call i32 @fclose(ptr noundef %63)
  br label %65

65:                                               ; preds = %60, %57
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = call i32 @remove(ptr noundef %66) #5
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = load i32, ptr %15, align 4, !tbaa !9
  %70 = call ptr @strerror(i32 noundef %69) #5
  %71 = call i32 @png_image_error(ptr noundef %68, ptr noundef %70)
  store i32 %71, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %72

72:                                               ; preds = %65, %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  br label %84

73:                                               ; preds = %35
  %74 = load ptr, ptr %14, align 8, !tbaa !5
  %75 = call i32 @fclose(ptr noundef %74)
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  %77 = call i32 @remove(ptr noundef %76) #5
  store i32 0, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %84

78:                                               ; preds = %30
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  %80 = call ptr @__errno_location() #6
  %81 = load i32, ptr %80, align 4, !tbaa !9
  %82 = call ptr @strerror(i32 noundef %81) #5
  %83 = call i32 @png_image_error(ptr noundef %79, ptr noundef %82)
  store i32 %83, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %84

84:                                               ; preds = %78, %73, %72
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  br label %95

85:                                               ; preds = %27, %24
  %86 = load ptr, ptr %8, align 8, !tbaa !5
  %87 = call i32 @png_image_error(ptr noundef %86, ptr noundef @.str.20)
  store i32 %87, ptr %7, align 4
  br label %95

88:                                               ; preds = %19, %6
  %89 = load ptr, ptr %8, align 8, !tbaa !5
  %90 = icmp ne ptr %89, null
  br i1 %90, label %91, label %94

91:                                               ; preds = %88
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  %93 = call i32 @png_image_error(ptr noundef %92, ptr noundef @.str.21)
  store i32 %93, ptr %7, align 4
  br label %95

94:                                               ; preds = %88
  store i32 0, ptr %7, align 4
  br label %95

95:                                               ; preds = %94, %91, %85, %84
  %96 = load i32, ptr %7, align 4
  ret i32 %96
}

declare noalias ptr @fopen(ptr noundef, ptr noundef) #1

declare i32 @fflush(ptr noundef) #1

; Function Attrs: nounwind
declare i32 @ferror(ptr noundef) #3

declare i32 @fclose(ptr noundef) #1

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #4

; Function Attrs: nounwind
declare i32 @remove(ptr noundef) #3

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) #3

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

!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 = !{!"", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !7, i64 36}


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

@.str.19 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [53 x i8], align 1

declare i32 @png_image_error(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @png_image_write_to_stdio(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @png_image_write_to_file(ptr noundef %0, ptr noundef readonly %1, i32 noundef %2, ptr noundef %3, i32 noundef %4, ptr noundef %5) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.critedge, label %7

7:                                                ; preds = %6
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = load i32, ptr %8, align 8, !tbaa !5
  %10 = icmp eq i32 %9, 1
  br i1 %10, label %11, label %48

11:                                               ; preds = %7
  %12 = icmp ne ptr %1, null
  %13 = icmp ne ptr %3, null
  %or.cond = and i1 %12, %13
  br i1 %or.cond, label %14, label %46

14:                                               ; preds = %11
  %15 = tail call noalias ptr @fopen(ptr noundef nonnull %1, ptr noundef nonnull @.str.19)
  %.not32 = icmp eq ptr %15, null
  br i1 %.not32, label %41, label %16

16:                                               ; preds = %14
  %17 = tail call i32 @png_image_write_to_stdio(ptr noundef nonnull %0, ptr noundef nonnull %15, i32 noundef %2, ptr noundef nonnull %3, i32 noundef %4, ptr noundef %5)
  %.not33 = icmp eq i32 %17, 0
  br i1 %.not33, label %38, label %18

18:                                               ; preds = %16
  %19 = tail call i32 @fflush(ptr noundef nonnull %15)
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %30

21:                                               ; preds = %18
  %22 = tail call i32 @ferror(ptr noundef nonnull %15) #6
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %30

24:                                               ; preds = %21
  %25 = tail call i32 @fclose(ptr noundef nonnull %15)
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %.critedge, label %27

27:                                               ; preds = %24
  %28 = tail call ptr @__errno_location() #7
  %29 = load i32, ptr %28, align 4, !tbaa !11
  br label %34

30:                                               ; preds = %21, %18
  %31 = tail call ptr @__errno_location() #7
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = tail call i32 @fclose(ptr noundef nonnull %15)
  br label %34

34:                                               ; preds = %30, %27
  %.0 = phi i32 [ %29, %27 ], [ %32, %30 ]
  %35 = tail call i32 @remove(ptr noundef nonnull %1) #6
  %36 = tail call ptr @strerror(i32 noundef %.0) #6
  %37 = tail call i32 @png_image_error(ptr noundef nonnull %0, ptr noundef %36) #6
  br label %.critedge

38:                                               ; preds = %16
  %39 = tail call i32 @fclose(ptr noundef nonnull %15)
  %40 = tail call i32 @remove(ptr noundef nonnull %1) #6
  br label %.critedge

41:                                               ; preds = %14
  %42 = tail call ptr @__errno_location() #7
  %43 = load i32, ptr %42, align 4, !tbaa !11
  %44 = tail call ptr @strerror(i32 noundef %43) #6
  %45 = tail call i32 @png_image_error(ptr noundef nonnull %0, ptr noundef %44) #6
  br label %.critedge

46:                                               ; preds = %11
  %47 = tail call i32 @png_image_error(ptr noundef nonnull %0, ptr noundef nonnull @.str.20) #6
  br label %.critedge

48:                                               ; preds = %7
  %49 = tail call i32 @png_image_error(ptr noundef nonnull %0, ptr noundef nonnull @.str.21) #6
  br label %.critedge

.critedge:                                        ; preds = %6, %38, %41, %24, %34, %48, %46
  %.2 = phi i32 [ %47, %46 ], [ %49, %48 ], [ 0, %38 ], [ %45, %41 ], [ %37, %34 ], [ 1, %24 ], [ 0, %6 ]
  ret i32 %.2
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fflush(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind memory(read)
declare noundef i32 @ferror(ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef i32 @remove(ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) local_unnamed_addr #5

attributes #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" }
attributes #2 = { nofree nounwind "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 = { nofree nounwind 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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 "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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(none) }

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass