; LLVM IR from abeaumont/competitive-programming :: kattis/favourable.ll (Z1cPK7sectioni)

; 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.section = type { i8, i8, [3 x i32] }

@cache = external dso_local global [401 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z1cPK7sectioni(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds %struct.section, ptr %8, i64 %10
  %12 = getelementptr inbounds %struct.section, ptr %11, i32 0, i32 0
  %13 = load i8, ptr %12, align 4, !tbaa !11, !range !14, !noundef !15
  %14 = trunc i8 %13 to i1
  br i1 %14, label %15, label %26

15:                                               ; preds = %2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds %struct.section, ptr %16, i64 %18
  %20 = getelementptr inbounds %struct.section, ptr %19, i32 0, i32 1
  %21 = load i8, ptr %20, align 1, !tbaa !16, !range !14, !noundef !15
  %22 = trunc i8 %21 to i1
  %23 = zext i1 %22 to i64
  %24 = select i1 %22, i32 1, i32 0
  %25 = sext i32 %24 to i64
  store i64 %25, ptr %3, align 8
  br label %65

26:                                               ; preds = %2
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [401 x i64], ptr @cache, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !17
  %31 = icmp ne i64 %30, -1
  br i1 %31, label %32, label %37

32:                                               ; preds = %26
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [401 x i64], ptr @cache, i64 0, i64 %34
  %36 = load i64, ptr %35, align 8, !tbaa !17
  store i64 %36, ptr %3, align 8
  br label %65

37:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %56, %37
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = icmp slt i32 %39, 3
  br i1 %40, label %42, label %41

41:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %59

42:                                               ; preds = %38
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds %struct.section, ptr %44, i64 %46
  %48 = getelementptr inbounds %struct.section, ptr %47, i32 0, i32 2
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [3 x i32], ptr %48, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !9
  %53 = call noundef i64 @_Z1cPK7sectioni(ptr noundef %43, i32 noundef %52)
  %54 = load i64, ptr %6, align 8, !tbaa !17
  %55 = add nsw i64 %54, %53
  store i64 %55, ptr %6, align 8, !tbaa !17
  br label %56

56:                                               ; preds = %42
  %57 = load i32, ptr %7, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %7, align 4, !tbaa !9
  br label %38, !llvm.loop !19

59:                                               ; preds = %41
  %60 = load i64, ptr %6, align 8, !tbaa !17
  %61 = load i32, ptr %5, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [401 x i64], ptr @cache, i64 0, i64 %62
  store i64 %60, ptr %63, align 8, !tbaa !17
  %64 = load i64, ptr %6, align 8, !tbaa !17
  store i64 %64, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %65

65:                                               ; preds = %59, %32, %15
  %66 = load i64, ptr %3, align 8
  ret i64 %66
}

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS7section", !13, i64 0, !13, i64 1, !7, i64 4}
!13 = !{!"bool", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = !{!12, !13, i64 1}
!17 = !{!18, !18, i64 0}
!18 = !{!"long long", !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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.section = type { i8, i8, [3 x i32] }

@cache = external dso_local local_unnamed_addr global [401 x i64], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z1cPK7sectioni(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds %struct.section, ptr %0, i64 %3
  %5 = load i8, ptr %4, align 4, !tbaa !5, !range !10, !noundef !11
  %6 = trunc nuw i8 %5 to i1
  br i1 %6, label %7, label %11

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %4, i64 1
  %9 = load i8, ptr %8, align 1, !tbaa !12, !range !10, !noundef !11
  %10 = zext nneg i8 %9 to i64
  br label %common.ret

11:                                               ; preds = %2
  %12 = getelementptr inbounds [401 x i64], ptr @cache, i64 0, i64 %3
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %.not = icmp eq i64 %13, -1
  br i1 %.not, label %.preheader, label %common.ret

common.ret:                                       ; preds = %7, %11, %.preheader
  %common.ret.op = phi i64 [ %24, %.preheader ], [ %10, %7 ], [ %13, %11 ]
  ret i64 %common.ret.op

.preheader:                                       ; preds = %11
  %14 = getelementptr inbounds i8, ptr %4, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !15
  %16 = tail call noundef i64 @_Z1cPK7sectioni(ptr noundef nonnull %0, i32 noundef %15)
  %17 = getelementptr inbounds i8, ptr %4, i64 8
  %18 = load i32, ptr %17, align 4, !tbaa !15
  %19 = tail call noundef i64 @_Z1cPK7sectioni(ptr noundef nonnull %0, i32 noundef %18)
  %20 = add nsw i64 %19, %16
  %21 = getelementptr inbounds i8, ptr %4, i64 12
  %22 = load i32, ptr %21, align 4, !tbaa !15
  %23 = tail call noundef i64 @_Z1cPK7sectioni(ptr noundef nonnull %0, i32 noundef %22)
  %24 = add nsw i64 %23, %20
  store i64 %24, ptr %12, align 8, !tbaa !13
  br label %common.ret
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: read, 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, !7, i64 0}
!6 = !{!"_ZTS7section", !7, i64 0, !7, i64 1, !8, i64 4}
!7 = !{!"bool", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{i8 0, i8 2}
!11 = !{}
!12 = !{!6, !7, i64 1}
!13 = !{!14, !14, i64 0}
!14 = !{!"long long", !8, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !8, i64 0}


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