; LLVM IR from asllop/AppOS :: term (term)

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

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

declare ptr @get_term_driver(i32 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 @core_lock(i32 noundef) #1

declare i32 @core_unlock(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @term_gets(i32 noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  %12 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = call ptr @get_term_driver(i32 noundef %13)
  store ptr %14, ptr %8, align 8, !tbaa !9
  %15 = load ptr, ptr %8, align 8, !tbaa !9
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %99

17:                                               ; preds = %3
  %18 = call i32 @core_lock(i32 noundef 4)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %90, %17
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 1
  %23 = icmp slt i32 %20, %22
  br i1 %23, label %24, label %91

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  br label %25

25:                                               ; preds = %33, %24
  %26 = load ptr, ptr %8, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.TermDriverStruct, ptr %26, i32 0, i32 10
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = load ptr, ptr %8, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.TermDriverStruct, ptr %29, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !13
  %32 = call i32 %28(i32 noundef %31)
  store i32 %32, ptr %10, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %25
  %34 = load i32, ptr %10, align 4, !tbaa !5
  %35 = icmp eq i32 %34, -1
  br i1 %35, label %25, label %36, !llvm.loop !14

36:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #3
  %37 = load i32, ptr %10, align 4, !tbaa !5
  %38 = and i32 %37, 255
  %39 = trunc i32 %38 to i8
  store i8 %39, ptr %11, align 1, !tbaa !16
  %40 = load i8, ptr %11, align 1, !tbaa !16
  %41 = sext i8 %40 to i32
  %42 = icmp eq i32 %41, 13
  br i1 %42, label %47, label %43

43:                                               ; preds = %36
  %44 = load i8, ptr %11, align 1, !tbaa !16
  %45 = sext i8 %44 to i32
  %46 = icmp eq i32 %45, 10
  br i1 %46, label %47, label %54

47:                                               ; preds = %43, %36
  %48 = load ptr, ptr %6, align 8, !tbaa !9
  %49 = load i32, ptr %9, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  store i8 0, ptr %51, align 1, !tbaa !16
  %52 = call i32 @core_unlock(i32 noundef 4)
  %53 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %53, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %88

54:                                               ; preds = %43
  %55 = load i8, ptr %11, align 1, !tbaa !16
  %56 = sext i8 %55 to i32
  %57 = icmp eq i32 %56, 127
  br i1 %57, label %62, label %58

58:                                               ; preds = %54
  %59 = load i8, ptr %11, align 1, !tbaa !16
  %60 = sext i8 %59 to i32
  %61 = icmp eq i32 %60, 8
  br i1 %61, label %62, label %73

62:                                               ; preds = %58, %54
  %63 = load i32, ptr %9, align 4, !tbaa !5
  %64 = add nsw i32 %63, -1
  store i32 %64, ptr %9, align 4, !tbaa !5
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = icmp slt i32 %65, 0
  br i1 %66, label %67, label %68

67:                                               ; preds = %62
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %67, %62
  %69 = load ptr, ptr %6, align 8, !tbaa !9
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds i8, ptr %69, i64 %71
  store i8 0, ptr %72, align 1, !tbaa !16
  br label %86

73:                                               ; preds = %58
  %74 = load i8, ptr %11, align 1, !tbaa !16
  %75 = sext i8 %74 to i32
  %76 = icmp sge i32 %75, 32
  br i1 %76, label %77, label %85

77:                                               ; preds = %73
  %78 = load i8, ptr %11, align 1, !tbaa !16
  %79 = load ptr, ptr %6, align 8, !tbaa !9
  %80 = load i32, ptr %9, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds i8, ptr %79, i64 %81
  store i8 %78, ptr %82, align 1, !tbaa !16
  %83 = load i32, ptr %9, align 4, !tbaa !5
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %9, align 4, !tbaa !5
  br label %85

85:                                               ; preds = %77, %73
  br label %86

86:                                               ; preds = %85, %68
  br label %87

87:                                               ; preds = %86
  store i32 0, ptr %12, align 4
  br label %88

88:                                               ; preds = %87, %47
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %89 = load i32, ptr %12, align 4
  switch i32 %89, label %98 [
    i32 0, label %90
  ]

90:                                               ; preds = %88
  br label %19, !llvm.loop !17

91:                                               ; preds = %19
  %92 = load ptr, ptr %6, align 8, !tbaa !9
  %93 = load i32, ptr %9, align 4, !tbaa !5
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds i8, ptr %92, i64 %94
  store i8 0, ptr %95, align 1, !tbaa !16
  %96 = call i32 @core_unlock(i32 noundef 4)
  %97 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %97, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %98

98:                                               ; preds = %91, %88
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %100

99:                                               ; preds = %3
  store i32 -505, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %100

100:                                              ; preds = %99, %98
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %101 = load i32, ptr %4, align 4
  ret i32 %101
}

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 = { 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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 72}
!12 = !{!"TermDriverStruct", !6, i64 0, !6, i64 4, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72}
!13 = !{!12, !6, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !15}


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

declare ptr @get_term_driver(i32 noundef) local_unnamed_addr #0

declare i32 @core_lock(i32 noundef) local_unnamed_addr #0

declare i32 @core_unlock(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @term_gets(i32 noundef %0, ptr nocapture noundef writeonly %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = tail call ptr @get_term_driver(i32 noundef %0) #3
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %.loopexit, label %5

5:                                                ; preds = %3
  %6 = tail call i32 @core_lock(i32 noundef 4) #3
  %7 = add nsw i32 %2, -1
  %8 = getelementptr inbounds i8, ptr %4, i64 72
  %9 = getelementptr inbounds i8, ptr %4, i64 4
  br label %10

10:                                               ; preds = %32, %5
  %.034 = phi i32 [ 0, %5 ], [ %.236, %32 ]
  %.033 = phi i32 [ undef, %5 ], [ %.1, %32 ]
  %11 = icmp slt i32 %.034, %7
  br i1 %11, label %.preheader, label %33

.preheader:                                       ; preds = %10, %.preheader
  %12 = load ptr, ptr %8, align 8, !tbaa !5
  %13 = load i32, ptr %9, align 4, !tbaa !11
  %14 = tail call i32 %12(i32 noundef %13) #3
  %15 = icmp eq i32 %14, -1
  br i1 %15, label %.preheader, label %16, !llvm.loop !12

16:                                               ; preds = %.preheader
  %17 = trunc i32 %14 to i8
  %sext = shl i32 %14, 24
  switch i32 %sext, label %26 [
    i32 218103808, label %18
    i32 167772160, label %18
    i32 2130706432, label %22
    i32 134217728, label %22
  ]

18:                                               ; preds = %16, %16
  %19 = sext i32 %.034 to i64
  %20 = getelementptr inbounds i8, ptr %1, i64 %19
  store i8 0, ptr %20, align 1, !tbaa !14
  %21 = tail call i32 @core_unlock(i32 noundef 4) #3
  br label %32

22:                                               ; preds = %16, %16
  %23 = tail call i32 @llvm.smax.i32(i32 %.034, i32 1)
  %spec.store.select = add nsw i32 %23, -1
  %24 = zext nneg i32 %spec.store.select to i64
  %25 = getelementptr inbounds i8, ptr %1, i64 %24
  store i8 0, ptr %25, align 1, !tbaa !14
  br label %32

26:                                               ; preds = %16
  %27 = icmp sgt i32 %sext, 520093696
  br i1 %27, label %28, label %32

28:                                               ; preds = %26
  %29 = sext i32 %.034 to i64
  %30 = getelementptr inbounds i8, ptr %1, i64 %29
  store i8 %17, ptr %30, align 1, !tbaa !14
  %31 = add nsw i32 %.034, 1
  br label %32

32:                                               ; preds = %26, %28, %22, %18
  %.236 = phi i32 [ %.034, %18 ], [ %spec.store.select, %22 ], [ %31, %28 ], [ %.034, %26 ]
  %.1 = phi i32 [ %.034, %18 ], [ %.033, %22 ], [ %.033, %28 ], [ %.033, %26 ]
  switch i32 %sext, label %10 [
    i32 218103808, label %.loopexit
    i32 167772160, label %.loopexit
  ]

33:                                               ; preds = %10
  %34 = sext i32 %.034 to i64
  %35 = getelementptr inbounds i8, ptr %1, i64 %34
  store i8 0, ptr %35, align 1, !tbaa !14
  %36 = tail call i32 @core_unlock(i32 noundef 4) #3
  br label %.loopexit

.loopexit:                                        ; preds = %32, %32, %3, %33
  %.3 = phi i32 [ %.034, %33 ], [ -505, %3 ], [ %.1, %32 ], [ %.1, %32 ]
  ret i32 %.3
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

attributes #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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !10, i64 72}
!6 = !{!"TermDriverStruct", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!8, !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass