; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/101 Hacks/101 Hack 2015/101 Hack July/Problem B (B)

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

@n = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@p = external dso_local global [100100 x i32], align 16
@num = external dso_local global i32, align 4
@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @q)
  store i32 1, ptr @p, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %46, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp sle i32 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %7
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @num)
  %14 = load i32, ptr @num, align 4, !tbaa !5
  %15 = icmp eq i32 %14, 3
  br i1 %15, label %16, label %32

16:                                               ; preds = %12
  %17 = load i32, ptr @x, align 4, !tbaa !5
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %16
  %20 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %20, ptr @y, align 4, !tbaa !5
  br label %23

21:                                               ; preds = %16
  %22 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %22, ptr @x, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %21, %19
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %30
  store i32 %28, ptr %31, align 4, !tbaa !5
  br label %45

32:                                               ; preds = %12
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = sub nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = load i32, ptr @num, align 4, !tbaa !5
  %39 = srem i32 %38, 3
  %40 = mul nsw i32 %37, %39
  %41 = srem i32 %40, 3
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %43
  store i32 %41, ptr %44, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %32, %23
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !9

49:                                               ; preds = %11
  br label %50

50:                                               ; preds = %95, %49
  %51 = load i32, ptr @q, align 4, !tbaa !5
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr @q, align 4, !tbaa !5
  %53 = icmp ne i32 %51, 0
  br i1 %53, label %54, label %96

54:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  %55 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3, ptr noundef %4)
  %56 = load i32, ptr %3, align 4, !tbaa !5
  %57 = load i32, ptr @x, align 4, !tbaa !5
  %58 = icmp sle i32 %56, %57
  br i1 %58, label %59, label %66

59:                                               ; preds = %54
  %60 = load i32, ptr @x, align 4, !tbaa !5
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = icmp sle i32 %60, %61
  br i1 %62, label %63, label %66

63:                                               ; preds = %59
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %5, align 4, !tbaa !5
  br label %66

66:                                               ; preds = %63, %59, %54
  %67 = load i32, ptr %3, align 4, !tbaa !5
  %68 = load i32, ptr @y, align 4, !tbaa !5
  %69 = icmp sle i32 %67, %68
  br i1 %69, label %70, label %77

70:                                               ; preds = %66
  %71 = load i32, ptr @y, align 4, !tbaa !5
  %72 = load i32, ptr %4, align 4, !tbaa !5
  %73 = icmp sle i32 %71, %72
  br i1 %73, label %74, label %77

74:                                               ; preds = %70
  %75 = load i32, ptr %5, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %5, align 4, !tbaa !5
  br label %77

77:                                               ; preds = %74, %70, %66
  %78 = load i32, ptr %5, align 4, !tbaa !5
  %79 = icmp eq i32 %78, 1
  br i1 %79, label %80, label %82

80:                                               ; preds = %77
  %81 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %95

82:                                               ; preds = %77
  %83 = load i32, ptr %4, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !5
  %87 = load i32, ptr %3, align 4, !tbaa !5
  %88 = sub nsw i32 %87, 1
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %89
  %91 = load i32, ptr %90, align 4, !tbaa !5
  %92 = mul nsw i32 %86, %91
  %93 = srem i32 %92, 3
  %94 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %93)
  br label %95

95:                                               ; preds = %82, %80
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %50, !llvm.loop !11

96:                                               ; preds = %50
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

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

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

declare i32 @printf(ptr noundef, ...) #1

attributes #0 = { mustprogress norecurse 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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

@n = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@p = external dso_local local_unnamed_addr global [100100 x i32], align 16
@num = external dso_local global i32, align 4
@x = external dso_local local_unnamed_addr global i32, align 4
@y = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @q)
  store i32 1, ptr @p, align 16, !tbaa !5
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %.not21 = icmp slt i32 %4, 1
  br i1 %.not21, label %.preheader, label %.lr.ph

.preheader:                                       ; preds = %24, %0
  %5 = load i32, ptr @q, align 4, !tbaa !5
  %6 = add nsw i32 %5, -1
  store i32 %6, ptr @q, align 4, !tbaa !5
  %.not1523 = icmp eq i32 %5, 0
  br i1 %.not1523, label %._crit_edge, label %.lr.ph24

.lr.ph:                                           ; preds = %0, %24
  %indvars.iv = phi i64 [ %indvars.iv.next, %24 ], [ 1, %0 ]
  %7 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @num)
  %8 = load i32, ptr @num, align 4, !tbaa !5
  %9 = icmp eq i32 %8, 3
  br i1 %9, label %10, label %17

10:                                               ; preds = %.lr.ph
  %11 = load i32, ptr @x, align 4, !tbaa !5
  %12 = icmp sgt i32 %11, 0
  %13 = trunc nuw nsw i64 %indvars.iv to i32
  %y.x = select i1 %12, ptr @y, ptr @x
  store i32 %13, ptr %y.x, align 4, !tbaa !5
  %14 = add nsw i64 %indvars.iv, -1
  %15 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  br label %24

17:                                               ; preds = %.lr.ph
  %18 = add nsw i64 %indvars.iv, -1
  %19 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = srem i32 %8, 3
  %22 = mul nsw i32 %20, %21
  %23 = srem i32 %22, 3
  br label %24

24:                                               ; preds = %10, %17
  %.sink = phi i32 [ %16, %10 ], [ %23, %17 ]
  %25 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %indvars.iv
  store i32 %.sink, ptr %25, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %.not.not = icmp slt i64 %indvars.iv, %27
  br i1 %.not.not, label %.lr.ph, label %.preheader, !llvm.loop !9

.lr.ph24:                                         ; preds = %.preheader, %47
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  %28 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1, ptr noundef nonnull %2)
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = load i32, ptr @x, align 4, !tbaa !5
  %.not16 = icmp sle i32 %29, %30
  %31 = load i32, ptr %2, align 4
  %.not17 = icmp sle i32 %30, %31
  %or.cond.not = select i1 %.not16, i1 %.not17, i1 false
  %32 = load i32, ptr @y, align 4, !tbaa !5
  %.not18 = icmp sle i32 %29, %32
  %.not19 = icmp sle i32 %32, %31
  %or.cond20.not = select i1 %.not18, i1 %.not19, i1 false
  %33 = xor i1 %or.cond.not, %or.cond20.not
  br i1 %33, label %34, label %36

34:                                               ; preds = %.lr.ph24
  %35 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %47

36:                                               ; preds = %.lr.ph24
  %37 = sext i32 %31 to i64
  %38 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = add nsw i32 %29, -1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100100 x i32], ptr @p, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = mul nsw i32 %43, %39
  %45 = srem i32 %44, 3
  %46 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %45)
  br label %47

47:                                               ; preds = %36, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  %48 = load i32, ptr @q, align 4, !tbaa !5
  %49 = add nsw i32 %48, -1
  store i32 %49, ptr @q, align 4, !tbaa !5
  %.not15 = icmp eq i32 %48, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph24, !llvm.loop !11

._crit_edge:                                      ; preds = %47, %.preheader
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nofree norecurse 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 = { nofree nounwind "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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass