; LLVM IR from dloebl/cgif :: src (cgif)

; 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.st_gif = type { [3 x ptr], %struct.st_gifconfig, ptr, ptr, i32, i32 }
%struct.st_gifconfig = type { ptr, ptr, i32, i32, i16, i16, i16, i16, ptr, ptr }

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

declare i32 @fclose(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @freeCGIF(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden i32 @flushFrame(ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @freeFrame(ptr noundef) #2

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

12:                                               ; preds = %1
  br label %55

13:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 1, ptr %5, align 4, !tbaa !14
  br label %14

14:                                               ; preds = %50, %13
  %15 = load i32, ptr %5, align 4, !tbaa !14
  %16 = icmp slt i32 %15, 3
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store i32 3, ptr %6, align 4
  br label %53

18:                                               ; preds = %14
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.st_gif, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %5, align 4, !tbaa !14
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [3 x ptr], ptr %20, i64 0, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %49

26:                                               ; preds = %18
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.st_gif, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %5, align 4, !tbaa !14
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [3 x ptr], ptr %29, i64 0, i64 %31
  %33 = load ptr, ptr %32, align 8, !tbaa !5
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.st_gif, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %5, align 4, !tbaa !14
  %37 = sub nsw i32 %36, 1
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [3 x ptr], ptr %35, i64 0, i64 %38
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = call i32 @flushFrame(ptr noundef %27, ptr noundef %33, ptr noundef %40)
  store i32 %41, ptr %3, align 4, !tbaa !14
  %42 = load i32, ptr %3, align 4, !tbaa !14
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %48

44:                                               ; preds = %26
  %45 = load i32, ptr %3, align 4, !tbaa !14
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.st_gif, ptr %46, i32 0, i32 4
  store i32 %45, ptr %47, align 8, !tbaa !9
  store i32 3, ptr %6, align 4
  br label %53

48:                                               ; preds = %26
  br label %49

49:                                               ; preds = %48, %18
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %5, align 4, !tbaa !14
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %5, align 4, !tbaa !14
  br label %14, !llvm.loop !15

53:                                               ; preds = %44, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %54

54:                                               ; preds = %53
  br label %55

55:                                               ; preds = %54, %12
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.st_gif, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !17
  %59 = call i32 @cgif_raw_close(ptr noundef %58)
  store i32 %59, ptr %3, align 4, !tbaa !14
  %60 = load i32, ptr %3, align 4, !tbaa !14
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %62, label %66

62:                                               ; preds = %55
  %63 = load i32, ptr %3, align 4, !tbaa !14
  %64 = load ptr, ptr %2, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.st_gif, ptr %64, i32 0, i32 4
  store i32 %63, ptr %65, align 8, !tbaa !9
  br label %66

66:                                               ; preds = %62, %55
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.st_gif, ptr %67, i32 0, i32 3
  %69 = load ptr, ptr %68, align 8, !tbaa !18
  %70 = icmp ne ptr %69, null
  br i1 %70, label %71, label %82

71:                                               ; preds = %66
  %72 = load ptr, ptr %2, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.st_gif, ptr %72, i32 0, i32 3
  %74 = load ptr, ptr %73, align 8, !tbaa !18
  %75 = call i32 @fclose(ptr noundef %74)
  store i32 %75, ptr %3, align 4, !tbaa !14
  %76 = load i32, ptr %3, align 4, !tbaa !14
  %77 = icmp ne i32 %76, 0
  br i1 %77, label %78, label %81

78:                                               ; preds = %71
  %79 = load ptr, ptr %2, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.st_gif, ptr %79, i32 0, i32 4
  store i32 3, ptr %80, align 8, !tbaa !9
  br label %81

81:                                               ; preds = %78, %71
  br label %82

82:                                               ; preds = %81, %66
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !14
  br label %83

83:                                               ; preds = %94, %82
  %84 = load i32, ptr %7, align 4, !tbaa !14
  %85 = icmp slt i32 %84, 3
  br i1 %85, label %87, label %86

86:                                               ; preds = %83
  store i32 6, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %97

87:                                               ; preds = %83
  %88 = load ptr, ptr %2, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.st_gif, ptr %88, i32 0, i32 0
  %90 = load i32, ptr %7, align 4, !tbaa !14
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds [3 x ptr], ptr %89, i64 0, i64 %91
  %93 = load ptr, ptr %92, align 8, !tbaa !5
  call void @freeFrame(ptr noundef %93)
  br label %94

94:                                               ; preds = %87
  %95 = load i32, ptr %7, align 4, !tbaa !14
  %96 = add nsw i32 %95, 1
  store i32 %96, ptr %7, align 4, !tbaa !14
  br label %83, !llvm.loop !19

97:                                               ; preds = %86
  %98 = load ptr, ptr %2, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.st_gif, ptr %98, i32 0, i32 4
  %100 = load i32, ptr %99, align 8, !tbaa !9
  store i32 %100, ptr %4, align 4, !tbaa !14
  %101 = load ptr, ptr %2, align 8, !tbaa !5
  call void @freeCGIF(ptr noundef %101)
  %102 = load i32, ptr %4, align 4, !tbaa !14
  %103 = icmp eq i32 %102, 6
  br i1 %103, label %104, label %105

104:                                              ; preds = %97
  store i32 -1, ptr %4, align 4, !tbaa !14
  br label %105

105:                                              ; preds = %104, %97
  %106 = load i32, ptr %4, align 4, !tbaa !14
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %106
}

declare i32 @cgif_raw_close(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 = { 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, !12, i64 88}
!10 = !{!"st_gif", !7, i64 0, !11, i64 24, !6, i64 72, !6, i64 80, !12, i64 88, !12, i64 92}
!11 = !{!"st_gifconfig", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 20, !13, i64 24, !13, i64 26, !13, i64 28, !13, i64 30, !6, i64 32, !6, i64 40}
!12 = !{!"int", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!10, !6, i64 72}
!18 = !{!10, !6, i64 80}
!19 = distinct !{!19, !16}


; 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 i32 @fclose(ptr nocapture noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare hidden i32 @flushFrame(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @cgif_close(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 88
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %.preheader.preheader, label %.loopexit

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

6:                                                ; preds = %.preheader.preheader
  %7 = load ptr, ptr %0, align 8, !tbaa !13
  %8 = tail call i32 @flushFrame(ptr noundef nonnull %0, ptr noundef nonnull %5, ptr noundef %7)
  %.not34 = icmp eq i32 %8, 0
  br i1 %.not34, label %.preheader.1, label %9

9:                                                ; preds = %12, %6
  %.lcssa = phi i32 [ %8, %6 ], [ %14, %12 ]
  store i32 %.lcssa, ptr %2, align 8, !tbaa !5
  br label %.loopexit

.preheader.1:                                     ; preds = %.preheader.preheader, %6
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %.not33.1 = icmp eq ptr %11, null
  br i1 %.not33.1, label %.loopexit, label %12

12:                                               ; preds = %.preheader.1
  %13 = load ptr, ptr %4, align 8, !tbaa !13
  %14 = tail call i32 @flushFrame(ptr noundef nonnull %0, ptr noundef nonnull %11, ptr noundef %13)
  %.not34.1 = icmp eq i32 %14, 0
  br i1 %.not34.1, label %.loopexit, label %9

.loopexit:                                        ; preds = %.preheader.1, %12, %9, %1
  %15 = getelementptr inbounds i8, ptr %0, i64 72
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  %17 = tail call i32 @cgif_raw_close(ptr noundef %16) #3
  %.not35 = icmp eq i32 %17, 0
  br i1 %.not35, label %19, label %18

18:                                               ; preds = %.loopexit
  store i32 %17, ptr %2, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %18, %.loopexit
  %20 = getelementptr inbounds i8, ptr %0, i64 80
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  %.not36 = icmp eq ptr %21, null
  br i1 %.not36, label %25, label %22

22:                                               ; preds = %19
  %23 = tail call i32 @fclose(ptr noundef nonnull %21)
  %.not37 = icmp eq i32 %23, 0
  br i1 %.not37, label %25, label %24

24:                                               ; preds = %22
  store i32 3, ptr %2, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %22, %24, %19
  %26 = load ptr, ptr %0, align 8, !tbaa !13
  tail call void @freeFrame(ptr noundef %26)
  %27 = getelementptr inbounds i8, ptr %0, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  tail call void @freeFrame(ptr noundef %28)
  %29 = getelementptr inbounds i8, ptr %0, i64 16
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  tail call void @freeFrame(ptr noundef %30)
  %31 = load i32, ptr %2, align 8, !tbaa !5
  tail call void @freeCGIF(ptr noundef nonnull %0)
  %32 = icmp eq i32 %31, 6
  %spec.store.select = select i1 %32, i32 -1, i32 %31
  ret i32 %spec.store.select
}

declare i32 @cgif_raw_close(ptr noundef) local_unnamed_addr #2

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 = { 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 = { "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, !11, i64 88}
!6 = !{!"st_gif", !7, i64 0, !9, i64 24, !10, i64 72, !10, i64 80, !11, i64 88, !11, i64 92}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"st_gifconfig", !10, i64 0, !10, i64 8, !11, i64 16, !11, i64 20, !12, i64 24, !12, i64 26, !12, i64 28, !12, i64 30, !10, i64 32, !10, i64 40}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !10, i64 0}
!14 = !{!6, !10, i64 72}
!15 = !{!6, !10, i64 80}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass