; LLVM IR from EyalAr/lwip :: src/lib/gif (gifalloc)

; 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.GifFileType = type { i32, i32, i32, i32, i8, ptr, i32, %struct.GifImageDesc, ptr, i32, ptr, i32, ptr, ptr }
%struct.GifImageDesc = type { i32, i32, i32, i32, i8, ptr }
%struct.SavedImage = type { %struct.GifImageDesc, ptr, i32, ptr }

; 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

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

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

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

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

7:                                                ; preds = %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.GifFileType, ptr %8, i32 0, i32 8
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %7, %1
  store i32 1, ptr %4, align 4
  br label %65

13:                                               ; preds = %7
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.GifFileType, ptr %14, i32 0, i32 8
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  store ptr %16, ptr %3, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %56, %13
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.GifFileType, ptr %19, i32 0, i32 8
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.GifFileType, ptr %22, i32 0, i32 6
  %24 = load i32, ptr %23, align 8, !tbaa !14
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.SavedImage, ptr %21, i64 %25
  %27 = icmp ult ptr %18, %26
  br i1 %27, label %28, label %59

28:                                               ; preds = %17
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.SavedImage, ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds %struct.GifImageDesc, ptr %30, i32 0, i32 5
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = icmp ne ptr %32, null
  br i1 %33, label %34, label %42

34:                                               ; preds = %28
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.SavedImage, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds %struct.GifImageDesc, ptr %36, i32 0, i32 5
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  call void @GifFreeMapObject(ptr noundef %38)
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.SavedImage, ptr %39, i32 0, i32 0
  %41 = getelementptr inbounds %struct.GifImageDesc, ptr %40, i32 0, i32 5
  store ptr null, ptr %41, align 8, !tbaa !15
  br label %42

42:                                               ; preds = %34, %28
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.SavedImage, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !17
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %51

47:                                               ; preds = %42
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.SavedImage, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 8, !tbaa !17
  call void @free(ptr noundef %50) #3
  br label %51

51:                                               ; preds = %47, %42
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.SavedImage, ptr %52, i32 0, i32 2
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.SavedImage, ptr %54, i32 0, i32 3
  call void @GifFreeExtensions(ptr noundef %53, ptr noundef %55)
  br label %56

56:                                               ; preds = %51
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.SavedImage, ptr %57, i32 1
  store ptr %58, ptr %3, align 8, !tbaa !5
  br label %17, !llvm.loop !18

59:                                               ; preds = %17
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.GifFileType, ptr %60, i32 0, i32 8
  %62 = load ptr, ptr %61, align 8, !tbaa !9
  call void @free(ptr noundef %62) #3
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.GifFileType, ptr %63, i32 0, i32 8
  store ptr null, ptr %64, align 8, !tbaa !9
  store i32 0, ptr %4, align 4
  br label %65

65:                                               ; preds = %59, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %66 = load i32, ptr %4, align 4
  switch i32 %66, label %68 [
    i32 0, label %67
    i32 1, label %67
  ]

67:                                               ; preds = %65, %65
  ret void

68:                                               ; preds = %65
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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, !6, i64 72}
!10 = !{!"GifFileType", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !7, i64 16, !6, i64 24, !11, i64 32, !12, i64 40, !6, i64 72, !11, i64 80, !6, i64 88, !11, i64 96, !6, i64 104, !6, i64 112}
!11 = !{!"int", !7, i64 0}
!12 = !{!"GifImageDesc", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !13, i64 16, !6, i64 24}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!10, !11, i64 32}
!15 = !{!16, !6, i64 24}
!16 = !{!"SavedImage", !12, i64 0, !6, i64 32, !11, i64 40, !6, i64 48}
!17 = !{!16, !6, i64 32}
!18 = distinct !{!18, !19}
!19 = !{!"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"

%struct.SavedImage = type { %struct.GifImageDesc, ptr, i32, ptr }
%struct.GifImageDesc = type { i32, i32, i32, i32, i8, ptr }

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @GifFreeMapObject(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @GifFreeSavedImages(ptr noundef %0) local_unnamed_addr #1 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %26, label %3

3:                                                ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 72
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %26, label %.preheader

.preheader:                                       ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  %8 = load i32, ptr %7, align 8, !tbaa !13
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %17
  %.021 = phi ptr [ %20, %17 ], [ %5, %.preheader ]
  %10 = getelementptr inbounds i8, ptr %.021, i64 24
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %.not = icmp eq ptr %11, null
  br i1 %.not, label %13, label %12

12:                                               ; preds = %.lr.ph
  tail call void @GifFreeMapObject(ptr noundef nonnull %11)
  store ptr null, ptr %10, align 8, !tbaa !14
  br label %13

13:                                               ; preds = %12, %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.021, i64 32
  %15 = load ptr, ptr %14, align 8, !tbaa !16
  %.not20 = icmp eq ptr %15, null
  br i1 %.not20, label %17, label %16

16:                                               ; preds = %13
  tail call void @free(ptr noundef nonnull %15) #2
  br label %17

17:                                               ; preds = %16, %13
  %18 = getelementptr inbounds i8, ptr %.021, i64 40
  %19 = getelementptr inbounds i8, ptr %.021, i64 48
  tail call void @GifFreeExtensions(ptr noundef nonnull %18, ptr noundef nonnull %19)
  %20 = getelementptr inbounds i8, ptr %.021, i64 56
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i32, ptr %7, align 8, !tbaa !13
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds %struct.SavedImage, ptr %21, i64 %23
  %25 = icmp ult ptr %20, %24
  br i1 %25, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %17, %.preheader
  %.lcssa = phi ptr [ %5, %.preheader ], [ %21, %17 ]
  tail call void @free(ptr noundef %.lcssa) #2
  store ptr null, ptr %4, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %1, %3, %._crit_edge
  ret void
}

attributes #0 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !10, i64 72}
!6 = !{!"GifFileType", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !8, i64 16, !10, i64 24, !7, i64 32, !11, i64 40, !10, i64 72, !7, i64 80, !10, i64 88, !7, i64 96, !10, i64 104, !10, i64 112}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"GifImageDesc", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !12, i64 16, !10, i64 24}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!6, !7, i64 32}
!14 = !{!15, !10, i64 24}
!15 = !{!"SavedImage", !11, i64 0, !10, i64 32, !7, i64 40, !10, i64 48}
!16 = !{!15, !10, i64 32}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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