; LLVM IR from 0intro/wmii :: lib/libbio (bgetc)

; 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.Biobuf = type { i32, i32, i32, i32, i32, i32, i32, i64, i32, ptr, ptr, ptr, [8196 x i8] }

; Function Attrs: nounwind uwtable
define dso_local i32 @Bgetc(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  br label %6

6:                                                ; preds = %107, %1
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.Biobuf, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 8, !tbaa !9
  store i32 %9, ptr %4, align 4, !tbaa !13
  %10 = load i32, ptr %4, align 4, !tbaa !13
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %25

12:                                               ; preds = %6
  %13 = load i32, ptr %4, align 4, !tbaa !13
  %14 = add nsw i32 %13, 1
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Biobuf, ptr %15, i32 0, i32 0
  store i32 %14, ptr %16, align 8, !tbaa !9
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.Biobuf, ptr %17, i32 0, i32 10
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %20 = load i32, ptr %4, align 4, !tbaa !13
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !15
  %24 = zext i8 %23 to i32
  store i32 %24, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %118

25:                                               ; preds = %6
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.Biobuf, ptr %26, i32 0, i32 4
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = icmp ne i32 %28, 1
  br i1 %29, label %30, label %39

30:                                               ; preds = %25
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Biobuf, ptr %31, i32 0, i32 4
  %33 = load i32, ptr %32, align 8, !tbaa !16
  %34 = icmp eq i32 %33, 3
  br i1 %34, label %35, label %38

35:                                               ; preds = %30
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Biobuf, ptr %36, i32 0, i32 4
  store i32 1, ptr %37, align 8, !tbaa !16
  br label %38

38:                                               ; preds = %35, %30
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %118

39:                                               ; preds = %25
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Biobuf, ptr %40, i32 0, i32 9
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  %43 = getelementptr inbounds i8, ptr %42, i64 -4
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Biobuf, ptr %44, i32 0, i32 10
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = getelementptr inbounds i8, ptr %46, i64 -4
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %43, ptr align 1 %47, i64 4, i1 false)
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Biobuf, ptr %48, i32 0, i32 5
  %50 = load i32, ptr %49, align 4, !tbaa !18
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.Biobuf, ptr %51, i32 0, i32 9
  %53 = load ptr, ptr %52, align 8, !tbaa !17
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.Biobuf, ptr %54, i32 0, i32 8
  %56 = load i32, ptr %55, align 8, !tbaa !19
  %57 = sext i32 %56 to i64
  %58 = call i64 @read(i32 noundef %50, ptr noundef %53, i64 noundef %57)
  %59 = trunc i64 %58 to i32
  store i32 %59, ptr %4, align 4, !tbaa !13
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Biobuf, ptr %60, i32 0, i32 9
  %62 = load ptr, ptr %61, align 8, !tbaa !17
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.Biobuf, ptr %63, i32 0, i32 11
  store ptr %62, ptr %64, align 8, !tbaa !20
  %65 = load i32, ptr %4, align 4, !tbaa !13
  %66 = icmp sle i32 %65, 0
  br i1 %66, label %67, label %76

67:                                               ; preds = %39
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.Biobuf, ptr %68, i32 0, i32 4
  store i32 3, ptr %69, align 8, !tbaa !16
  %70 = load i32, ptr %4, align 4, !tbaa !13
  %71 = icmp slt i32 %70, 0
  br i1 %71, label %72, label %75

72:                                               ; preds = %67
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.Biobuf, ptr %73, i32 0, i32 4
  store i32 0, ptr %74, align 8, !tbaa !16
  br label %75

75:                                               ; preds = %72, %67
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %118

76:                                               ; preds = %39
  %77 = load i32, ptr %4, align 4, !tbaa !13
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Biobuf, ptr %78, i32 0, i32 8
  %80 = load i32, ptr %79, align 8, !tbaa !19
  %81 = icmp slt i32 %77, %80
  br i1 %81, label %82, label %107

82:                                               ; preds = %76
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.Biobuf, ptr %83, i32 0, i32 10
  %85 = load ptr, ptr %84, align 8, !tbaa !14
  %86 = load i32, ptr %4, align 4, !tbaa !13
  %87 = sext i32 %86 to i64
  %88 = sub i64 0, %87
  %89 = getelementptr inbounds i8, ptr %85, i64 %88
  %90 = getelementptr inbounds i8, ptr %89, i64 -4
  %91 = load ptr, ptr %3, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.Biobuf, ptr %91, i32 0, i32 9
  %93 = load ptr, ptr %92, align 8, !tbaa !17
  %94 = getelementptr inbounds i8, ptr %93, i64 -4
  %95 = load i32, ptr %4, align 4, !tbaa !13
  %96 = add nsw i32 %95, 4
  %97 = sext i32 %96 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %90, ptr align 1 %94, i64 %97, i1 false)
  %98 = load ptr, ptr %3, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.Biobuf, ptr %98, i32 0, i32 10
  %100 = load ptr, ptr %99, align 8, !tbaa !14
  %101 = load i32, ptr %4, align 4, !tbaa !13
  %102 = sext i32 %101 to i64
  %103 = sub i64 0, %102
  %104 = getelementptr inbounds i8, ptr %100, i64 %103
  %105 = load ptr, ptr %3, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.Biobuf, ptr %105, i32 0, i32 11
  store ptr %104, ptr %106, align 8, !tbaa !20
  br label %107

107:                                              ; preds = %82, %76
  %108 = load i32, ptr %4, align 4, !tbaa !13
  %109 = sub nsw i32 0, %108
  %110 = load ptr, ptr %3, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.Biobuf, ptr %110, i32 0, i32 0
  store i32 %109, ptr %111, align 8, !tbaa !9
  %112 = load i32, ptr %4, align 4, !tbaa !13
  %113 = sext i32 %112 to i64
  %114 = load ptr, ptr %3, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.Biobuf, ptr %114, i32 0, i32 7
  %116 = load i64, ptr %115, align 8, !tbaa !21
  %117 = add nsw i64 %116, %113
  store i64 %117, ptr %115, align 8, !tbaa !21
  br label %6

118:                                              ; preds = %75, %38, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %119 = load i32, ptr %2, align 4
  ret i32 %119
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #3

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 }

!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, !11, i64 0}
!10 = !{!"Biobuf", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !12, i64 32, !11, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !7, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 56}
!15 = !{!7, !7, i64 0}
!16 = !{!10, !11, i64 16}
!17 = !{!10, !6, i64 48}
!18 = !{!10, !11, i64 20}
!19 = !{!10, !11, i64 40}
!20 = !{!10, !6, i64 64}
!21 = !{!10, !12, i64 32}


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

; Function Attrs: nofree nounwind uwtable
define dso_local range(i32 -1, 256) i32 @Bgetc(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %.not45 = icmp eq i32 %2, 0
  br i1 %.not45, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = getelementptr inbounds i8, ptr %0, i64 56
  %5 = getelementptr inbounds i8, ptr %0, i64 40
  %6 = getelementptr inbounds i8, ptr %0, i64 64
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  %8 = load i32, ptr %3, align 8, !tbaa !12
  switch i32 %8, label %.loopexit [
    i32 1, label %17
    i32 3, label %16
  ]

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %1
  %.lcssa = phi i32 [ %2, %1 ], [ %48, %._crit_edge.loopexit ]
  %9 = add nsw i32 %.lcssa, 1
  store i32 %9, ptr %0, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %0, i64 56
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = sext i32 %.lcssa to i64
  %13 = getelementptr inbounds i8, ptr %11, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !14
  %15 = zext i8 %14 to i32
  br label %.loopexit

16:                                               ; preds = %.lr.ph
  store i32 1, ptr %3, align 8, !tbaa !12
  br label %.loopexit

17:                                               ; preds = %.lr.ph
  %18 = getelementptr inbounds i8, ptr %0, i64 20
  %19 = getelementptr inbounds i8, ptr %0, i64 48
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %20, i64 -4
  %22 = load ptr, ptr %4, align 8, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %22, i64 -4
  %24 = load i32, ptr %23, align 1
  store i32 %24, ptr %21, align 1
  %25 = load i32, ptr %18, align 4, !tbaa !16
  %26 = load ptr, ptr %19, align 8, !tbaa !15
  %27 = load i32, ptr %5, align 8, !tbaa !17
  %28 = sext i32 %27 to i64
  %29 = tail call i64 @read(i32 noundef %25, ptr noundef %26, i64 noundef %28) #3
  %30 = trunc i64 %29 to i32
  %31 = load ptr, ptr %19, align 8, !tbaa !15
  store ptr %31, ptr %6, align 8, !tbaa !18
  %32 = icmp slt i32 %30, 1
  br i1 %32, label %33, label %34

33:                                               ; preds = %17
  %.not37 = icmp eq i32 %30, 0
  %spec.select = select i1 %.not37, i32 3, i32 0
  store i32 %spec.select, ptr %3, align 8, !tbaa !12
  br label %.loopexit

34:                                               ; preds = %17
  %35 = load i32, ptr %5, align 8, !tbaa !17
  %36 = icmp sgt i32 %35, %30
  br i1 %36, label %37, label %.._crit_edge.loopexit_crit_edge

.._crit_edge.loopexit_crit_edge:                  ; preds = %34
  %.pre = and i64 %29, 2147483647
  br label %._crit_edge.loopexit

37:                                               ; preds = %34
  %38 = load ptr, ptr %4, align 8, !tbaa !13
  %39 = and i64 %29, 2147483647
  %40 = sub nsw i64 0, %39
  %41 = getelementptr inbounds i8, ptr %38, i64 %40
  %42 = getelementptr inbounds i8, ptr %41, i64 -4
  %43 = getelementptr inbounds i8, ptr %31, i64 -4
  %44 = add nuw nsw i64 %29, 4
  %45 = and i64 %44, 4294967295
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %42, ptr nonnull align 1 %43, i64 %45, i1 false)
  %46 = load ptr, ptr %4, align 8, !tbaa !13
  %47 = getelementptr inbounds i8, ptr %46, i64 %40
  store ptr %47, ptr %6, align 8, !tbaa !18
  br label %._crit_edge.loopexit

._crit_edge.loopexit:                             ; preds = %.._crit_edge.loopexit_crit_edge, %37
  %.pre-phi = phi i64 [ %.pre, %.._crit_edge.loopexit_crit_edge ], [ %39, %37 ]
  %48 = sub nsw i32 0, %30
  %49 = load i64, ptr %7, align 8, !tbaa !19
  %50 = add nsw i64 %49, %.pre-phi
  store i64 %50, ptr %7, align 8, !tbaa !19
  br label %._crit_edge

.loopexit:                                        ; preds = %.lr.ph, %16, %33, %._crit_edge
  %.0 = phi i32 [ %15, %._crit_edge ], [ -1, %33 ], [ -1, %16 ], [ -1, %.lr.ph ]
  ret i32 %.0
}

; 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) #1

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #2

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


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