; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions (RMID)

; 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.node = type { i32, ptr, ptr }

@start = external dso_local global ptr, align 8
@med = external dso_local global ptr, align 8
@temp = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external global ptr, align 8

; 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
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %97, %0
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  %6 = icmp ne i32 %5, -1
  br i1 %6, label %7, label %98

7:                                                ; preds = %4
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %10, label %42

10:                                               ; preds = %7
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = add nsw i32 %11, 1
  store i32 %12, ptr %3, align 4, !tbaa !5
  %13 = call noalias ptr @malloc(i64 noundef 8) #6
  store ptr %13, ptr @temp, align 8, !tbaa !9
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = load ptr, ptr @temp, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.node, ptr %15, i32 0, i32 0
  store i32 %14, ptr %16, align 8, !tbaa !11
  %17 = load ptr, ptr @temp, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.node, ptr %17, i32 0, i32 2
  store ptr null, ptr %18, align 8, !tbaa !13
  %19 = load ptr, ptr @temp, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.node, ptr %19, i32 0, i32 1
  store ptr null, ptr %20, align 8, !tbaa !14
  %21 = load ptr, ptr @med, align 8, !tbaa !9
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %25

23:                                               ; preds = %10
  %24 = load ptr, ptr @temp, align 8, !tbaa !9
  store ptr %24, ptr @med, align 8, !tbaa !9
  store ptr %24, ptr @start, align 8, !tbaa !9
  br label %41

25:                                               ; preds = %10
  %26 = load ptr, ptr @temp, align 8, !tbaa !9
  %27 = load ptr, ptr @start, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.node, ptr %27, i32 0, i32 2
  store ptr %26, ptr %28, align 8, !tbaa !13
  %29 = load ptr, ptr @start, align 8, !tbaa !9
  %30 = load ptr, ptr @temp, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.node, ptr %30, i32 0, i32 1
  store ptr %29, ptr %31, align 8, !tbaa !14
  %32 = load ptr, ptr @temp, align 8, !tbaa !9
  store ptr %32, ptr @start, align 8, !tbaa !9
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = and i32 %33, 1
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %40

36:                                               ; preds = %25
  %37 = load ptr, ptr @med, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.node, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  store ptr %39, ptr @med, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %36, %25
  br label %41

41:                                               ; preds = %40, %23
  br label %97

42:                                               ; preds = %7
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = icmp eq i32 %43, -1
  br i1 %44, label %45, label %92

45:                                               ; preds = %42
  %46 = load ptr, ptr @med, align 8, !tbaa !9
  store ptr %46, ptr @temp, align 8, !tbaa !9
  %47 = load ptr, ptr @med, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.node, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !11
  %50 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %49)
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = and i32 %51, 1
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %58

54:                                               ; preds = %45
  %55 = load ptr, ptr @med, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.node, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !14
  store ptr %57, ptr @med, align 8, !tbaa !9
  br label %62

58:                                               ; preds = %45
  %59 = load ptr, ptr @med, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.node, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  store ptr %61, ptr @med, align 8, !tbaa !9
  br label %62

62:                                               ; preds = %58, %54
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = add nsw i32 %63, -1
  store i32 %64, ptr %3, align 4, !tbaa !5
  %65 = load ptr, ptr @temp, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.node, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = icmp ne ptr %67, null
  br i1 %68, label %69, label %77

69:                                               ; preds = %62
  %70 = load ptr, ptr @temp, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.node, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %71, align 8, !tbaa !13
  %73 = load ptr, ptr @temp, align 8, !tbaa !9
  %74 = getelementptr inbounds %struct.node, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !14
  %76 = getelementptr inbounds %struct.node, ptr %75, i32 0, i32 2
  store ptr %72, ptr %76, align 8, !tbaa !13
  br label %77

77:                                               ; preds = %69, %62
  %78 = load ptr, ptr @temp, align 8, !tbaa !9
  %79 = getelementptr inbounds %struct.node, ptr %78, i32 0, i32 2
  %80 = load ptr, ptr %79, align 8, !tbaa !13
  %81 = icmp ne ptr %80, null
  br i1 %81, label %82, label %90

82:                                               ; preds = %77
  %83 = load ptr, ptr @temp, align 8, !tbaa !9
  %84 = getelementptr inbounds %struct.node, ptr %83, i32 0, i32 1
  %85 = load ptr, ptr %84, align 8, !tbaa !14
  %86 = load ptr, ptr @temp, align 8, !tbaa !9
  %87 = getelementptr inbounds %struct.node, ptr %86, i32 0, i32 2
  %88 = load ptr, ptr %87, align 8, !tbaa !13
  %89 = getelementptr inbounds %struct.node, ptr %88, i32 0, i32 1
  store ptr %85, ptr %89, align 8, !tbaa !14
  br label %90

90:                                               ; preds = %82, %77
  %91 = load ptr, ptr @temp, align 8, !tbaa !9
  call void @free(ptr noundef %91) #5
  br label %96

