; LLVM IR from zchrissirhcz/pixel :: legacy (px_png_libpng)

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

@.str.2 = external hidden unnamed_addr constant [75 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [19 x i8], align 1
@__PRETTY_FUNCTION__.px_write_png = external hidden unnamed_addr constant [73 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

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

declare noalias ptr @png_create_info_struct(ptr noundef) #1

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

declare i64 @png_get_rowbytes(ptr noundef, ptr noundef) #1

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

declare i32 @fclose(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @px_write_png(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) #3 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  store i64 %4, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = call noalias ptr @fopen(ptr noundef %15, ptr noundef @.str.4)
  store ptr %16, ptr %11, align 8, !tbaa !5
  %17 = load ptr, ptr %11, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %20

19:                                               ; preds = %5
  br label %21

20:                                               ; preds = %5
  call void @__assert_fail(ptr noundef @.str.5, ptr noundef @.str.2, i32 noundef 35, ptr noundef @__PRETTY_FUNCTION__.px_write_png) #5
  unreachable

21:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %22 = call noalias ptr @png_create_write_struct(ptr noundef @.str.3, ptr noundef null, ptr noundef null, ptr noundef null)
  store ptr %22, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %23 = load ptr, ptr %12, align 8, !tbaa !5
  %24 = call noalias ptr @png_create_info_struct(ptr noundef %23)
  store ptr %24, ptr %13, align 8, !tbaa !5
  %25 = load ptr, ptr %12, align 8, !tbaa !5
  %26 = load ptr, ptr %11, align 8, !tbaa !5
  call void @png_init_io(ptr noundef %25, ptr noundef %26)
  %27 = load ptr, ptr %12, align 8, !tbaa !5
  %28 = load ptr, ptr %13, align 8, !tbaa !5
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = trunc i64 %29 to i32
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = trunc i64 %31 to i32
  %33 = load i64, ptr %10, align 8, !tbaa !9
  %34 = trunc i64 %33 to i32
  call void @png_set_IHDR(ptr noundef %27, ptr noundef %28, i32 noundef %30, i32 noundef %32, i32 noundef %34, i32 noundef 0, i32 noundef 0, i32 noundef 0, i32 noundef 0)
  %35 = load ptr, ptr %12, align 8, !tbaa !5
  %36 = load ptr, ptr %13, align 8, !tbaa !5
  call void @png_write_info(ptr noundef %35, ptr noundef %36)
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store i32 0, ptr %14, align 4, !tbaa !11
  br label %37

37:                                               ; preds = %53, %21
  %38 = load i32, ptr %14, align 4, !tbaa !11
  %39 = zext i32 %38 to i64
  %40 = load i64, ptr %9, align 8, !tbaa !9
  %41 = icmp ult i64 %39, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  br label %56

43:                                               ; preds = %37
  %44 = load ptr, ptr %12, align 8, !tbaa !5
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = load i32, ptr %14, align 4, !tbaa !11
  %47 = zext i32 %46 to i64
  %48 = load ptr, ptr %12, align 8, !tbaa !5
  %49 = load ptr, ptr %13, align 8, !tbaa !5
  %50 = call i64 @png_get_rowbytes(ptr noundef %48, ptr noundef %49)
  %51 = mul i64 %47, %50
  %52 = getelementptr inbounds i8, ptr %45, i64 %51
  call void @png_write_row(ptr noundef %44, ptr noundef %52)
  br label %53

53:                                               ; preds = %43
  %54 = load i32, ptr %14, align 4, !tbaa !11
  %55 = add i32 %54, 1
  store i32 %55, ptr %14, align 4, !tbaa !11
  br label %37, !llvm.loop !13

56:                                               ; preds = %42
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  call void @png_write_end(ptr noundef %57, ptr noundef null)
  call void @png_destroy_write_struct(ptr noundef %12, ptr noundef %13)
  %58 = load ptr, ptr %11, align 8, !tbaa !5
  %59 = call i32 @fclose(ptr noundef %58)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  ret void
}

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

declare void @png_set_IHDR(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) #1

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

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

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

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

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 = { noreturn 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 = { 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 #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; 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.2 = external hidden unnamed_addr constant [75 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [19 x i8], align 1
@__PRETTY_FUNCTION__.px_write_png = external hidden unnamed_addr constant [73 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: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

declare noalias ptr @png_create_info_struct(ptr noundef) local_unnamed_addr #3

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

declare i64 @png_get_rowbytes(ptr noundef, ptr noundef) local_unnamed_addr #3

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

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

; Function Attrs: nounwind uwtable
define dso_local void @px_write_png(ptr nocapture noundef readonly %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) local_unnamed_addr #4 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = tail call noalias ptr @fopen(ptr noundef %0, ptr noundef nonnull @.str.4)
  %.not = icmp eq ptr %8, null
  br i1 %.not, label %9, label %10

9:                                                ; preds = %5
  tail call void @__assert_fail(ptr noundef nonnull @.str.5, ptr noundef nonnull @.str.2, i32 noundef 35, ptr noundef nonnull @__PRETTY_FUNCTION__.px_write_png) #5
  unreachable

10:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #6
  %11 = tail call noalias ptr @png_create_write_struct(ptr noundef nonnull @.str.3, ptr noundef null, ptr noundef null, ptr noundef null) #6
  store ptr %11, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #6
  %12 = tail call noalias ptr @png_create_info_struct(ptr noundef %11) #6
  store ptr %12, ptr %7, align 8, !tbaa !5
  tail call void @png_init_io(ptr noundef %11, ptr noundef nonnull %8) #6
  %13 = trunc i64 %2 to i32
  %14 = trunc i64 %3 to i32
  %15 = trunc i64 %4 to i32
  tail call void @png_set_IHDR(ptr noundef %11, ptr noundef %12, i32 noundef %13, i32 noundef %14, i32 noundef %15, i32 noundef 0, i32 noundef 0, i32 noundef 0, i32 noundef 0) #6
  tail call void @png_write_info(ptr noundef %11, ptr noundef %12) #6
  %.not14 = icmp eq i64 %3, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %10
  tail call void @png_write_end(ptr noundef %11, ptr noundef null) #6
  call void @png_destroy_write_struct(ptr noundef nonnull %6, ptr noundef nonnull %7) #6
  %16 = call i32 @fclose(ptr noundef nonnull %8)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #6
  ret void

.lr.ph:                                           ; preds = %10, %.lr.ph
  %17 = phi i64 [ %22, %.lr.ph ], [ 0, %10 ]
  %.013 = phi i32 [ %21, %.lr.ph ], [ 0, %10 ]
  %18 = tail call i64 @png_get_rowbytes(ptr noundef %11, ptr noundef %12) #6
  %19 = mul i64 %18, %17
  %20 = getelementptr inbounds i8, ptr %1, i64 %19
  tail call void @png_write_row(ptr noundef %11, ptr noundef %20) #6
  %21 = add i32 %.013, 1
  %22 = zext i32 %21 to i64
  %23 = icmp ult i64 %22, %3
  br i1 %23, label %.lr.ph, label %._crit_edge, !llvm.loop !9
}

declare noalias ptr @png_create_write_struct(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @png_set_IHDR(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

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

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

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { noreturn 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 = { "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 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 #5 = { noreturn nounwind }
attributes #6 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass