; LLVM IR from tangjz/acm-icpc :: codeforces/complete/536E.ll (Z7seg_updiii)

; 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, i32 }

@f = external dso_local global [100001 x i32], align 16
@seg = external dso_local global [200003 x %struct.Segment], 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: mustprogress uwtable
define dso_local void @_Z7seg_updiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.Segment, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = call noundef i32 @_Z7seg_idxii(i32 noundef %13, i32 noundef %14)
  store i32 %15, ptr %7, align 4, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %27

19:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 12, ptr %8) #4
  %20 = getelementptr inbounds %struct.Segment, ptr %8, i32 0, i32 0
  %21 = load i32, ptr getelementptr inbounds ([100001 x i32], ptr @f, i64 0, i64 1), align 4, !tbaa !5
  store i32 %21, ptr %20, align 4, !tbaa !9
  %22 = getelementptr inbounds %struct.Segment, ptr %8, i32 0, i32 1
  store i32 1, ptr %22, align 4, !tbaa !11
  %23 = getelementptr inbounds %struct.Segment, ptr %8, i32 0, i32 2
  store i32 1, ptr %23, align 4, !tbaa !12
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %25
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %26, ptr align 4 %8, i64 12, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 12, ptr %8) #4
  store i32 1, ptr %9, align 4
  br label %157

27:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = add nsw i32 %28, %29
  %31 = ashr i32 %30, 1
  store i32 %31, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = load i32, ptr %10, align 4, !tbaa !5
  %34 = call noundef i32 @_Z7seg_idxii(i32 noundef %32, i32 noundef %33)
  store i32 %34, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %35 = load i32, ptr %10, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = call noundef i32 @_Z7seg_idxii(i32 noundef %36, i32 noundef %37)
  store i32 %38, ptr %12, align 4, !tbaa !5
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr %10, align 4, !tbaa !5
  %41 = icmp sle i32 %39, %40
  br i1 %41, label %42, label %46

42:                                               ; preds = %27
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = load i32, ptr %10, align 4, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !5
  call void @_Z7seg_updiii(i32 noundef %43, i32 noundef %44, i32 noundef %45)
  br label %51

46:                                               ; preds = %27
  %47 = load i32, ptr %10, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = load i32, ptr %6, align 4, !tbaa !5
  call void @_Z7seg_updiii(i32 noundef %48, i32 noundef %49, i32 noundef %50)
  br label %51

51:                                               ; preds = %46, %42
  %52 = load i32, ptr %11, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.Segment, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = load i32, ptr %11, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %58
  %60 = getelementptr inbounds %struct.Segment, ptr %59, i32 0, i32 2
  %61 = load i32, ptr %60, align 4, !tbaa !12
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [100001 x i32], ptr @f, i64 0, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !5
  %65 = sub nsw i32 %56, %64
  %66 = load i32, ptr %12, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %67
  %69 = getelementptr inbounds %struct.Segment, ptr %68, i32 0, i32 0
  %70 = load i32, ptr %69, align 4, !tbaa !9
  %71 = add nsw i32 %65, %70
  %72 = load i32, ptr %12, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %73
  %75 = getelementptr inbounds %struct.Segment, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %75, align 4, !tbaa !11
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds [100001 x i32], ptr @f, i64 0, i64 %77
  %79 = load i32, ptr %78, align 4, !tbaa !5
  %80 = sub nsw i32 %71, %79
  %81 = load i32, ptr %11, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %82
  %84 = getelementptr inbounds %struct.Segment, ptr %83, i32 0, i32 2
  %85 = load i32, ptr %84, align 4, !tbaa !12
  %86 = load i32, ptr %12, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %87
  %89 = getelementptr inbounds %struct.Segment, ptr %88, i32 0, i32 1
  %90 = load i32, ptr %89, align 4, !tbaa !11
  %91 = add nsw i32 %85, %90
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [100001 x i32], ptr @f, i64 0, i64 %92
  %94 = load i32, ptr %93, align 4, !tbaa !5
  %95 = add nsw i32 %80, %94
  %96 = load i32, ptr %7, align 4, !tbaa !5
  %97 = sext i32 %96 to i64
  %98 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %97
  %99 = getelementptr inbounds %struct.Segment, ptr %98, i32 0, i32 0
  store i32 %95, ptr %99, align 4, !tbaa !9
  %100 = load i32, ptr %11, align 4, !tbaa !5
  %101 = sext i32 %100 to i64
  %102 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %101
  %103 = getelementptr inbounds %struct.Segment, ptr %102, i32 0, i32 1
  %104 = load i32, ptr %103, align 4, !tbaa !11
  %105 = load i32, ptr %11, align 4, !tbaa !5
  %106 = sext i32 %105 to i64
  %107 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %106
  %108 = getelementptr inbounds %struct.Segment, ptr %107, i32 0, i32 1
  %109 = load i32, ptr %108, align 4, !tbaa !11
  %110 = load i32, ptr %10, align 4, !tbaa !5
  %111 = load i32, ptr %4, align 4, !tbaa !5
  %112 = sub nsw i32 %110, %111
  %113 = add nsw i32 %112, 1
  %114 = icmp eq i32 %109, %113
  br i1 %114, label %115, label %121

115:                                              ; preds = %51
  %116 = load i32, ptr %12, align 4, !tbaa !5
  %117 = sext i32 %116 to i64
  %118 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %117
  %119 = getelementptr inbounds %struct.Segment, ptr %118, i32 0, i32 1
  %120 = load i32, ptr %119, align 4, !tbaa !11
  br label %122

121:                                              ; preds = %51
  br label %122

122:                                              ; preds = %121, %115
  %123 = phi i32 [ %120, %115 ], [ 0, %121 ]
  %124 = add nsw i32 %104, %123
  %125 = load i32, ptr %7, align 4, !tbaa !5
  %126 = sext i32 %125 to i64
  %127 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %126
  %128 = getelementptr inbounds %struct.Segment, ptr %127, i32 0, i32 1
  store i32 %124, ptr %128, align 4, !tbaa !11
  %129 = load i32, ptr %12, align 4, !tbaa !5
  %130 = sext i32 %129 to i64
  %131 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %130
  %132 = getelementptr inbounds %struct.Segment, ptr %131, i32 0, i32 2
  %133 = load i32, ptr %132, align 4, !tbaa !12
  %134 = load i32, ptr %12, align 4, !tbaa !5
  %135 = sext i32 %134 to i64
  %136 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %135
  %137 = getelementptr inbounds %struct.Segment, ptr %136, i32 0, i32 2
  %138 = load i32, ptr %137, align 4, !tbaa !12
  %139 = load i32, ptr %5, align 4, !tbaa !5
  %140 = load i32, ptr %10, align 4, !tbaa !5
  %141 = sub nsw i32 %139, %140
  %142 = icmp eq i32 %138, %141
  br i1 %142, label %143, label %149

143:                                              ; preds = %122
  %144 = load i32, ptr %11, align 4, !tbaa !5
  %145 = sext i32 %144 to i64
  %146 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %145
  %147 = getelementptr inbounds %struct.Segment, ptr %146, i32 0, i32 2
  %148 = load i32, ptr %147, align 4, !tbaa !12
  br label %150

149:                                              ; preds = %122
  br label %150

150:                                              ; preds = %149, %143
  %151 = phi i32 [ %148, %143 ], [ 0, %149 ]
  %152 = add nsw i32 %133, %151
  %153 = load i32, ptr %7, align 4, !tbaa !5
  %154 = sext i32 %153 to i64
  %155 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %154
  %156 = getelementptr inbounds %struct.Segment, ptr %155, i32 0, i32 2
  store i32 %152, ptr %156, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  store i32 0, ptr %9, align 4
  br label %157

157:                                              ; preds = %150, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  %158 = load i32, ptr %9, align 4
  switch i32 %158, label %160 [
    i32 0, label %159
    i32 1, label %159
  ]

159:                                              ; preds = %157, %157
  ret void

160:                                              ; preds = %157
  unreachable
}

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

; 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) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!10 = !{!"_ZTS7Segment", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 8}
!13 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 4, !5}


; 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, i32 }

@f = external dso_local local_unnamed_addr global [100001 x i32], align 16
@seg = external dso_local local_unnamed_addr global [200003 x %struct.Segment], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z7seg_updiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %1)
  %5 = icmp eq i32 %0, %1
  br i1 %5, label %6, label %10

6:                                                ; preds = %3
  %7 = load i32, ptr getelementptr inbounds (i8, ptr @f, i64 4), align 4, !tbaa !5
  %8 = sext i32 %4 to i64
  %9 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %8
  store i32 %7, ptr %9, align 4, !tbaa !5
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %9, i64 4
  store i32 1, ptr %.sroa.4.0..sroa_idx, align 4, !tbaa !5
  br label %57

10:                                               ; preds = %3
  %11 = add nsw i32 %1, %0
  %12 = ashr i32 %11, 1
  %13 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %12)
  %14 = add nsw i32 %12, 1
  %15 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %14, i32 noundef %1)
  %.not = icmp slt i32 %12, %2
  br i1 %.not, label %17, label %16

16:                                               ; preds = %10
  tail call void @_Z7seg_updiii(i32 noundef %0, i32 noundef %12, i32 noundef %2)
  br label %18

17:                                               ; preds = %10
  tail call void @_Z7seg_updiii(i32 noundef %14, i32 noundef %1, i32 noundef %2)
  br label %18

18:                                               ; preds = %17, %16
  %19 = sext i32 %13 to i64
  %20 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = getelementptr inbounds i8, ptr %20, i64 8
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100001 x i32], ptr @f, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = sext i32 %15 to i64
  %28 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = getelementptr inbounds i8, ptr %28, i64 4
  %31 = load i32, ptr %30, align 4, !tbaa !12
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100001 x i32], ptr @f, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %31, %23
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [100001 x i32], ptr @f, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = add i32 %21, %29
  %40 = add i32 %26, %34
  %41 = sub i32 %39, %40
  %42 = add nsw i32 %41, %38
  %43 = sext i32 %4 to i64
  %44 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %43
  store i32 %42, ptr %44, align 4, !tbaa !9
  %45 = getelementptr inbounds i8, ptr %20, i64 4
  %46 = load i32, ptr %45, align 4, !tbaa !12
  %reass.sub = sub i32 %12, %0
  %47 = add i32 %reass.sub, 1
  %48 = icmp eq i32 %46, %47
  %spec.select = select i1 %48, i32 %31, i32 0
  %49 = add nsw i32 %spec.select, %46
  %50 = getelementptr inbounds i8, ptr %44, i64 4
  store i32 %49, ptr %50, align 4, !tbaa !12
  %51 = getelementptr inbounds i8, ptr %28, i64 8
  %52 = load i32, ptr %51, align 4, !tbaa !11
  %53 = sub nsw i32 %1, %12
  %54 = icmp eq i32 %52, %53
  %55 = select i1 %54, i32 %23, i32 0
  %56 = add nsw i32 %55, %52
  br label %57

57:                                               ; preds = %18, %6
  %58 = phi i64 [ %43, %18 ], [ %8, %6 ]
  %.sink = phi i32 [ %56, %18 ], [ 1, %6 ]
  %59 = getelementptr inbounds [200003 x %struct.Segment], ptr @seg, i64 0, i64 %58, i32 2
  store i32 %.sink, ptr %59, align 4, !tbaa !5
  ret void
}

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

attributes #0 = { 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 #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" }

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass