; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/SHORT/COOK65/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"

@q = external dso_local global i32, align 4
@s = external dso_local global i64, align 8
@a = external dso_local global i64, align 8
@b = external dso_local global i64, align 8
@sum = external dso_local global i64, align 8
@h = external dso_local global [134217728 x i16], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 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
  store i32 0, ptr %1, align 4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @q)
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @s, ptr noundef @a, ptr noundef @b)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  br label %7

7:                                                ; preds = %72, %0
  %8 = load i32, ptr @q, align 4, !tbaa !5
  %9 = add nsw i32 %8, -1
  store i32 %9, ptr @q, align 4, !tbaa !5
  %10 = icmp ne i32 %8, 0
  br i1 %10, label %11, label %79

11:                                               ; preds = %7
  %12 = load i64, ptr @s, align 8, !tbaa !9
  %13 = ashr i64 %12, 1
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr %2, align 4, !tbaa !5
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = ashr i32 %15, 27
  store i32 %16, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = and i32 %17, 134217727
  store i32 %18, ptr %4, align 4, !tbaa !5
  %19 = load i64, ptr @s, align 8, !tbaa !9
  %20 = and i64 %19, 1
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %47

22:                                               ; preds = %11
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [134217728 x i16], ptr @h, i64 0, i64 %24
  %26 = load i16, ptr %25, align 2, !tbaa !11
  %27 = sext i16 %26 to i32
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = ashr i32 %27, %28
  %30 = and i32 %29, 1
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %46

32:                                               ; preds = %22
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = shl i32 1, %33
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [134217728 x i16], ptr @h, i64 0, i64 %36
  %38 = load i16, ptr %37, align 2, !tbaa !11
  %39 = sext i16 %38 to i32
  %40 = xor i32 %39, %34
  %41 = trunc i32 %40 to i16
  store i16 %41, ptr %37, align 2, !tbaa !11
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = load i64, ptr @sum, align 8, !tbaa !9
  %45 = add nsw i64 %44, %43
  store i64 %45, ptr @sum, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %32, %22
  br label %72

47:                                               ; preds = %11
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [134217728 x i16], ptr @h, i64 0, i64 %49
  %51 = load i16, ptr %50, align 2, !tbaa !11
  %52 = sext i16 %51 to i32
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = ashr i32 %52, %53
  %55 = and i32 %54, 1
  %56 = icmp sgt i32 %55, 0
  br i1 %56, label %57, label %71

57:                                               ; preds = %47
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = shl i32 1, %58
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [134217728 x i16], ptr @h, i64 0, i64 %61
  %63 = load i16, ptr %62, align 2, !tbaa !11
  %64 = sext i16 %63 to i32
  %65 = xor i32 %64, %59
  %66 = trunc i32 %65 to i16
  store i16 %66, ptr %62, align 2, !tbaa !11
  %67 = load i32, ptr %2, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = load i64, ptr @sum, align 8, !tbaa !9
  %70 = sub nsw i64 %69, %68
  store i64 %70, ptr @sum, align 8, !tbaa !9
  br label %71

71:                                               ; preds = %57, %47
  br label %72

72:                                               ; preds = %71, %46
  %73 = load i64, ptr @s, align 8, !tbaa !9
  %74 = load i64, ptr @a, align 8, !tbaa !9
  %75 = mul nsw i64 %73, %74
  %76 = load i64, ptr @b, align 8, !tbaa !9
  %77 = add nsw i64 %75, %76
  %78 = srem i64 %77, 4294967296
  store i64 %78, ptr @s, align 8, !tbaa !9
  br label %7, !llvm.loop !13

79:                                               ; preds = %7
  %80 = load i64, ptr @sum, align 8, !tbaa !9
  %81 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %80)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  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

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

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

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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@q = external dso_local global i32, align 4
@s = external dso_local global i64, align 8
@a = external dso_local global i64, align 8
@b = external dso_local global i64, align 8
@sum = external dso_local local_unnamed_addr global i64, align 8
@h = external dso_local local_unnamed_addr global [134217728 x i16], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @q)
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @s, ptr noundef nonnull @a, ptr noundef nonnull @b)
  %q.promoted = load i32, ptr @q, align 4, !tbaa !5
  %.not15 = icmp eq i32 %q.promoted, 0
  %.pre = load i64, ptr @sum, align 8, !tbaa !9
  br i1 %.not15, label %._crit_edge19, label %.lr.ph

.lr.ph:                                           ; preds = %0
  %s.promoted = load i64, ptr @s, align 8, !tbaa !9
  %3 = load i64, ptr @a, align 8, !tbaa !9
  %4 = load i64, ptr @b, align 8, !tbaa !9
  br label %5

5:                                                ; preds = %.lr.ph, %31
  %.in = phi i32 [ %q.promoted, %.lr.ph ], [ %8, %31 ]
  %6 = phi i64 [ %s.promoted, %.lr.ph ], [ %35, %31 ]
  %7 = phi i64 [ %.pre, %.lr.ph ], [ %32, %31 ]
  %8 = add nsw i32 %.in, -1
  %9 = lshr i64 %6, 1
  %10 = trunc i64 %9 to i32
  %11 = ashr i32 %10, 27
  %12 = and i64 %6, 1
  %.not12 = icmp eq i64 %12, 0
  %13 = and i64 %9, 134217727
  %14 = getelementptr inbounds [134217728 x i16], ptr @h, i64 0, i64 %13
  %15 = load i16, ptr %14, align 2, !tbaa !11
  %16 = sext i16 %15 to i32
  %17 = shl nuw nsw i32 1, %11
  %18 = and i32 %17, %16
  %.not13 = icmp eq i32 %18, 0
  br i1 %.not12, label %25, label %19

19:                                               ; preds = %5
  br i1 %.not13, label %20, label %31

20:                                               ; preds = %19
  %21 = trunc i32 %17 to i16
  %22 = xor i16 %15, %21
  store i16 %22, ptr %14, align 2, !tbaa !11
  %sext14 = shl i64 %9, 32
  %23 = ashr exact i64 %sext14, 32
  %24 = add nsw i64 %7, %23
  br label %.sink.split

25:                                               ; preds = %5
  br i1 %.not13, label %31, label %26

26:                                               ; preds = %25
  %27 = trunc i32 %17 to i16
  %28 = xor i16 %15, %27
  store i16 %28, ptr %14, align 2, !tbaa !11
  %sext = shl i64 %9, 32
  %29 = ashr exact i64 %sext, 32
  %30 = sub nsw i64 %7, %29
  br label %.sink.split

.sink.split:                                      ; preds = %20, %26
  %.sink = phi i64 [ %30, %26 ], [ %24, %20 ]
  store i64 %.sink, ptr @sum, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %.sink.split, %25, %19
  %32 = phi i64 [ %7, %25 ], [ %7, %19 ], [ %.sink, %.sink.split ]
  %33 = mul nsw i64 %3, %6
  %34 = add nsw i64 %33, %4
  %35 = srem i64 %34, 4294967296
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %._crit_edge, label %5, !llvm.loop !13

._crit_edge:                                      ; preds = %31
  store i64 %35, ptr @s, align 8, !tbaa !9
  br label %._crit_edge19

._crit_edge19:                                    ; preds = %0, %._crit_edge
  %36 = phi i64 [ %32, %._crit_edge ], [ %.pre, %0 ]
  store i32 -1, ptr @q, align 4, !tbaa !5
  %37 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %36)
  ret i32 0
}

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

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

!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 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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