; LLVM IR from dloebl/cgif :: examples (cgif_example_video)

; 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_gifconfig = type { ptr, ptr, i32, i32, i16, i16, i16, i16, ptr, ptr }
%struct.st_frameconfig = type { ptr, ptr, i32, i32, i16, i16, i8 }

@__const.main.aPalette = external hidden unnamed_addr constant [9 x i8], align 1
@.str = external hidden unnamed_addr constant [23 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca %struct.st_gifconfig, align 8
  %4 = alloca %struct.st_frameconfig, align 8
  %5 = alloca ptr, align 8
  %6 = alloca [9 x i8], align 1
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #6
  call void @llvm.lifetime.start.p0(i64 48, ptr %3) #6
  call void @llvm.lifetime.start.p0(i64 32, ptr %4) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.start.p0(i64 9, ptr %6) #6
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %6, ptr align 1 @__const.main.aPalette, i64 9, i1 false)
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #6
  store i16 3, ptr %7, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  store i32 12, ptr %8, align 4, !tbaa !9
  %12 = getelementptr inbounds [9 x i8], ptr %6, i64 0, i64 0
  %13 = load i16, ptr %7, align 2, !tbaa !5
  call void @initGIFConfig(ptr noundef %3, ptr noundef @.str, i16 noundef zeroext 100, i16 noundef zeroext 100, ptr noundef %12, i16 noundef zeroext %13)
  %14 = call ptr @cgif_newgif(ptr noundef %3)
  store ptr %14, ptr %2, align 8, !tbaa !11
  %15 = call noalias ptr @malloc(i64 noundef 10000) #7
  store ptr %15, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %47, %0
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  br label %50

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #6
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %40, %21
  %23 = load i32, ptr %11, align 4, !tbaa !9
  %24 = icmp slt i32 %23, 10000
  br i1 %24, label %26, label %25

25:                                               ; preds = %22
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #6
  br label %43

26:                                               ; preds = %22
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = load i32, ptr %11, align 4, !tbaa !9
  %29 = srem i32 %28, 100
  %30 = add nsw i32 %27, %29
  %31 = sdiv i32 %30, 4
  %32 = load i16, ptr %7, align 2, !tbaa !5
  %33 = zext i16 %32 to i32
  %34 = srem i32 %31, %33
  %35 = trunc i32 %34 to i8
  %36 = load ptr, ptr %5, align 8, !tbaa !11
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %36, i64 %38
  store i8 %35, ptr %39, align 1, !tbaa !13
  br label %40

40:                                               ; preds = %26
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %11, align 4, !tbaa !9
  br label %22, !llvm.loop !14

43:                                               ; preds = %25
  %44 = load ptr, ptr %5, align 8, !tbaa !11
  call void @initFrameConfig(ptr noundef %4, ptr noundef %44, i16 noundef zeroext 10)
  %45 = load ptr, ptr %2, align 8, !tbaa !11
  %46 = call i32 @cgif_addframe(ptr noundef %45, ptr noundef %4)
  br label %47

47:                                               ; preds = %43
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %9, align 4, !tbaa !9
  br label %16, !llvm.loop !16

50:                                               ; preds = %20
  %51 = load ptr, ptr %5, align 8, !tbaa !11
  call void @free(ptr noundef %51) #6
  %52 = load ptr, ptr %2, align 8, !tbaa !11
  %53 = call i32 @cgif_close(ptr noundef %52)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 9, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 32, ptr %4) #6
  call void @llvm.lifetime.end.p0(i64 48, ptr %3) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #6
  %54 = load i32, ptr %1, align 4
  ret i32 %54
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden void @initGIFConfig(ptr noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, ptr noundef, i16 noundef zeroext) #0

declare ptr @cgif_newgif(ptr noundef) #3

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #4

; 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
declare hidden void @initFrameConfig(ptr noundef, ptr noundef, i16 noundef zeroext) #0

declare i32 @cgif_addframe(ptr noundef, ptr noundef) #3

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

declare i32 @cgif_close(ptr noundef) #3

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 #4 = { nounwind allocsize(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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 = !{!"short", !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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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.st_gifconfig = type { ptr, ptr, i32, i32, i16, i16, i16, i16, ptr, ptr }
%struct.st_frameconfig = type { ptr, ptr, i32, i32, i16, i16, i8 }

@__const.main.aPalette = external hidden unnamed_addr constant [9 x i8], align 1
@.str = external hidden unnamed_addr constant [23 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca %struct.st_gifconfig, align 8
  %2 = alloca %struct.st_frameconfig, align 8
  %3 = alloca [9 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %1) #6
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %2) #6
  call void @llvm.lifetime.start.p0(i64 9, ptr nonnull %3) #6
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(9) %3, ptr noundef nonnull align 1 dereferenceable(9) @__const.main.aPalette, i64 9, i1 false)
  call void @initGIFConfig(ptr noundef nonnull %1, ptr noundef nonnull @.str, i16 noundef zeroext 100, i16 noundef zeroext 100, ptr noundef nonnull %3, i16 noundef zeroext 3)
  %4 = call ptr @cgif_newgif(ptr noundef nonnull %1) #6
  %5 = call noalias dereferenceable_or_null(10000) ptr @malloc(i64 noundef 10000) #7
  br label %vector.ph

vector.ph:                                        ; preds = %middle.block, %0
  %.01517 = phi i32 [ 0, %0 ], [ %17, %middle.block ]
  %broadcast.splatinsert = insertelement <16 x i32> poison, i32 %.01517, i64 0
  %broadcast.splat = shufflevector <16 x i32> %broadcast.splatinsert, <16 x i32> poison, <16 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <16 x i16> [ <i16 0, i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7, i16 8, i16 9, i16 10, i16 11, i16 12, i16 13, i16 14, i16 15>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %6 = urem <16 x i16> %vec.ind, <i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100, i16 100>
  %7 = zext nneg <16 x i16> %6 to <16 x i32>
  %8 = add nuw nsw <16 x i32> %broadcast.splat, %7
  %9 = lshr <16 x i32> %8, <i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2>
  %10 = urem <16 x i32> %9, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
  %11 = trunc nuw nsw <16 x i32> %10 to <16 x i8>
  %12 = getelementptr inbounds i8, ptr %5, i64 %index
  store <16 x i8> %11, ptr %12, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 16
  %vec.ind.next = add <16 x i16> %vec.ind, <i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16, i16 16>
  %13 = icmp eq i64 %index.next, 10000
  br i1 %13, label %middle.block, label %vector.body, !llvm.loop !8

14:                                               ; preds = %middle.block
  call void @free(ptr noundef nonnull %5) #6
  %15 = call i32 @cgif_close(ptr noundef %4) #6
  call void @llvm.lifetime.end.p0(i64 9, ptr nonnull %3) #6
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %2) #6
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %1) #6
  ret i32 0

middle.block:                                     ; preds = %vector.body
  call void @initFrameConfig(ptr noundef nonnull %2, ptr noundef nonnull %5, i16 noundef zeroext 10)
  %16 = call i32 @cgif_addframe(ptr noundef %4, ptr noundef nonnull %2) #6
  %17 = add nuw nsw i32 %.01517, 1
  %exitcond19.not = icmp eq i32 %17, 12
  br i1 %exitcond19.not, label %14, label %vector.ph, !llvm.loop !12
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden void @initGIFConfig(ptr noundef, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

declare ptr @cgif_newgif(ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #4

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

; Function Attrs: nounwind uwtable
declare hidden void @initFrameConfig(ptr noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

declare i32 @cgif_addframe(ptr noundef, ptr noundef) local_unnamed_addr #3

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

declare i32 @cgif_close(ptr noundef) local_unnamed_addr #3

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 #4 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass