; 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 @FreeLastSavedImage(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 %53

13:                                               ; preds = %7
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.GifFileType, ptr %14, i32 0, i32 6
  %16 = load i32, ptr %15, align 8, !tbaa !14
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr %15, align 8, !tbaa !14
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.GifFileType, ptr %18, i32 0, i32 8
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.GifFileType, ptr %21, i32 0, i32 6
  %23 = load i32, ptr %22, align 8, !tbaa !14
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds %struct.SavedImage, ptr %20, i64 %24
  store ptr %25, ptr %3, align 8, !tbaa !5
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.SavedImage, ptr %26, i32 0, i32 0
  %28 = getelementptr inbounds %struct.GifImageDesc, ptr %27, i32 0, i32 5
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %39

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

39:                                               ; preds = %31, %13
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.SavedImage, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  %43 = icmp ne ptr %42, null
  br i1 %43, label %44, label %48

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

48:                                               ; preds = %44, %39
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.SavedImage, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.SavedImage, ptr %51, i32 0, i32 3
  call void @GifFreeExtensions(ptr noundef %50, ptr noundef %52)
  store i32 0, ptr %4, align 4
  br label %53

53:                                               ; preds = %48, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %54 = load i32, ptr %4, align 4
  switch i32 %54, label %56 [
    i32 0, label %55
    i32 1, label %55
  ]

55:                                               ; preds = %53, %53
  ret void

56:                                               ; preds = %53
  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}


; 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 @FreeLastSavedImage(ptr noundef %0) local_unnamed_addr #1 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %23, 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 %23, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 32
  %9 = load i32, ptr %8, align 8, !tbaa !13
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr %8, align 8, !tbaa !13
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds %struct.SavedImage, ptr %5, i64 %11
  %13 = getelementptr inbounds i8, ptr %12, i64 24
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %16, label %15

15:                                               ; preds = %7
  tail call void @GifFreeMapObject(ptr noundef nonnull %14)
  store ptr null, ptr %13, align 8, !tbaa !14
  br label %16

16:                                               ; preds = %15, %7
  %17 = getelementptr inbounds i8, ptr %12, i64 32
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %.not14 = icmp eq ptr %18, null
  br i1 %.not14, label %20, label %19

19:                                               ; preds = %16
  tail call void @free(ptr noundef nonnull %18) #2
  br label %20

20:                                               ; preds = %19, %16
  %21 = getelementptr inbounds i8, ptr %12, i64 40
  %22 = getelementptr inbounds i8, ptr %12, i64 48
  tail call void @GifFreeExtensions(ptr noundef nonnull %21, ptr noundef nonnull %22)
  br label %23

23:                                               ; preds = %1, %3, %20
  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}


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