; LLVM IR from justinxu9905/bustub-2022-fall :: third_party/linenoise (linenoise)

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

@maskmode = external hidden global i32, align 4
@mlmode = external hidden global i32, align 4
@hintsCallback = external hidden global ptr, align 8

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @linenoiseEditInsert(ptr noundef %0, i8 noundef signext %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.linenoiseState, ptr %8, i32 0, i32 8
  %10 = load i64, ptr %9, align 8, !tbaa !10
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.linenoiseState, ptr %11, i32 0, i32 3
  %13 = load i64, ptr %12, align 8, !tbaa !14
  %14 = icmp ult i64 %10, %13
  br i1 %14, label %15, label %135

15:                                               ; preds = %2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.linenoiseState, ptr %16, i32 0, i32 8
  %18 = load i64, ptr %17, align 8, !tbaa !10
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.linenoiseState, ptr %19, i32 0, i32 6
  %21 = load i64, ptr %20, align 8, !tbaa !15
  %22 = icmp eq i64 %18, %21
  br i1 %22, label %23, label %87

23:                                               ; preds = %15
  %24 = load i8, ptr %5, align 1, !tbaa !9
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.linenoiseState, ptr %25, i32 0, i32 2
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.linenoiseState, ptr %28, i32 0, i32 6
  %30 = load i64, ptr %29, align 8, !tbaa !15
  %31 = getelementptr inbounds i8, ptr %27, i64 %30
  store i8 %24, ptr %31, align 1, !tbaa !9
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.linenoiseState, ptr %32, i32 0, i32 6
  %34 = load i64, ptr %33, align 8, !tbaa !15
  %35 = add i64 %34, 1
  store i64 %35, ptr %33, align 8, !tbaa !15
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.linenoiseState, ptr %36, i32 0, i32 8
  %38 = load i64, ptr %37, align 8, !tbaa !10
  %39 = add i64 %38, 1
  store i64 %39, ptr %37, align 8, !tbaa !10
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.linenoiseState, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.linenoiseState, ptr %43, i32 0, i32 8
  %45 = load i64, ptr %44, align 8, !tbaa !10
  %46 = getelementptr inbounds i8, ptr %42, i64 %45
  store i8 0, ptr %46, align 1, !tbaa !9
  %47 = load i32, ptr @mlmode, align 4, !tbaa !17
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %84, label %49

49:                                               ; preds = %23
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.linenoiseState, ptr %50, i32 0, i32 5
  %52 = load i64, ptr %51, align 8, !tbaa !18
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.linenoiseState, ptr %53, i32 0, i32 8
  %55 = load i64, ptr %54, align 8, !tbaa !10
  %56 = add i64 %52, %55
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.linenoiseState, ptr %57, i32 0, i32 9
  %59 = load i64, ptr %58, align 8, !tbaa !19
  %60 = icmp ult i64 %56, %59
  br i1 %60, label %61, label %84

61:                                               ; preds = %49
  %62 = load ptr, ptr @hintsCallback, align 8, !tbaa !5
  %63 = icmp ne ptr %62, null
  br i1 %63, label %84, label %64

64:                                               ; preds = %61
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #4
  %65 = load i32, ptr @maskmode, align 4, !tbaa !17
  %66 = icmp eq i32 %65, 1
  br i1 %66, label %67, label %68

67:                                               ; preds = %64
  br label %71

68:                                               ; preds = %64
  %69 = load i8, ptr %5, align 1, !tbaa !9
  %70 = sext i8 %69 to i32
  br label %71

71:                                               ; preds = %68, %67
  %72 = phi i32 [ 42, %67 ], [ %70, %68 ]
  %73 = trunc i32 %72 to i8
  store i8 %73, ptr %6, align 1, !tbaa !9
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.linenoiseState, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %75, align 4, !tbaa !20
  %77 = call i64 @write(i32 noundef %76, ptr noundef %6, i64 noundef 1)
  %78 = icmp eq i64 %77, -1
  br i1 %78, label %79, label %80

79:                                               ; preds = %71
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %81

80:                                               ; preds = %71
  store i32 0, ptr %7, align 4
  br label %81

81:                                               ; preds = %80, %79
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #4
  %82 = load i32, ptr %7, align 4
  switch i32 %82, label %138 [
    i32 0, label %83
    i32 1, label %136
  ]

83:                                               ; preds = %81
  br label %86

84:                                               ; preds = %61, %49, %23
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  call void @refreshLine(ptr noundef %85)
  br label %86

86:                                               ; preds = %84, %83
  br label %134

87:                                               ; preds = %15
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.linenoiseState, ptr %88, i32 0, i32 2
  %90 = load ptr, ptr %89, align 8, !tbaa !16
  %91 = load ptr, ptr %4, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.linenoiseState, ptr %91, i32 0, i32 6
  %93 = load i64, ptr %92, align 8, !tbaa !15
  %94 = getelementptr inbounds i8, ptr %90, i64 %93
  %95 = getelementptr inbounds i8, ptr %94, i64 1
  %96 = load ptr, ptr %4, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.linenoiseState, ptr %96, i32 0, i32 2
  %98 = load ptr, ptr %97, align 8, !tbaa !16
  %99 = load ptr, ptr %4, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.linenoiseState, ptr %99, i32 0, i32 6
  %101 = load i64, ptr %100, align 8, !tbaa !15
  %102 = getelementptr inbounds i8, ptr %98, i64 %101
  %103 = load ptr, ptr %4, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.linenoiseState, ptr %103, i32 0, i32 8
  %105 = load i64, ptr %104, align 8, !tbaa !10
  %106 = load ptr, ptr %4, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.linenoiseState, ptr %106, i32 0, i32 6
  %108 = load i64, ptr %107, align 8, !tbaa !15
  %109 = sub i64 %105, %108
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %95, ptr align 1 %102, i64 %109, i1 false)
  %110 = load i8, ptr %5, align 1, !tbaa !9
  %111 = load ptr, ptr %4, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.linenoiseState, ptr %111, i32 0, i32 2
  %113 = load ptr, ptr %112, align 8, !tbaa !16
  %114 = load ptr, ptr %4, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.linenoiseState, ptr %114, i32 0, i32 6
  %116 = load i64, ptr %115, align 8, !tbaa !15
  %117 = getelementptr inbounds i8, ptr %113, i64 %116
  store i8 %110, ptr %117, align 1, !tbaa !9
  %118 = load ptr, ptr %4, align 8, !tbaa !5
  %119 = getelementptr inbounds %struct.linenoiseState, ptr %118, i32 0, i32 8
  %120 = load i64, ptr %119, align 8, !tbaa !10
  %121 = add i64 %120, 1
  store i64 %121, ptr %119, align 8, !tbaa !10
  %122 = load ptr, ptr %4, align 8, !tbaa !5
  %123 = getelementptr inbounds %struct.linenoiseState, ptr %122, i32 0, i32 6
  %124 = load i64, ptr %123, align 8, !tbaa !15
  %125 = add i64 %124, 1
  store i64 %125, ptr %123, align 8, !tbaa !15
  %126 = load ptr, ptr %4, align 8, !tbaa !5
  %127 = getelementptr inbounds %struct.linenoiseState, ptr %126, i32 0, i32 2
  %128 = load ptr, ptr %127, align 8, !tbaa !16
  %129 = load ptr, ptr %4, align 8, !tbaa !5
  %130 = getelementptr inbounds %struct.linenoiseState, ptr %129, i32 0, i32 8
  %131 = load i64, ptr %130, align 8, !tbaa !10
  %132 = getelementptr inbounds i8, ptr %128, i64 %131
  store i8 0, ptr %132, align 1, !tbaa !9
  %133 = load ptr, ptr %4, align 8, !tbaa !5
  call void @refreshLine(ptr noundef %133)
  br label %134

134:                                              ; preds = %87, %86
  br label %135

135:                                              ; preds = %134, %2
  store i32 0, ptr %3, align 4
  br label %136

136:                                              ; preds = %135, %81
  %137 = load i32, ptr %3, align 4
  ret i32 %137

138:                                              ; preds = %81
  unreachable
}

; Function Attrs: nounwind uwtable
declare hidden void @refreshLine(ptr noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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}
!10 = !{!11, !13, i64 56}
!11 = !{!"linenoiseState", !12, i64 0, !12, i64 4, !6, i64 8, !13, i64 16, !6, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !13, i64 72, !12, i64 80}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!11, !13, i64 16}
!15 = !{!11, !13, i64 40}
!16 = !{!11, !6, i64 8}
!17 = !{!12, !12, i64 0}
!18 = !{!11, !13, i64 32}
!19 = !{!11, !13, i64 64}
!20 = !{!11, !12, i64 4}


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

@maskmode = external hidden local_unnamed_addr global i32, align 4
@mlmode = external hidden local_unnamed_addr global i32, align 4
@hintsCallback = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @linenoiseEditInsert(ptr noundef %0, i8 noundef signext %1) local_unnamed_addr #2 {
  %3 = alloca i8, align 1
  %4 = getelementptr inbounds i8, ptr %0, i64 56
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %8 = icmp ult i64 %5, %7
  br i1 %8, label %9, label %54

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = icmp eq i64 %5, %11
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  br i1 %12, label %15, label %41

15:                                               ; preds = %9
  %16 = getelementptr inbounds i8, ptr %14, i64 %5
  store i8 %1, ptr %16, align 1, !tbaa !15
  %17 = load i64, ptr %10, align 8, !tbaa !13
  %18 = add i64 %17, 1
  store i64 %18, ptr %10, align 8, !tbaa !13
  %19 = load i64, ptr %4, align 8, !tbaa !5
  %20 = add i64 %19, 1
  store i64 %20, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %13, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %21, i64 %20
  store i8 0, ptr %22, align 1, !tbaa !15
  %23 = load i32, ptr @mlmode, align 4, !tbaa !16
  %.not = icmp eq i32 %23, 0
  br i1 %.not, label %24, label %.sink.split

24:                                               ; preds = %15
  %25 = getelementptr inbounds i8, ptr %0, i64 32
  %26 = load i64, ptr %25, align 8, !tbaa !17
  %27 = load i64, ptr %4, align 8, !tbaa !5
  %28 = add i64 %27, %26
  %29 = getelementptr inbounds i8, ptr %0, i64 64
  %30 = load i64, ptr %29, align 8, !tbaa !18
  %31 = icmp uge i64 %28, %30
  %32 = load ptr, ptr @hintsCallback, align 8
  %33 = icmp ne ptr %32, null
  %or.cond = select i1 %31, i1 true, i1 %33
  br i1 %or.cond, label %.sink.split, label %34

34:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %3) #4
  %35 = load i32, ptr @maskmode, align 4, !tbaa !16
  %36 = icmp eq i32 %35, 1
  %37 = select i1 %36, i8 42, i8 %1
  store i8 %37, ptr %3, align 1, !tbaa !15
  %38 = getelementptr inbounds i8, ptr %0, i64 4
  %39 = load i32, ptr %38, align 4, !tbaa !19
  %40 = call i64 @write(i32 noundef %39, ptr noundef nonnull %3, i64 noundef 1) #4
  %.not37 = icmp eq i64 %40, -1
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #4
  br i1 %.not37, label %55, label %54

41:                                               ; preds = %9
  %42 = getelementptr inbounds i8, ptr %14, i64 %11
  %43 = getelementptr inbounds i8, ptr %42, i64 1
  %44 = sub i64 %5, %11
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %43, ptr align 1 %42, i64 %44, i1 false)
  %45 = load ptr, ptr %13, align 8, !tbaa !14
  %46 = load i64, ptr %10, align 8, !tbaa !13
  %47 = getelementptr inbounds i8, ptr %45, i64 %46
  store i8 %1, ptr %47, align 1, !tbaa !15
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = add i64 %48, 1
  store i64 %49, ptr %4, align 8, !tbaa !5
  %50 = load i64, ptr %10, align 8, !tbaa !13
  %51 = add i64 %50, 1
  store i64 %51, ptr %10, align 8, !tbaa !13
  %52 = load ptr, ptr %13, align 8, !tbaa !14
  %53 = getelementptr inbounds i8, ptr %52, i64 %49
  store i8 0, ptr %53, align 1, !tbaa !15
  br label %.sink.split

.sink.split:                                      ; preds = %15, %24, %41
  tail call void @refreshLine(ptr noundef nonnull %0)
  br label %54

54:                                               ; preds = %.sink.split, %34, %2
  br label %55

55:                                               ; preds = %34, %54
  %.1 = phi i32 [ 0, %54 ], [ -1, %34 ]
  ret i32 %.1
}

; Function Attrs: nounwind uwtable
declare hidden void @refreshLine(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

attributes #0 = { nofree "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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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, !11, i64 56}
!6 = !{!"linenoiseState", !7, i64 0, !7, i64 4, !10, i64 8, !11, i64 16, !10, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !7, i64 80}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !11, i64 16}
!13 = !{!6, !11, i64 40}
!14 = !{!6, !10, i64 8}
!15 = !{!8, !8, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !11, i64 32}
!18 = !{!6, !11, i64 64}
!19 = !{!6, !7, i64 4}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass