; LLVM IR from mkazhdan/DMG :: Util/ZLIB (gzio)

; 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.gz_stream = type { %struct.z_stream_s, i32, i32, ptr, ptr, ptr, i64, ptr, ptr, i32, i8, i64 }
%struct.z_stream_s = type { ptr, i32, i64, ptr, i32, i64, ptr, ptr, ptr, ptr, ptr, i32, i64, i64 }

; 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 i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #1

declare i64 @crc32(i64 noundef, ptr noundef, i32 noundef) #1

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

13:                                               ; preds = %3
  %14 = load ptr, ptr %8, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.gz_stream, ptr %14, i32 0, i32 10
  %16 = load i8, ptr %15, align 4, !tbaa !11
  %17 = sext i8 %16 to i32
  %18 = icmp ne i32 %17, 119
  br i1 %18, label %19, label %20

19:                                               ; preds = %13, %3
  store i32 -2, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %90

20:                                               ; preds = %13
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.gz_stream, ptr %22, i32 0, i32 0
  %24 = getelementptr inbounds %struct.z_stream_s, ptr %23, i32 0, i32 0
  store ptr %21, ptr %24, align 8, !tbaa !15
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.gz_stream, ptr %26, i32 0, i32 0
  %28 = getelementptr inbounds %struct.z_stream_s, ptr %27, i32 0, i32 1
  store i32 %25, ptr %28, align 8, !tbaa !16
  br label %29

29:                                               ; preds = %74, %20
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.gz_stream, ptr %30, i32 0, i32 0
  %32 = getelementptr inbounds %struct.z_stream_s, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 8, !tbaa !16
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %75

35:                                               ; preds = %29
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.gz_stream, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds %struct.z_stream_s, ptr %37, i32 0, i32 4
  %39 = load i32, ptr %38, align 8, !tbaa !17
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %63

41:                                               ; preds = %35
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.gz_stream, ptr %42, i32 0, i32 5
  %44 = load ptr, ptr %43, align 8, !tbaa !18
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.gz_stream, ptr %45, i32 0, i32 0
  %47 = getelementptr inbounds %struct.z_stream_s, ptr %46, i32 0, i32 3
  store ptr %44, ptr %47, align 8, !tbaa !19
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.gz_stream, ptr %48, i32 0, i32 5
  %50 = load ptr, ptr %49, align 8, !tbaa !18
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.gz_stream, ptr %51, i32 0, i32 3
  %53 = load ptr, ptr %52, align 8, !tbaa !20
  %54 = call i64 @fwrite(ptr noundef %50, i64 noundef 1, i64 noundef 16384, ptr noundef %53)
  %55 = icmp ne i64 %54, 16384
  br i1 %55, label %56, label %59

56:                                               ; preds = %41
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.gz_stream, ptr %57, i32 0, i32 1
  store i32 -1, ptr %58, align 8, !tbaa !21
  br label %75

59:                                               ; preds = %41
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.gz_stream, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds %struct.z_stream_s, ptr %61, i32 0, i32 4
  store i32 16384, ptr %62, align 8, !tbaa !17
  br label %63

63:                                               ; preds = %59, %35
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.gz_stream, ptr %64, i32 0, i32 0
  %66 = call i32 @deflate(ptr noundef %65, i32 noundef 0)
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.gz_stream, ptr %67, i32 0, i32 1
  store i32 %66, ptr %68, align 8, !tbaa !21
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.gz_stream, ptr %69, i32 0, i32 1
  %71 = load i32, ptr %70, align 8, !tbaa !21
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %74

73:                                               ; preds = %63
  br label %75

74:                                               ; preds = %63
  br label %29, !llvm.loop !22

75:                                               ; preds = %73, %56, %29
  %76 = load ptr, ptr %8, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.gz_stream, ptr %76, i32 0, i32 6
  %78 = load i64, ptr %77, align 8, !tbaa !24
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = load i32, ptr %7, align 4, !tbaa !9
  %81 = call i64 @crc32(i64 noundef %78, ptr noundef %79, i32 noundef %80)
  %82 = load ptr, ptr %8, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.gz_stream, ptr %82, i32 0, i32 6
  store i64 %81, ptr %83, align 8, !tbaa !24
  %84 = load i32, ptr %7, align 4, !tbaa !9
  %85 = load ptr, ptr %8, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.gz_stream, ptr %85, i32 0, i32 0
  %87 = getelementptr inbounds %struct.z_stream_s, ptr %86, i32 0, i32 1
  %88 = load i32, ptr %87, align 8, !tbaa !16
  %89 = sub i32 %84, %88
  store i32 %89, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %90

90:                                               ; preds = %75, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %91 = load i32, ptr %4, align 4
  ret i32 %91
}

declare i32 @deflate(ptr noundef, i32 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 = { 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, !7, i64 172}
!12 = !{!"gz_stream", !13, i64 0, !10, i64 112, !10, i64 116, !6, i64 120, !6, i64 128, !6, i64 136, !14, i64 144, !6, i64 152, !6, i64 160, !10, i64 168, !7, i64 172, !14, i64 176}
!13 = !{!"z_stream_s", !6, i64 0, !10, i64 8, !14, i64 16, !6, i64 24, !10, i64 32, !14, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !14, i64 96, !14, i64 104}
!14 = !{!"long", !7, i64 0}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !10, i64 8}
!17 = !{!12, !10, i64 32}
!18 = !{!12, !6, i64 136}
!19 = !{!12, !6, i64 24}
!20 = !{!12, !6, i64 120}
!21 = !{!12, !10, i64 112}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = !{!12, !14, i64 144}


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

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #0

declare i64 @crc32(i64 noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @gzwrite(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %31, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 172
  %7 = load i8, ptr %6, align 4, !tbaa !5
  %.not = icmp eq i8 %7, 119
  br i1 %.not, label %8, label %31

8:                                                ; preds = %5
  store ptr %1, ptr %0, align 8, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %2, ptr %9, align 8, !tbaa !14
  %.not2528 = icmp eq i32 %2, 0
  br i1 %.not2528, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %8
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %11 = getelementptr inbounds i8, ptr %0, i64 136
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  %13 = getelementptr inbounds i8, ptr %0, i64 120
  %14 = getelementptr inbounds i8, ptr %0, i64 112
  br label %15

thread-pre-split:                                 ; preds = %24
  %.pr = load i32, ptr %9, align 8, !tbaa !14
  %.not25 = icmp eq i32 %.pr, 0
  br i1 %.not25, label %.loopexit, label %15

15:                                               ; preds = %.lr.ph, %thread-pre-split
  %16 = load i32, ptr %10, align 8, !tbaa !15
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %24

18:                                               ; preds = %15
  %19 = load ptr, ptr %11, align 8, !tbaa !16
  store ptr %19, ptr %12, align 8, !tbaa !17
  %20 = load ptr, ptr %13, align 8, !tbaa !18
  %21 = tail call i64 @fwrite(ptr noundef %19, i64 noundef 1, i64 noundef 16384, ptr noundef %20)
  %.not26 = icmp eq i64 %21, 16384
  br i1 %.not26, label %23, label %22

22:                                               ; preds = %18
  store i32 -1, ptr %14, align 8, !tbaa !19
  br label %.loopexit

23:                                               ; preds = %18
  store i32 16384, ptr %10, align 8, !tbaa !15
  br label %24

24:                                               ; preds = %23, %15
  %25 = tail call i32 @deflate(ptr noundef nonnull %0, i32 noundef 0) #3
  store i32 %25, ptr %14, align 8, !tbaa !19
  %.not27 = icmp eq i32 %25, 0
  br i1 %.not27, label %thread-pre-split, label %.loopexit, !llvm.loop !20

.loopexit:                                        ; preds = %thread-pre-split, %24, %8, %22
  %26 = getelementptr inbounds i8, ptr %0, i64 144
  %27 = load i64, ptr %26, align 8, !tbaa !22
  %28 = tail call i64 @crc32(i64 noundef %27, ptr noundef %1, i32 noundef %2) #3
  store i64 %28, ptr %26, align 8, !tbaa !22
  %29 = load i32, ptr %9, align 8, !tbaa !14
  %30 = sub i32 %2, %29
  br label %31

31:                                               ; preds = %3, %5, %.loopexit
  %.0 = phi i32 [ %30, %.loopexit ], [ -2, %5 ], [ -2, %3 ]
  ret i32 %.0
}

declare i32 @deflate(ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 }

!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, !9, i64 172}
!6 = !{!"gz_stream", !7, i64 0, !11, i64 112, !11, i64 116, !8, i64 120, !8, i64 128, !8, i64 136, !12, i64 144, !8, i64 152, !8, i64 160, !11, i64 168, !9, i64 172, !12, i64 176}
!7 = !{!"z_stream_s", !8, i64 0, !11, i64 8, !12, i64 16, !8, i64 24, !11, i64 32, !12, i64 40, !8, i64 48, !8, i64 56, !8, i64 64, !8, i64 72, !8, i64 80, !11, i64 88, !12, i64 96, !12, i64 104}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"long", !9, i64 0}
!13 = !{!6, !8, i64 0}
!14 = !{!6, !11, i64 8}
!15 = !{!6, !11, i64 32}
!16 = !{!6, !8, i64 136}
!17 = !{!6, !8, i64 24}
!18 = !{!6, !8, i64 120}
!19 = !{!6, !11, i64 112}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!6, !12, i64 144}


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