; 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
@inp_buf_rgn = external hidden global %struct.rgn_info, align 8

; Function Attrs: nounwind uwtable
define dso_local void @memlog_init(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %7 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %7, ptr @selected_mode, align 4, !tbaa !9
  %8 = load i32, ptr @selected_mode, align 4, !tbaa !9
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %3
  br label %39

11:                                               ; preds = %3
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %12, ptr @out_buf_rgn, align 8, !tbaa !11
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = udiv i32 %13, 2
  store i32 %14, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 1), align 8, !tbaa !13
  store i32 0, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 2), align 4, !tbaa !14
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = udiv i32 %16, 2
  %18 = zext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %15, i64 %18
  store ptr %19, ptr @inp_buf_rgn, align 8, !tbaa !11
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = udiv i32 %20, 2
  store i32 %21, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 1), align 8, !tbaa !13
  store i32 0, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !14
  %22 = load i32, ptr @selected_mode, align 4, !tbaa !9
  %23 = and i32 %22, 1
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %27, label %25

25:                                               ; preds = %11
  %26 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %26, ptr getelementptr inbounds (%struct.rgn_info, ptr @out_buf_rgn, i32 0, i32 1), align 8, !tbaa !13
  br label %35

27:                                               ; preds = %11
  %28 = load i32, ptr @selected_mode, align 4, !tbaa !9
  %29 = and i32 %28, 2
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %34, label %31

31:                                               ; preds = %27
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %32, ptr @inp_buf_rgn, align 8, !tbaa !11
  %33 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %33, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 1), align 8, !tbaa !13
  br label %34

34:                                               ; preds = %31, %27
  br label %35

35:                                               ; preds = %34, %25
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = zext i32 %37 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %36, i8 0, i64 %38, i1 false)
  br label %39

39:                                               ; preds = %35, %10
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 nounwind willreturn memory(argmem: write) }

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


; 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
@inp_buf_rgn = external hidden local_unnamed_addr global %struct.rgn_info, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write, inaccessiblemem: none) uwtable
define dso_local void @memlog_init(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  store i32 %2, ptr @selected_mode, align 4, !tbaa !5
  %4 = icmp eq i32 %2, 0
  br i1 %4, label %15, label %5

5:                                                ; preds = %3
  store ptr %0, ptr @out_buf_rgn, align 8, !tbaa !9
  %6 = lshr i32 %1, 1
  store i32 %6, ptr getelementptr inbounds (i8, ptr @out_buf_rgn, i64 8), align 8, !tbaa !12
  store i32 0, ptr getelementptr inbounds (i8, ptr @out_buf_rgn, i64 12), align 4, !tbaa !13
  %7 = zext nneg i32 %6 to i64
  %8 = getelementptr inbounds i8, ptr %0, i64 %7
  store ptr %8, ptr @inp_buf_rgn, align 8, !tbaa !9
  store i32 %6, ptr getelementptr inbounds (i8, ptr @inp_buf_rgn, i64 8), align 8, !tbaa !12
  store i32 0, ptr getelementptr inbounds (i8, ptr @inp_buf_rgn, i64 12), align 4, !tbaa !13
  %9 = and i32 %2, 1
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %.sink.split, label %10

10:                                               ; preds = %5
  %11 = and i32 %2, 2
  %.not10 = icmp eq i32 %11, 0
  br i1 %.not10, label %12, label %13

12:                                               ; preds = %10
  store ptr %0, ptr @inp_buf_rgn, align 8, !tbaa !9
  br label %.sink.split

.sink.split:                                      ; preds = %5, %12
  %.sink = phi ptr [ getelementptr inbounds (i8, ptr @inp_buf_rgn, i64 8), %12 ], [ getelementptr inbounds (i8, ptr @out_buf_rgn, i64 8), %5 ]
  store i32 %1, ptr %.sink, align 8, !tbaa !12
  br label %13

13:                                               ; preds = %.sink.split, %10
  %14 = zext i32 %1 to i64
  tail call void @llvm.memset.p0.i64(ptr align 1 %0, i8 0, i64 %14, i1 false)
  br label %15

15:                                               ; preds = %3, %13
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(write, inaccessiblemem: none) 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: write) }

!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 8}
!13 = !{!10, !6, i64 12}


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