; LLVM IR from reyadussalahin/problem-solving :: CONTEST/random_contest/Y2019/practice_for_preli/FBullsCowsAndDigits (Solution)

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

@bull = external dso_local global i32, align 4
@cow = external dso_local global i32, align 4
@used = external dso_local global [8 x i32], align 16
@my_stack = external dso_local global [8 x i32], align 16
@selected = external dso_local global [8 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

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

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) #0

declare i32 @fflush(ptr noundef) #1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4permi(i32 noundef %0) #3 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp eq i32 %5, 4
  br i1 %6, label %7, label %33

7:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %21, %7
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 4
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %8
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [8 x i32], ptr @my_stack, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [8 x i32], ptr @selected, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %19)
  br label %21

21:                                               ; preds = %12
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %3, align 4, !tbaa !5
  br label %8, !llvm.loop !9

24:                                               ; preds = %11
  %25 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  %26 = load ptr, ptr @stdout, align 8, !tbaa !11
  %27 = call i32 @fflush(ptr noundef %26)
  %28 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef @bull, ptr noundef @cow)
  %29 = load i32, ptr @bull, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 4
  br i1 %30, label %31, label %32

31:                                               ; preds = %24
  call void @exit(i32 noundef 0) #5
  unreachable

32:                                               ; preds = %24
  br label %61

33:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %58, %33
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = icmp slt i32 %35, 4
  br i1 %36, label %38, label %37

37:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %61

38:                                               ; preds = %34
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [8 x i32], ptr @used, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %57, label %44

44:                                               ; preds = %38
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [8 x i32], ptr @used, i64 0, i64 %46
  store i32 1, ptr %47, align 4, !tbaa !5
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = load i32, ptr %2, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [8 x i32], ptr @my_stack, i64 0, i64 %50
  store i32 %48, ptr %51, align 4, !tbaa !5
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  call void @_Z4permi(i32 noundef %53)
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [8 x i32], ptr @used, i64 0, i64 %55
  store i32 0, ptr %56, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %44, %38
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %4, align 4, !tbaa !5
  br label %34, !llvm.loop !13

61:                                               ; preds = %37, %32
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { noreturn 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 #3 = { 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 }
attributes #5 = { noreturn 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 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !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"

@bull = external dso_local global i32, align 4
@cow = external dso_local global i32, align 4
@used = external dso_local local_unnamed_addr global [8 x i32], align 16
@my_stack = external dso_local local_unnamed_addr global [8 x i32], align 16
@selected = external dso_local local_unnamed_addr global [8 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

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

; Function Attrs: nofree nounwind
declare noundef i32 @fflush(ptr nocapture noundef) local_unnamed_addr #0

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4permi(i32 noundef %0) local_unnamed_addr #2 {
  %2 = icmp eq i32 %0, 4
  br i1 %2, label %.preheader.preheader, label %.preheader14

.preheader.preheader:                             ; preds = %1
  %3 = load i32, ptr @my_stack, align 16, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds [8 x i32], ptr @selected, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %6)
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @my_stack, i64 4), align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [8 x i32], ptr @selected, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %11)
  %13 = load i32, ptr getelementptr inbounds (i8, ptr @my_stack, i64 8), align 8, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [8 x i32], ptr @selected, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %16)
  %18 = load i32, ptr getelementptr inbounds (i8, ptr @my_stack, i64 12), align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [8 x i32], ptr @selected, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %21)
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %24 = load ptr, ptr @stdout, align 8, !tbaa !9
  %25 = tail call i32 @fflush(ptr noundef %24)
  %26 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull @bull, ptr noundef nonnull @cow)
  %27 = load i32, ptr @bull, align 4, !tbaa !5
  %28 = icmp eq i32 %27, 4
  br i1 %28, label %33, label %common.ret

.preheader14:                                     ; preds = %1
  %29 = sext i32 %0 to i64
  %30 = getelementptr inbounds [8 x i32], ptr @my_stack, i64 0, i64 %29
  %31 = add nsw i32 %0, 1
  %32 = load i32, ptr @used, align 16, !tbaa !5
  %.not = icmp eq i32 %32, 0
  br i1 %.not, label %34, label %35

33:                                               ; preds = %.preheader.preheader
  tail call void @exit(i32 noundef 0) #3
  unreachable

34:                                               ; preds = %.preheader14
  store i32 1, ptr @used, align 16, !tbaa !5
  store i32 0, ptr %30, align 4, !tbaa !5
  tail call void @_Z4permi(i32 noundef %31)
  store i32 0, ptr @used, align 16, !tbaa !5
  br label %35

35:                                               ; preds = %.preheader14, %34
  %36 = load i32, ptr getelementptr inbounds (i8, ptr @used, i64 4), align 4, !tbaa !5
  %.not.1 = icmp eq i32 %36, 0
  br i1 %.not.1, label %37, label %38

37:                                               ; preds = %35
  store i32 1, ptr getelementptr inbounds (i8, ptr @used, i64 4), align 4, !tbaa !5
  store i32 1, ptr %30, align 4, !tbaa !5
  tail call void @_Z4permi(i32 noundef %31)
  store i32 0, ptr getelementptr inbounds (i8, ptr @used, i64 4), align 4, !tbaa !5
  br label %38

38:                                               ; preds = %37, %35
  %39 = load i32, ptr getelementptr inbounds (i8, ptr @used, i64 8), align 8, !tbaa !5
  %.not.2 = icmp eq i32 %39, 0
  br i1 %.not.2, label %40, label %41

40:                                               ; preds = %38
  store i32 1, ptr getelementptr inbounds (i8, ptr @used, i64 8), align 8, !tbaa !5
  store i32 2, ptr %30, align 4, !tbaa !5
  tail call void @_Z4permi(i32 noundef %31)
  store i32 0, ptr getelementptr inbounds (i8, ptr @used, i64 8), align 8, !tbaa !5
  br label %41

41:                                               ; preds = %40, %38
  %42 = load i32, ptr getelementptr inbounds (i8, ptr @used, i64 12), align 4, !tbaa !5
  %.not.3 = icmp eq i32 %42, 0
  br i1 %.not.3, label %43, label %common.ret

common.ret:                                       ; preds = %.preheader.preheader, %41, %43
  ret void

43:                                               ; preds = %41
  store i32 1, ptr getelementptr inbounds (i8, ptr @used, i64 12), align 4, !tbaa !5
  store i32 3, ptr %30, align 4, !tbaa !5
  tail call void @_Z4permi(i32 noundef %31)
  store i32 0, ptr getelementptr inbounds (i8, ptr @used, i64 12), align 4, !tbaa !5
  br label %common.ret
}

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass