; LLVM IR from NopeForge/nope.gl :: libnopegl/src (android_handlerthread)

; 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.android_handlerthread = type { i64, %union.pthread_mutex_t, %union.pthread_cond_t, ptr, ptr }
%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 }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }

; Function Attrs: nounwind uwtable
define dso_local ptr @ngli_android_handlerthread_new() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %4 = call ptr @ngli_calloc(i64 noundef 1, i64 noundef 112)
  store ptr %4, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = icmp ne ptr %5, null
  br i1 %6, label %8, label %7

7:                                                ; preds = %0
  store ptr null, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %50

8:                                                ; preds = %0
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.android_handlerthread, ptr %9, i32 0, i32 1
  %11 = call i32 @pthread_mutex_init(ptr noundef %10, ptr noundef null) #4
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.android_handlerthread, ptr %12, i32 0, i32 2
  %14 = call i32 @pthread_cond_init(ptr noundef %13, ptr noundef null) #4
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.android_handlerthread, ptr %15, i32 0, i32 1
  %17 = call i32 @pthread_mutex_lock(ptr noundef %16) #4
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.android_handlerthread, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = call i32 @pthread_create(ptr noundef %19, ptr noundef null, ptr noundef @run, ptr noundef %20) #4
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %34

23:                                               ; preds = %8
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.android_handlerthread, ptr %24, i32 0, i32 1
  %26 = call i32 @pthread_mutex_unlock(ptr noundef %25) #4
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.android_handlerthread, ptr %27, i32 0, i32 1
  %29 = call i32 @pthread_mutex_destroy(ptr noundef %28) #4
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.android_handlerthread, ptr %30, i32 0, i32 2
  %32 = call i32 @pthread_cond_destroy(ptr noundef %31) #4
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  call void @ngli_free(ptr noundef %33)
  store ptr null, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %50

34:                                               ; preds = %8
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.android_handlerthread, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.android_handlerthread, ptr %37, i32 0, i32 1
  %39 = call i32 @pthread_cond_wait(ptr noundef %36, ptr noundef %38)
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.android_handlerthread, ptr %40, i32 0, i32 1
  %42 = call i32 @pthread_mutex_unlock(ptr noundef %41) #4
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.android_handlerthread, ptr %43, i32 0, i32 4
  %45 = load ptr, ptr %44, align 8, !tbaa !9
  %46 = icmp ne ptr %45, null
  br i1 %46, label %48, label %47

47:                                               ; preds = %34
  call void @ngli_android_handlerthread_free(ptr noundef %2)
  store ptr null, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %50

48:                                               ; preds = %34
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %49, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %50

50:                                               ; preds = %48, %47, %23, %7
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  %51 = load ptr, ptr %1, align 8
  ret ptr %51
}

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

declare ptr @ngli_calloc(i64 noundef, i64 noundef) #2

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

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

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

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

; Function Attrs: nounwind uwtable
declare hidden ptr @run(ptr noundef) #0

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

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

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

declare void @ngli_free(ptr noundef) #2

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @ngli_android_handlerthread_free(ptr noundef) #0

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

!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, !6, i64 104}
!10 = !{!"android_handlerthread", !11, i64 0, !7, i64 8, !7, i64 48, !6, i64 96, !6, i64 104}
!11 = !{!"long", !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"

; Function Attrs: nounwind uwtable
define dso_local ptr @ngli_android_handlerthread_new() local_unnamed_addr #0 {
  %1 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #4
  %2 = tail call ptr @ngli_calloc(i64 noundef 1, i64 noundef 112) #4
  store ptr %2, ptr %1, align 8, !tbaa !5
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %20, label %3

3:                                                ; preds = %0
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  %5 = tail call i32 @pthread_mutex_init(ptr noundef nonnull %4, ptr noundef null) #4
  %6 = getelementptr inbounds i8, ptr %2, i64 48
  %7 = tail call i32 @pthread_cond_init(ptr noundef nonnull %6, ptr noundef null) #4
  %8 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %4) #4
  %9 = tail call i32 @pthread_create(ptr noundef nonnull %2, ptr noundef null, ptr noundef nonnull @run, ptr noundef nonnull %2) #4
  %.not2 = icmp eq i32 %9, 0
  br i1 %.not2, label %14, label %10

10:                                               ; preds = %3
  %11 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %4) #4
  %12 = tail call i32 @pthread_mutex_destroy(ptr noundef nonnull %4) #4
  %13 = tail call i32 @pthread_cond_destroy(ptr noundef nonnull %6) #4
  tail call void @ngli_free(ptr noundef nonnull %2) #4
  br label %20

14:                                               ; preds = %3
  %15 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %6, ptr noundef nonnull %4) #4
  %16 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %4) #4
  %17 = getelementptr inbounds i8, ptr %2, i64 104
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %.not3 = icmp eq ptr %18, null
  br i1 %.not3, label %19, label %20

19:                                               ; preds = %14
  call void @ngli_android_handlerthread_free(ptr noundef nonnull %1)
  br label %20

20:                                               ; preds = %14, %0, %19, %10
  %.0 = phi ptr [ null, %10 ], [ null, %19 ], [ null, %0 ], [ %2, %14 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #4
  ret ptr %.0
}

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

declare ptr @ngli_calloc(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @pthread_cond_init(ptr noundef, ptr noundef) local_unnamed_addr #3

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

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden ptr @run(ptr noundef) #0

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

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

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

declare void @ngli_free(ptr noundef) local_unnamed_addr #2

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @ngli_android_handlerthread_free(ptr noundef) local_unnamed_addr #0

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

!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, !6, i64 104}
!10 = !{!"android_handlerthread", !11, i64 0, !7, i64 8, !7, i64 48, !6, i64 96, !6, i64 104}
!11 = !{!"long", !7, i64 0}


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