; LLVM IR from tangjz/acm-icpc :: codeforces/gym/102012E.ll (Z7seg_queiix)

; 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.Segment = type { i32, i32, i64, i64 }

@seg = external dso_local global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7seg_queiix(i32 noundef %0, i32 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = call noundef i32 @_Z7seg_idxii(i32 noundef %12, i32 noundef %13)
  store i32 %14, ptr %8, align 4, !tbaa !5
  %15 = load ptr, ptr @seg, align 8, !tbaa !11
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds %struct.Segment, ptr %15, i64 %17
  %19 = getelementptr inbounds %struct.Segment, ptr %18, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = icmp slt i64 %20, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %74

24:                                               ; preds = %3
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = icmp eq i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  store i32 1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %74

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = add nsw i32 %30, %31
  %33 = ashr i32 %32, 1
  store i32 %33, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !5
  %36 = call noundef i32 @_Z7seg_idxii(i32 noundef %34, i32 noundef %35)
  store i32 %36, ptr %11, align 4, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %6, align 4, !tbaa !5
  call void @_Z8seg_downii(i32 noundef %37, i32 noundef %38)
  %39 = load ptr, ptr @seg, align 8, !tbaa !11
  %40 = load i32, ptr %11, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.Segment, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.Segment, ptr %42, i32 0, i32 2
  %44 = load i64, ptr %43, align 8, !tbaa !13
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = icmp slt i64 %44, %45
  br i1 %46, label %47, label %53

47:                                               ; preds = %29
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = call noundef i32 @_Z7seg_queiix(i32 noundef %49, i32 noundef %50, i64 noundef %51)
  br label %72

53:                                               ; preds = %29
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = load i32, ptr %10, align 4, !tbaa !5
  %56 = load i64, ptr %7, align 8, !tbaa !9
  %57 = call noundef i32 @_Z7seg_queiix(i32 noundef %54, i32 noundef %55, i64 noundef %56)
  %58 = load ptr, ptr @seg, align 8, !tbaa !11
  %59 = load i32, ptr %8, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds %struct.Segment, ptr %58, i64 %60
  %62 = getelementptr inbounds %struct.Segment, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 8, !tbaa !15
  %64 = add nsw i32 %57, %63
  %65 = load ptr, ptr @seg, align 8, !tbaa !11
  %66 = load i32, ptr %11, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds %struct.Segment, ptr %65, i64 %67
  %69 = getelementptr inbounds %struct.Segment, ptr %68, i32 0, i32 0
  %70 = load i32, ptr %69, align 8, !tbaa !15
  %71 = sub nsw i32 %64, %70
  br label %72

72:                                               ; preds = %53, %47
  %73 = phi i32 [ %52, %47 ], [ %71, %53 ]
  store i32 %73, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %74

74:                                               ; preds = %72, %28, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %75 = load i32, ptr %4, align 4
  ret i32 %75
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z8seg_downii(i32 noundef, i32 noundef) #3

; 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 = { inlinehint mustprogress 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 = { inlinehint 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 #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"_ZTS7Segment", !6, i64 0, !6, i64 4, !10, i64 8, !10, i64 16}
!15 = !{!14, !6, i64 0}


; 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.Segment = type { i32, i32, i64, i64 }

@seg = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7seg_queiix(i32 noundef %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %1)
  %5 = load ptr, ptr @seg, align 8, !tbaa !5
  %6 = sext i32 %4 to i64
  %7 = getelementptr inbounds %struct.Segment, ptr %5, i64 %6, i32 2
  %8 = load i64, ptr %7, align 8, !tbaa !9
  %9 = icmp slt i64 %8, %2
  br i1 %9, label %common.ret54, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse
  %10 = phi i64 [ %24, %tailrecurse ], [ %6, %3 ]
  %.tr32 = phi i32 [ %21, %tailrecurse ], [ %0, %3 ]
  %11 = icmp eq i32 %.tr32, %1
  br i1 %11, label %common.ret54, label %12

12:                                               ; preds = %.lr.ph
  %13 = add nsw i32 %.tr32, %1
  %14 = ashr i32 %13, 1
  %15 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %.tr32, i32 noundef %14)
  tail call void @_Z8seg_downii(i32 noundef %.tr32, i32 noundef %1)
  %16 = load ptr, ptr @seg, align 8, !tbaa !5
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds %struct.Segment, ptr %16, i64 %17, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = icmp slt i64 %19, %2
  br i1 %20, label %tailrecurse, label %28

tailrecurse:                                      ; preds = %12
  %21 = add nsw i32 %14, 1
  %22 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %21, i32 noundef %1)
  %23 = load ptr, ptr @seg, align 8, !tbaa !5
  %24 = sext i32 %22 to i64
  %25 = getelementptr inbounds %struct.Segment, ptr %23, i64 %24, i32 2
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = icmp slt i64 %26, %2
  br i1 %27, label %common.ret54, label %.lr.ph

common.ret54:                                     ; preds = %3, %tailrecurse, %.lr.ph, %28
  %common.ret54.op = phi i32 [ %36, %28 ], [ 0, %3 ], [ 1, %.lr.ph ], [ 0, %tailrecurse ]
  ret i32 %common.ret54.op

28:                                               ; preds = %12
  %29 = tail call noundef i32 @_Z7seg_queiix(i32 noundef %.tr32, i32 noundef %14, i64 noundef %2)
  %30 = load ptr, ptr @seg, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Segment, ptr %30, i64 %10
  %32 = load i32, ptr %31, align 8, !tbaa !13
  %33 = add nsw i32 %32, %29
  %34 = getelementptr inbounds %struct.Segment, ptr %30, i64 %17
  %35 = load i32, ptr %34, align 8, !tbaa !13
  %36 = sub i32 %33, %35
  br label %common.ret54
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z8seg_downii(i32 noundef, i32 noundef) local_unnamed_addr #2

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 = { inlinehint mustprogress 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 = { inlinehint 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" }

!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, !12, i64 8}
!10 = !{!"_ZTS7Segment", !11, i64 0, !11, i64 4, !12, i64 8, !12, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!10, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass