; LLVM IR from yinqiwen/ardb :: src/common/channel/redis (zmalloc)

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

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@used_memory_mutex = external dso_local global %union.pthread_mutex_t, align 8
@zmalloc_thread_safe = external hidden global i32, align 4
@used_memory = external hidden global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @zmalloc(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %5 = load i64, ptr %2, align 8, !tbaa !5
  %6 = add i64 %5, 8
  %7 = call noalias ptr @malloc(i64 noundef %6) #5
  store ptr %7, ptr %3, align 8, !tbaa !9
  %8 = load ptr, ptr %3, align 8, !tbaa !9
  %9 = icmp ne ptr %8, null
  br i1 %9, label %12, label %10

10:                                               ; preds = %1
  %11 = load i64, ptr %2, align 8, !tbaa !5
  call void @zmalloc_oom(i64 noundef %11)
  br label %12

12:                                               ; preds = %10, %1
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = load ptr, ptr %3, align 8, !tbaa !9
  store i64 %13, ptr %14, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = add i64 %16, 8
  store i64 %17, ptr %4, align 8, !tbaa !5
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = and i64 %18, 7
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %27

21:                                               ; preds = %15
  %22 = load i64, ptr %4, align 8, !tbaa !5
  %23 = and i64 %22, 7
  %24 = sub i64 8, %23
  %25 = load i64, ptr %4, align 8, !tbaa !5
  %26 = add i64 %25, %24
  store i64 %26, ptr %4, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %21, %15
  %28 = load i32, ptr @zmalloc_thread_safe, align 4, !tbaa !11
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %36

30:                                               ; preds = %27
  %31 = call i32 @pthread_mutex_lock(ptr noundef @used_memory_mutex) #4
  %32 = load i64, ptr %4, align 8, !tbaa !5
  %33 = load i64, ptr @used_memory, align 8, !tbaa !5
  %34 = add i64 %33, %32
  store i64 %34, ptr @used_memory, align 8, !tbaa !5
  %35 = call i32 @pthread_mutex_unlock(ptr noundef @used_memory_mutex) #4
  br label %40

36:                                               ; preds = %27
  %37 = load i64, ptr %4, align 8, !tbaa !5
  %38 = load i64, ptr @used_memory, align 8, !tbaa !5
  %39 = add i64 %38, %37
  store i64 %39, ptr @used_memory, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %36, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %41

41:                                               ; preds = %40
  br label %42

42:                                               ; preds = %41
  %43 = load ptr, ptr %3, align 8, !tbaa !9
  %44 = getelementptr inbounds i8, ptr %43, i64 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret ptr %44
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @zmalloc_oom(i64 noundef) #0

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #3

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr 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 = { nounwind allocsize(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 #3 = { 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 }
attributes #5 = { nounwind allocsize(0) }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"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"

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@used_memory_mutex = external dso_local global %union.pthread_mutex_t, align 8
@zmalloc_thread_safe = external hidden local_unnamed_addr global i32, align 4
@used_memory = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local noalias nonnull ptr @zmalloc(i64 noundef %0) local_unnamed_addr #0 {
  %2 = add i64 %0, 8
  %3 = tail call noalias ptr @malloc(i64 noundef %2) #3
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %4, label %5

4:                                                ; preds = %1
  tail call void @zmalloc_oom(i64 noundef %0)
  br label %5

5:                                                ; preds = %4, %1
  store i64 %0, ptr %3, align 8, !tbaa !5
  %6 = and i64 %0, 7
  %.not12 = icmp eq i64 %6, 0
  %7 = and i64 %0, -8
  %8 = add i64 %7, 16
  %.0 = select i1 %.not12, i64 %2, i64 %8
  %9 = load i32, ptr @zmalloc_thread_safe, align 4, !tbaa !9
  %.not13 = icmp eq i32 %9, 0
  br i1 %.not13, label %15, label %10

10:                                               ; preds = %5
  %11 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @used_memory_mutex) #4
  %12 = load i64, ptr @used_memory, align 8, !tbaa !5
  %13 = add i64 %12, %.0
  store i64 %13, ptr @used_memory, align 8, !tbaa !5
  %14 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @used_memory_mutex) #4
  br label %18

15:                                               ; preds = %5
  %16 = load i64, ptr @used_memory, align 8, !tbaa !5
  %17 = add i64 %16, %.0
  store i64 %17, ptr @used_memory, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %15, %10
  %19 = getelementptr inbounds i8, ptr %3, i64 8
  ret ptr %19
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @zmalloc_oom(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #2

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #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 = { nounwind allocsize(0) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


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