; LLVM IR from adventure-db/adventure :: lib/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"

@stdout = external global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@stdin = external global ptr, align 8

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

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

; Function Attrs: nounwind
declare noalias ptr @strdup(ptr noundef) #2

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

declare i32 @fflush(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local ptr @linenoise(ptr noundef %0) #4 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca [4096 x i8], align 16
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4096, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %8 = call i32 @isUnsupportedTerm()
  %9 = icmp ne i32 %8, 0
  br i1 %9, label %10, label %53

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, ptr noundef %11)
  %13 = load ptr, ptr @stdout, align 8, !tbaa !5
  %14 = call i32 @fflush(ptr noundef %13)
  %15 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 0
  %16 = load ptr, ptr @stdin, align 8, !tbaa !5
  %17 = call ptr @fgets(ptr noundef %15, i32 noundef 4096, ptr noundef %16)
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %20

19:                                               ; preds = %10
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %52

20:                                               ; preds = %10
  %21 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 0
  %22 = call i64 @strlen(ptr noundef %21) #6
  store i64 %22, ptr %6, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %44, %20
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %42

26:                                               ; preds = %23
  %27 = load i64, ptr %6, align 8, !tbaa !9
  %28 = sub i64 %27, 1
  %29 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = sext i8 %30 to i32
  %32 = icmp eq i32 %31, 10
  br i1 %32, label %40, label %33

33:                                               ; preds = %26
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = sub i64 %34, 1
  %36 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %38 = sext i8 %37 to i32
  %39 = icmp eq i32 %38, 13
  br label %40

40:                                               ; preds = %33, %26
  %41 = phi i1 [ true, %26 ], [ %39, %33 ]
  br label %42

42:                                               ; preds = %40, %23
  %43 = phi i1 [ false, %23 ], [ %41, %40 ]
  br i1 %43, label %44, label %49

44:                                               ; preds = %42
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = add i64 %45, -1
  store i64 %46, ptr %6, align 8, !tbaa !9
  %47 = load i64, ptr %6, align 8, !tbaa !9
  %48 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 %47
  store i8 0, ptr %48, align 1, !tbaa !11
  br label %23, !llvm.loop !12

49:                                               ; preds = %42
  %50 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 0
  %51 = call noalias ptr @strdup(ptr noundef %50) #5
  store ptr %51, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %52

52:                                               ; preds = %49, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %63

53:                                               ; preds = %1
  %54 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 0
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = call i32 @linenoiseRaw(ptr noundef %54, i64 noundef 4096, ptr noundef %55)
  store i32 %56, ptr %5, align 4, !tbaa !14
  %57 = load i32, ptr %5, align 4, !tbaa !14
  %58 = icmp eq i32 %57, -1
  br i1 %58, label %59, label %60

59:                                               ; preds = %53
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %63

60:                                               ; preds = %53
  %61 = getelementptr inbounds [4096 x i8], ptr %4, i64 0, i64 0
  %62 = call noalias ptr @strdup(ptr noundef %61) #5
  store ptr %62, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %63

63:                                               ; preds = %60, %59, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4096, ptr %4) #5
  %64 = load ptr, ptr %2, align 8
  ret ptr %64
}

; Function Attrs: nounwind uwtable
declare hidden i32 @isUnsupportedTerm() #4

declare ptr @fgets(ptr noundef, i32 noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i32 @linenoiseRaw(ptr noundef, i64 noundef, ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind willreturn memory(read) "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 "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 = { "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 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !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"

@stdout = external local_unnamed_addr global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias ptr @strdup(ptr nocapture noundef readonly) local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @linenoise(ptr noundef %0) local_unnamed_addr #4 {
  %2 = alloca [4096 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 4096, ptr nonnull %2) #5
  %3 = tail call i32 @isUnsupportedTerm()
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %16, label %4

4:                                                ; preds = %1
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, ptr noundef %0)
  %6 = load ptr, ptr @stdout, align 8, !tbaa !5
  %7 = tail call i32 @fflush(ptr noundef %6)
  %8 = load ptr, ptr @stdin, align 8, !tbaa !5
  %9 = call ptr @fgets(ptr noundef nonnull %2, i32 noundef 4096, ptr noundef %8)
  %10 = icmp eq ptr %9, null
  br i1 %10, label %20, label %11

11:                                               ; preds = %4
  %12 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %2) #6
  %.not1213 = icmp eq i64 %12, 0
  br i1 %.not1213, label %.sink.split, label %.lr.ph

.lr.ph:                                           ; preds = %11, %.critedge2
  %.014 = phi i64 [ %13, %.critedge2 ], [ %12, %11 ]
  %13 = add i64 %.014, -1
  %14 = getelementptr inbounds [4096 x i8], ptr %2, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !9
  switch i8 %15, label %.sink.split [
    i8 10, label %.critedge2
    i8 13, label %.critedge2
  ]

.critedge2:                                       ; preds = %.lr.ph, %.lr.ph
  store i8 0, ptr %14, align 1, !tbaa !9
  %.not12 = icmp eq i64 %13, 0
  br i1 %.not12, label %.sink.split, label %.lr.ph, !llvm.loop !10

16:                                               ; preds = %1
  %17 = call i32 @linenoiseRaw(ptr noundef nonnull %2, i64 noundef 4096, ptr noundef %0)
  %18 = icmp eq i32 %17, -1
  br i1 %18, label %20, label %.sink.split

.sink.split:                                      ; preds = %.lr.ph, %.critedge2, %16, %11
  %19 = call noalias ptr @strdup(ptr noundef nonnull %2) #5
  br label %20

20:                                               ; preds = %.sink.split, %16, %4
  %.1 = phi ptr [ null, %4 ], [ null, %16 ], [ %19, %.sink.split ]
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %2) #5
  ret ptr %.1
}

; Function Attrs: nounwind uwtable
declare hidden i32 @isUnsupportedTerm() local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef ptr @fgets(ptr noundef, i32 noundef, ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden i32 @linenoiseRaw(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 nofree nounwind willreturn 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 #3 = { 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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