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

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

@a = external dso_local global [100001 x i32], align 16
@seg = external dso_local global ptr, align 8

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

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

; 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 @_Z8seg_initii(i32 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = call noundef i32 @_Z7seg_idxii(i32 noundef %8, i32 noundef %9)
  store i32 %10, ptr %5, align 4, !tbaa !5
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp eq i32 %11, %12
  br i1 %13, label %14, label %23

14:                                               ; preds = %2
  %15 = load ptr, ptr @seg, align 8, !tbaa !9
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds %struct.Segment, ptr %15, i64 %17
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  call void @_ZN7Segment3setEii(ptr noundef nonnull align 8 dereferenceable(24) %18, i32 noundef 1, i32 noundef %22)
  store i32 1, ptr %6, align 4
  br label %45

23:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = add nsw i32 %24, %25
  %27 = ashr i32 %26, 1
  store i32 %27, ptr %7, align 4, !tbaa !5
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !5
  call void @_Z8seg_initii(i32 noundef %28, i32 noundef %29)
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  %32 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z8seg_initii(i32 noundef %31, i32 noundef %32)
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z6seg_upii(i32 noundef %33, i32 noundef %34)
  %35 = load ptr, ptr @seg, align 8, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.Segment, ptr %35, i64 %37
  %39 = getelementptr inbounds %struct.Segment, ptr %38, i32 0, i32 3
  store i64 0, ptr %39, align 8, !tbaa !11
  %40 = load ptr, ptr @seg, align 8, !tbaa !9
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds %struct.Segment, ptr %40, i64 %42
  %44 = getelementptr inbounds %struct.Segment, ptr %43, i32 0, i32 1
  store i32 0, ptr %44, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  store i32 0, ptr %6, align 4
  br label %45

45:                                               ; preds = %23, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  %46 = load i32, ptr %6, align 4
  switch i32 %46, label %48 [
    i32 0, label %47
    i32 1, label %47
  ]

47:                                               ; preds = %45, %45
  ret void

48:                                               ; preds = %45
  unreachable
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7Segment3setEii(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef, i32 noundef) #3 align 2

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

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


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

@a = external dso_local local_unnamed_addr global [100001 x i32], align 16
@seg = external dso_local local_unnamed_addr global ptr, align 8

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z8seg_initii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %1)
  %4 = icmp eq i32 %0, %1
  br i1 %4, label %common.ret, label %11

common.ret:                                       ; preds = %2
  %5 = load ptr, ptr @seg, align 8, !tbaa !5
  %6 = sext i32 %3 to i64
  %7 = getelementptr inbounds %struct.Segment, ptr %5, i64 %6
  %8 = sext i32 %0 to i64
  %9 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !9
  tail call void @_ZN7Segment3setEii(ptr noundef nonnull align 8 dereferenceable(24) %7, i32 noundef 1, i32 noundef %10)
  br label %common.ret17

common.ret17:                                     ; preds = %11, %common.ret
  ret void

11:                                               ; preds = %2
  %12 = add nsw i32 %1, %0
  %13 = ashr i32 %12, 1
  tail call void @_Z8seg_initii(i32 noundef %0, i32 noundef %13)
  %14 = add nsw i32 %13, 1
  tail call void @_Z8seg_initii(i32 noundef %14, i32 noundef %1)
  tail call void @_Z6seg_upii(i32 noundef %0, i32 noundef %1)
  %15 = load ptr, ptr @seg, align 8, !tbaa !5
  %16 = sext i32 %3 to i64
  %17 = getelementptr inbounds %struct.Segment, ptr %15, i64 %16, i32 3
  store i64 0, ptr %17, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.Segment, ptr %15, i64 %16, i32 1
  store i32 0, ptr %18, align 4, !tbaa !14
  br label %common.ret17
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7Segment3setEii(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef, i32 noundef) local_unnamed_addr #2 align 2

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

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

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


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