; LLVM IR from google/esh :: shell/lib/memlog (memlog)

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

@selected_mode = external hidden global i32, align 4
@out_buf_rgn = external hidden global %struct.rgn_info, align 8

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

6:                                                ; preds = %1
  %7 = load i8, ptr %2, align 1, !tbaa !5
  %8 = sext i8 %7 to i32
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %6, %1
  br label %23

11:                                               ; preds = %6
  %12 = load i8, ptr %2, align 1, !tbaa !5
  %13 = load ptr, ptr @out_buf_rgn, align 8, !tbaa !10
  %14 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 2), align 4, !tbaa !13
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i8, ptr %13, i64 %15
  store volatile i8 %12, ptr %16, align 1, !tbaa !5
  %17 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 2), align 4, !tbaa !13
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 2), align 4, !tbaa !13
  %19 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 2), align 4, !tbaa !13
  %20 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 1), align 8, !tbaa !14
  %21 = icmp eq i32 %19, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %11
  store i32 0, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 2), align 4, !tbaa !13
  br label %23

23:                                               ; preds = %22, %11, %10
  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" }

!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"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = !{!11, !12, i64 0}
!11 = !{!"rgn_info", !12, i64 0, !9, i64 8, !9, i64 12}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!11, !9, i64 12}
!14 = !{!11, !9, i64 8}


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

%struct.rgn_info = type { ptr, i32, i32 }

@selected_mode = external hidden local_unnamed_addr global i32, align 4
@out_buf_rgn = external hidden local_unnamed_addr global %struct.rgn_info, align 8

; Function Attrs: nofree norecurse nounwind uwtable
define dso_local void @memlog_putc(i8 noundef signext %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @selected_mode, align 4, !tbaa !5
  %3 = and i32 %2, 2
  %4 = icmp eq i32 %3, 0
  %5 = icmp eq i8 %0, 0
  %or.cond = or i1 %5, %4
  br i1 %or.cond, label %15, label %6

6:                                                ; preds = %1
  %7 = load ptr, ptr @out_buf_rgn, align 8, !tbaa !9
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @out_buf_rgn, i64 12), align 4, !tbaa !12
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i8, ptr %7, i64 %9
  store volatile i8 %0, ptr %10, align 1, !tbaa !13
  %11 = load i32, ptr getelementptr inbounds (i8, ptr @out_buf_rgn, i64 12), align 4, !tbaa !12
  %12 = add nsw i32 %11, 1
  %13 = load i32, ptr getelementptr inbounds (i8, ptr @out_buf_rgn, i64 8), align 8, !tbaa !14
  %14 = icmp eq i32 %12, %13
  %spec.store.select = select i1 %14, i32 0, i32 %12
  store i32 %spec.store.select, ptr getelementptr inbounds (i8, ptr @out_buf_rgn, i64 12), align 4
  br label %15

15:                                               ; preds = %6, %1
  ret void
}

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

!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, !11, i64 0}
!10 = !{!"rgn_info", !11, i64 0, !6, i64 8, !6, i64 12}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 12}
!13 = !{!7, !7, i64 0}
!14 = !{!10, !6, i64 8}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass