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

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

declare i64 @adler32(i64 noundef, ptr noundef, i32 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

; Function Attrs: nounwind uwtable
define dso_local i32 @inflateSetDictionary(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 i32, align 4
  %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 4, ptr %8) #3
  %10 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %10, ptr %8, align 4, !tbaa !9
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %25, label %13

13:                                               ; preds = %3
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.z_stream_s, ptr %14, i32 0, i32 7
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = icmp eq ptr %16, null
  br i1 %17, label %25, label %18

18:                                               ; preds = %13
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.z_stream_s, ptr %19, i32 0, i32 7
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = getelementptr inbounds %struct.internal_state, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !14
  %24 = icmp ne i32 %23, 6
  br i1 %24, label %25, label %26

25:                                               ; preds = %18, %13, %3
  store i32 -2, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %72

26:                                               ; preds = %18
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = call i64 @adler32(i64 noundef 1, ptr noundef %27, i32 noundef %28)
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.z_stream_s, ptr %30, i32 0, i32 12
  %32 = load i64, ptr %31, align 8, !tbaa !16
  %33 = icmp ne i64 %29, %32
  br i1 %33, label %34, label %35

34:                                               ; preds = %26
  store i32 -3, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %72

35:                                               ; preds = %26
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.z_stream_s, ptr %36, i32 0, i32 12
  store i64 1, ptr %37, align 8, !tbaa !16
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.z_stream_s, ptr %39, i32 0, i32 7
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds %struct.internal_state, ptr %41, i32 0, i32 3
  %43 = load i32, ptr %42, align 4, !tbaa !17
  %44 = shl i32 1, %43
  %45 = icmp uge i32 %38, %44
  br i1 %45, label %46, label %60

46:                                               ; preds = %35
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.z_stream_s, ptr %47, i32 0, i32 7
  %49 = load ptr, ptr %48, align 8, !tbaa !11
  %50 = getelementptr inbounds %struct.internal_state, ptr %49, i32 0, i32 3
  %51 = load i32, ptr %50, align 4, !tbaa !17
  %52 = shl i32 1, %51
  %53 = sub nsw i32 %52, 1
  store i32 %53, ptr %8, align 4, !tbaa !9
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = load i32, ptr %8, align 4, !tbaa !9
  %56 = sub i32 %54, %55
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = zext i32 %56 to i64
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  store ptr %59, ptr %6, align 8, !tbaa !5
  br label %60

60:                                               ; preds = %46, %35
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.z_stream_s, ptr %61, i32 0, i32 7
  %63 = load ptr, ptr %62, align 8, !tbaa !11
  %64 = getelementptr inbounds %struct.internal_state, ptr %63, i32 0, i32 4
  %65 = load ptr, ptr %64, align 8, !tbaa !18
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = load i32, ptr %8, align 4, !tbaa !9
  call void @inflate_set_dictionary(ptr noundef %65, ptr noundef %66, i32 noundef %67)
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.z_stream_s, ptr %68, i32 0, i32 7
  %70 = load ptr, ptr %69, align 8, !tbaa !11
  %71 = getelementptr inbounds %struct.internal_state, ptr %70, i32 0, i32 0
  store i32 7, ptr %71, align 8, !tbaa !14
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %72

72:                                               ; preds = %60, %34, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %73 = load i32, ptr %4, align 4
  ret i32 %73
}

declare void @inflate_set_dictionary(ptr noundef, ptr noundef, i32 noundef) #0

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 = { nocallback nofree nosync 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 = { 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, !6, i64 56}
!12 = !{!"z_stream_s", !6, i64 0, !10, i64 8, !13, i64 16, !6, i64 24, !10, i64 32, !13, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !13, i64 96, !13, i64 104}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !10, i64 0}
!15 = !{!"internal_state", !10, i64 0, !7, i64 8, !10, i64 24, !10, i64 28, !6, i64 32}
!16 = !{!12, !13, i64 96}
!17 = !{!15, !10, i64 28}
!18 = !{!15, !6, i64 32}


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

declare i64 @adler32(i64 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 56
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %25, label %9

9:                                                ; preds = %5
  %10 = load i32, ptr %7, align 8, !tbaa !12
  %.not = icmp eq i32 %10, 6
  br i1 %.not, label %11, label %25

11:                                               ; preds = %9
  %12 = tail call i64 @adler32(i64 noundef 1, ptr noundef %1, i32 noundef %2) #2
  %13 = getelementptr inbounds i8, ptr %0, i64 96
  %14 = load i64, ptr %13, align 8, !tbaa !14
  %.not24 = icmp eq i64 %12, %14
  br i1 %.not24, label %15, label %25

15:                                               ; preds = %11
  store i64 1, ptr %13, align 8, !tbaa !14
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 28
  %18 = load i32, ptr %17, align 4, !tbaa !15
  %19 = shl nuw i32 1, %18
  %.not25 = icmp ugt i32 %19, %2
  %20 = add nsw i32 %19, -1
  %21 = sub i32 %2, %20
  %narrow = select i1 %.not25, i32 0, i32 %21
  %.019.idx = zext i32 %narrow to i64
  %.019 = getelementptr inbounds i8, ptr %1, i64 %.019.idx
  %.0 = select i1 %.not25, i32 %2, i32 %20
  %22 = getelementptr inbounds i8, ptr %16, i64 32
  %23 = load ptr, ptr %22, align 8, !tbaa !16
  tail call void @inflate_set_dictionary(ptr noundef %23, ptr noundef %.019, i32 noundef %.0) #2
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 7, ptr %24, align 8, !tbaa !12
  br label %25

25:                                               ; preds = %11, %3, %5, %9, %15
  %.018 = phi i32 [ 0, %15 ], [ -2, %9 ], [ -2, %5 ], [ -2, %3 ], [ -3, %11 ]
  ret i32 %.018
}

declare void @inflate_set_dictionary(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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 = { 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, !7, i64 56}
!6 = !{!"z_stream_s", !7, i64 0, !10, i64 8, !11, i64 16, !7, i64 24, !10, i64 32, !11, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !11, i64 96, !11, i64 104}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"internal_state", !10, i64 0, !8, i64 8, !10, i64 24, !10, i64 28, !7, i64 32}
!14 = !{!6, !11, i64 96}
!15 = !{!13, !10, i64 28}
!16 = !{!13, !7, i64 32}


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