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

; Function Attrs: nounwind uwtable
define dso_local i32 @memlog_getc() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = load i32, ptr @selected_mode, align 4, !tbaa !5
  %4 = and i32 %3, 1
  %5 = icmp ne i32 %4, 0
  br i1 %5, label %7, label %6

6:                                                ; preds = %0
  store i32 -1, ptr %1, align 4
  br label %35

7:                                                ; preds = %0
  %8 = load ptr, ptr @inp_buf_rgn, align 8, !tbaa !9
  %9 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !12
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i8, ptr %8, i64 %10
  %12 = load volatile i8, ptr %11, align 1, !tbaa !13
  %13 = zext i8 %12 to i32
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %7
  store i32 -1, ptr %1, align 4
  br label %35

16:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  %17 = load ptr, ptr @inp_buf_rgn, align 8, !tbaa !9
  %18 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !12
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %17, i64 %19
  %21 = load volatile i8, ptr %20, align 1, !tbaa !13
  %22 = zext i8 %21 to i32
  store i32 %22, ptr %2, align 4, !tbaa !5
  %23 = load ptr, ptr @inp_buf_rgn, align 8, !tbaa !9
  %24 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !12
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %23, i64 %25
  store volatile i8 0, ptr %26, align 1, !tbaa !13
  %27 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !12
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !12
  %29 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !12
  %30 = load i32, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 1), align 8, !tbaa !14
  %31 = icmp eq i32 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %16
  store i32 0, ptr getelementptr inbounds (%struct.rgn_info, ptr @inp_buf_rgn, i32 0, i32 2), align 4, !tbaa !12
  br label %33

33:                                               ; preds = %32, %16
  %34 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %34, ptr %1, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  br label %35

35:                                               ; preds = %33, %15, %6
  %36 = load i32, ptr %1, align 4
  ret i32 %36
}

; 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

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 = { 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 = !{!"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}


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

; Function Attrs: nofree norecurse nounwind uwtable
define dso_local range(i32 -1, 256) i32 @memlog_getc() local_unnamed_addr #0 {
  %1 = load i32, ptr @selected_mode, align 4, !tbaa !5
  %2 = and i32 %1, 1
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %17, label %3

3:                                                ; preds = %0
  %4 = load ptr, ptr @inp_buf_rgn, align 8, !tbaa !9
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @inp_buf_rgn, i64 12), align 4, !tbaa !12
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds i8, ptr %4, i64 %6
  %8 = load volatile i8, ptr %7, align 1, !tbaa !13
  %9 = icmp eq i8 %8, 0
  br i1 %9, label %17, label %10

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

17:                                               ; preds = %3, %0, %10
  %.0 = phi i32 [ %12, %10 ], [ -1, %0 ], [ -1, %3 ]
  ret i32 %.0
}

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,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass