; LLVM IR from xing1357/SimpleOS :: src (console)

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

@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 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

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

; Function Attrs: nounwind uwtable
declare dso_local void @console_ungetchar_bound(i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local void @printf(ptr noundef, ...) #1

declare signext i8 @kb_getchar(...) #2

; Function Attrs: nounwind uwtable
define dso_local void @getstr_bound(ptr noundef %0, i8 noundef zeroext %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %2
  br label %37

10:                                               ; preds = %2
  br label %11

11:                                               ; preds = %36, %10
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  %12 = call signext i8 (...) @kb_getchar()
  store i8 %12, ptr %5, align 1, !tbaa !9
  %13 = load i8, ptr %5, align 1, !tbaa !9
  %14 = sext i8 %13 to i32
  %15 = icmp eq i32 %14, 10
  br i1 %15, label %16, label %17

16:                                               ; preds = %11
  call void (ptr, ...) @printf(ptr noundef @.str.1)
  store i32 1, ptr %6, align 4
  br label %34

17:                                               ; preds = %11
  %18 = load i8, ptr %5, align 1, !tbaa !9
  %19 = sext i8 %18 to i32
  %20 = icmp eq i32 %19, 8
  br i1 %20, label %21, label %26

21:                                               ; preds = %17
  %22 = load i8, ptr %4, align 1, !tbaa !9
  call void @console_ungetchar_bound(i8 noundef zeroext %22)
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i32 -1
  store ptr %24, ptr %3, align 8, !tbaa !5
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  store i8 0, ptr %25, align 1, !tbaa !9
  br label %32

26:                                               ; preds = %17
  %27 = load i8, ptr %5, align 1, !tbaa !9
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i32 1
  store ptr %29, ptr %3, align 8, !tbaa !5
  store i8 %27, ptr %28, align 1, !tbaa !9
  %30 = load i8, ptr %5, align 1, !tbaa !9
  %31 = sext i8 %30 to i32
  call void (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %31)
  br label %32

32:                                               ; preds = %26, %21
  br label %33

33:                                               ; preds = %32
  store i32 0, ptr %6, align 4
  br label %34

34:                                               ; preds = %33, %16
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  %35 = load i32, ptr %6, align 4
  switch i32 %35, label %38 [
    i32 0, label %36
    i32 1, label %37
  ]

36:                                               ; preds = %34
  br label %11

37:                                               ; preds = %34, %9
  ret void

38:                                               ; preds = %34
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}


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

@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @console_ungetchar_bound(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @printf(ptr noundef, ...) local_unnamed_addr #0

declare signext i8 @kb_getchar(...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @getstr_bound(ptr noundef writeonly %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %9, label %.preheader

.preheader:                                       ; preds = %2, %.preheader.backedge
  %.09 = phi ptr [ %.09.be, %.preheader.backedge ], [ %0, %2 ]
  %3 = tail call signext i8 (...) @kb_getchar() #2
  switch i8 %3, label %6 [
    i8 10, label %.thread
    i8 8, label %4
  ]

.thread:                                          ; preds = %.preheader
  tail call void (ptr, ...) @printf(ptr noundef nonnull @.str.1)
  br label %9

4:                                                ; preds = %.preheader
  tail call void @console_ungetchar_bound(i8 noundef zeroext %1)
  %5 = getelementptr inbounds i8, ptr %.09, i64 -1
  store i8 0, ptr %5, align 1, !tbaa !5
  br label %.preheader.backedge

6:                                                ; preds = %.preheader
  %7 = sext i8 %3 to i32
  %8 = getelementptr inbounds i8, ptr %.09, i64 1
  store i8 %3, ptr %.09, align 1, !tbaa !5
  tail call void (ptr, ...) @printf(ptr noundef nonnull @.str.2, i32 noundef %7)
  br label %.preheader.backedge

.preheader.backedge:                              ; preds = %6, %4
  %.09.be = phi ptr [ %5, %4 ], [ %8, %6 ]
  br label %.preheader

9:                                                ; preds = %.thread, %2
  ret void
}

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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