; LLVM IR from AngelJumbo/lavat :: lavat (next_color)

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

@colors = external hidden global [8 x i16], align 16

; 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 uwtable
define dso_local signext i16 @next_color(i16 noundef signext %0) #1 {
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i16 %0, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %6

6:                                                ; preds = %37, %1
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = icmp slt i32 %7, 8
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  store i32 2, ptr %5, align 4
  br label %40

10:                                               ; preds = %6
  %11 = load i16, ptr %3, align 2, !tbaa !5
  %12 = sext i16 %11 to i32
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [8 x i16], ptr @colors, i64 0, i64 %14
  %16 = load i16, ptr %15, align 2, !tbaa !5
  %17 = sext i16 %16 to i32
  %18 = icmp eq i32 %12, %17
  br i1 %18, label %29, label %19

19:                                               ; preds = %10
  %20 = load i16, ptr %3, align 2, !tbaa !5
  %21 = sext i16 %20 to i32
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [8 x i16], ptr @colors, i64 0, i64 %23
  %25 = load i16, ptr %24, align 2, !tbaa !5
  %26 = sext i16 %25 to i32
  %27 = or i32 %26, 256
  %28 = icmp eq i32 %21, %27
  br i1 %28, label %29, label %36

29:                                               ; preds = %19, %10
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  %32 = srem i32 %31, 8
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [8 x i16], ptr @colors, i64 0, i64 %33
  %35 = load i16, ptr %34, align 2, !tbaa !5
  store i16 %35, ptr %2, align 2
  store i32 1, ptr %5, align 4
  br label %40

36:                                               ; preds = %19
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %4, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %4, align 4, !tbaa !9
  br label %6, !llvm.loop !11

40:                                               ; preds = %29, %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %41 = load i32, ptr %5, align 4
  switch i32 %41, label %46 [
    i32 2, label %42
    i32 1, label %44
  ]

42:                                               ; preds = %40
  %43 = load i16, ptr @colors, align 16, !tbaa !5
  store i16 %43, ptr %2, align 2
  br label %44

44:                                               ; preds = %42, %40
  %45 = load i16, ptr %2, align 2
  ret i16 %45

46:                                               ; preds = %40
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 = distinct !{!11, !12}
!12 = !{!"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"

@colors = external hidden local_unnamed_addr global [8 x i16], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local signext i16 @next_color(i16 noundef signext %0) local_unnamed_addr #0 {
  %2 = load i16, ptr @colors, align 16
  %3 = icmp eq i16 %2, %0
  %4 = or i16 %2, 256
  %5 = icmp eq i16 %4, %0
  %or.cond = or i1 %3, %5
  br i1 %or.cond, label %6, label %9

6:                                                ; preds = %39, %34, %29, %24, %19, %14, %9, %1
  %.0916.lcssa.wide = phi i64 [ 1, %1 ], [ 2, %9 ], [ 3, %14 ], [ 4, %19 ], [ 5, %24 ], [ 6, %29 ], [ 7, %34 ], [ 0, %39 ]
  %7 = getelementptr inbounds [8 x i16], ptr @colors, i64 0, i64 %.0916.lcssa.wide
  %8 = load i16, ptr %7, align 2, !tbaa !5
  br label %.loopexit

9:                                                ; preds = %1
  %10 = load i16, ptr getelementptr inbounds (i8, ptr @colors, i64 2), align 2, !tbaa !5
  %11 = icmp eq i16 %10, %0
  %12 = or i16 %10, 256
  %13 = icmp eq i16 %12, %0
  %or.cond.1 = or i1 %11, %13
  br i1 %or.cond.1, label %6, label %14

14:                                               ; preds = %9
  %15 = load i16, ptr getelementptr inbounds (i8, ptr @colors, i64 4), align 4, !tbaa !5
  %16 = icmp eq i16 %15, %0
  %17 = or i16 %15, 256
  %18 = icmp eq i16 %17, %0
  %or.cond.2 = or i1 %16, %18
  br i1 %or.cond.2, label %6, label %19

19:                                               ; preds = %14
  %20 = load i16, ptr getelementptr inbounds (i8, ptr @colors, i64 6), align 2, !tbaa !5
  %21 = icmp eq i16 %20, %0
  %22 = or i16 %20, 256
  %23 = icmp eq i16 %22, %0
  %or.cond.3 = or i1 %21, %23
  br i1 %or.cond.3, label %6, label %24

24:                                               ; preds = %19
  %25 = load i16, ptr getelementptr inbounds (i8, ptr @colors, i64 8), align 8, !tbaa !5
  %26 = icmp eq i16 %25, %0
  %27 = or i16 %25, 256
  %28 = icmp eq i16 %27, %0
  %or.cond.4 = or i1 %26, %28
  br i1 %or.cond.4, label %6, label %29

29:                                               ; preds = %24
  %30 = load i16, ptr getelementptr inbounds (i8, ptr @colors, i64 10), align 2, !tbaa !5
  %31 = icmp eq i16 %30, %0
  %32 = or i16 %30, 256
  %33 = icmp eq i16 %32, %0
  %or.cond.5 = or i1 %31, %33
  br i1 %or.cond.5, label %6, label %34

34:                                               ; preds = %29
  %35 = load i16, ptr getelementptr inbounds (i8, ptr @colors, i64 12), align 4, !tbaa !5
  %36 = icmp eq i16 %35, %0
  %37 = or i16 %35, 256
  %38 = icmp eq i16 %37, %0
  %or.cond.6 = or i1 %36, %38
  br i1 %or.cond.6, label %6, label %39

39:                                               ; preds = %34
  %40 = load i16, ptr getelementptr inbounds (i8, ptr @colors, i64 14), align 2, !tbaa !5
  %41 = icmp eq i16 %40, %0
  %42 = or i16 %40, 256
  %43 = icmp eq i16 %42, %0
  %or.cond.7 = or i1 %41, %43
  br i1 %or.cond.7, label %6, label %.loopexit

.loopexit:                                        ; preds = %39, %6
  %spec.select = phi i16 [ %8, %6 ], [ %2, %39 ]
  ret i16 %spec.select
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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" }

!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"}


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