92:                                               ; preds = %42
  %93 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  store ptr null, ptr @med, align 8, !tbaa !9
  store ptr null, ptr @start, align 8, !tbaa !9
  store i32 0, ptr %3, align 4, !tbaa !5
  %94 = load ptr, ptr @stdin, align 8, !tbaa !9
  %95 = call i32 @fflush(ptr noundef %94)
  br label %96

96:                                               ; preds = %92, %90
  br label %97

97:                                               ; preds = %96, %41
  br label %4, !llvm.loop !15

98:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  ret i32 0
}

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

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

declare i32 @fflush(ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { nounwind allocsize(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 "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 }
attributes #6 = { nounwind allocsize(0) }

!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, !6, i64 0}
!12 = !{!"_ZTS4node", !6, i64 0, !10, i64 8, !10, i64 16}
!13 = !{!12, !10, i64 16}
!14 = !{!12, !10, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@start = external dso_local local_unnamed_addr global ptr, align 8
@med = external dso_local local_unnamed_addr global ptr, align 8
@temp = external dso_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #6
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %.not11 = icmp eq i32 %2, -1
  br i1 %.not11, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %40
  %.012 = phi i32 [ %.1, %40 ], [ 0, %0 ]
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %5, label %19

5:                                                ; preds = %.lr.ph
  %6 = add nsw i32 %.012, 1
  %7 = call noalias dereferenceable_or_null(8) ptr @malloc(i64 noundef 8) #7
  store ptr %7, ptr @temp, align 8, !tbaa !9
  store i32 %3, ptr %7, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %8, i8 0, i64 16, i1 false)
  %9 = load ptr, ptr @med, align 8, !tbaa !9
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %5
  store ptr %7, ptr @med, align 8, !tbaa !9
  store ptr %7, ptr @start, align 8, !tbaa !9
  br label %40

12:                                               ; preds = %5
  %13 = load ptr, ptr @start, align 8, !tbaa !9
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  store ptr %7, ptr %14, align 8, !tbaa !13
  store ptr %13, ptr %8, align 8, !tbaa !14
  store ptr %7, ptr @start, align 8, !tbaa !9
  %15 = and i32 %.012, 1
  %.not10.not = icmp eq i32 %15, 0
  br i1 %.not10.not, label %16, label %40

16:                                               ; preds = %12
  %17 = getelementptr inbounds i8, ptr %9, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !13
  store ptr %18, ptr @med, align 8, !tbaa !9
  br label %40

19:                                               ; preds = %.lr.ph
  %20 = icmp eq i32 %3, -1
  br i1 %20, label %21, label %36

21:                                               ; preds = %19
  %22 = load ptr, ptr @med, align 8, !tbaa !9
  store ptr %22, ptr @temp, align 8, !tbaa !9
  %23 = load i32, ptr %22, align 8, !tbaa !11
  %24 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %23)
  %25 = and i32 %.012, 1
  %.not7 = icmp eq i32 %25, 0
  %26 = load ptr, ptr @med, align 8
  %storemerge.in.v = select i1 %.not7, i64 16, i64 8
  %storemerge.in = getelementptr inbounds i8, ptr %26, i64 %storemerge.in.v
  %storemerge = load ptr, ptr %storemerge.in, align 8, !tbaa !9
  store ptr %storemerge, ptr @med, align 8, !tbaa !9
  %27 = add nsw i32 %.012, -1
  %28 = load ptr, ptr @temp, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %28, i64 8
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  %.not8 = icmp eq ptr %30, null
  %.phi.trans.insert = getelementptr inbounds i8, ptr %28, i64 16
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !13
  br i1 %.not8, label %._crit_edge13, label %31

31:                                               ; preds = %21
  %32 = getelementptr inbounds i8, ptr %30, i64 16
  store ptr %.pre, ptr %32, align 8, !tbaa !13
  br label %._crit_edge13

._crit_edge13:                                    ; preds = %21, %31
  %.not9 = icmp eq ptr %.pre, null
  br i1 %.not9, label %35, label %33

33:                                               ; preds = %._crit_edge13
  %34 = getelementptr inbounds i8, ptr %.pre, i64 8
  store ptr %30, ptr %34, align 8, !tbaa !14
  br label %35

35:                                               ; preds = %33, %._crit_edge13
  call void @free(ptr noundef nonnull %28) #6
  br label %40

36:                                               ; preds = %19
  %37 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  store ptr null, ptr @med, align 8, !tbaa !9
  store ptr null, ptr @start, align 8, !tbaa !9
  %38 = load ptr, ptr @stdin, align 8, !tbaa !9
  %39 = call i32 @fflush(ptr noundef %38)
  br label %40

40:                                               ; preds = %35, %36, %11, %16, %12
  %.1 = phi i32 [ %6, %11 ], [ %6, %16 ], [ %6, %12 ], [ %27, %35 ], [ 0, %36 ]
  %41 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %.not = icmp eq i32 %41, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %40, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #6
  ret i32 0
}

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

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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #3

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #4

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #5

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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, !6, i64 0}
!12 = !{!"_ZTS4node", !6, i64 0, !10, i64 8, !10, i64 16}
!13 = !{!12, !10, i64 16}
!14 = !{!12, !10, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass