; LLVM IR from nelhage/reptyr :: platform/linux (linux_ptrace)

; 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.ptrace_child = type { i32, i32, i32, i32, i32, i64, i64, %struct.user_regs_struct }
%struct.user_regs_struct = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }

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

; Function Attrs: nounwind uwtable
declare hidden i64 @__ptrace_command(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind
declare i32 @kill(i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @ptrace_wait(ptr noundef) #1

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

; Function Attrs: nounwind uwtable
define dso_local i32 @ptrace_advance_to_state(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  br label %8

8:                                                ; preds = %66, %2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.ptrace_child, ptr %9, i32 0, i32 1
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp ne i32 %11, %12
  br i1 %13, label %14, label %67

14:                                               ; preds = %8
  %15 = load i32, ptr %5, align 4, !tbaa !9
  switch i32 %15, label %53 [
    i32 2, label %16
    i32 1, label %16
    i32 3, label %36
    i32 4, label %40
  ]

16:                                               ; preds = %14, %14
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ptrace_child, ptr %17, i32 0, i32 3
  %19 = load i32, ptr %18, align 4, !tbaa !16
  %20 = and i32 %19, 255
  %21 = icmp eq i32 %20, 127
  br i1 %21, label %22, label %32

22:                                               ; preds = %16
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.ptrace_child, ptr %23, i32 0, i32 3
  %25 = load i32, ptr %24, align 4, !tbaa !16
  %26 = and i32 %25, 65280
  %27 = ashr i32 %26, 8
  %28 = icmp eq i32 %27, 11
  br i1 %28, label %29, label %32

29:                                               ; preds = %22
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.ptrace_child, ptr %30, i32 0, i32 4
  store i32 11, ptr %31, align 8, !tbaa !17
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %68

32:                                               ; preds = %22, %16
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = call i64 @__ptrace_command(ptr noundef %33, i32 noundef 24, ptr noundef null, ptr noundef null)
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %6, align 4, !tbaa !9
  br label %56

36:                                               ; preds = %14
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = call i64 @__ptrace_command(ptr noundef %37, i32 noundef 7, ptr noundef null, ptr noundef null)
  %39 = trunc i64 %38 to i32
  store i32 %39, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %68

40:                                               ; preds = %14
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.ptrace_child, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !18
  %44 = call i32 @kill(i32 noundef %43, i32 noundef 19) #4
  store i32 %44, ptr %6, align 4, !tbaa !9
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = icmp slt i32 %45, 0
  br i1 %46, label %47, label %52

47:                                               ; preds = %40
  %48 = call ptr @__errno_location() #5
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ptrace_child, ptr %50, i32 0, i32 4
  store i32 %49, ptr %51, align 8, !tbaa !17
  br label %52

52:                                               ; preds = %47, %40
  br label %56

53:                                               ; preds = %14
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.ptrace_child, ptr %54, i32 0, i32 4
  store i32 22, ptr %55, align 8, !tbaa !17
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %68

56:                                               ; preds = %52, %32
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = icmp slt i32 %57, 0
  br i1 %58, label %59, label %61

59:                                               ; preds = %56
  %60 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %60, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %68

61:                                               ; preds = %56
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = call i32 @ptrace_wait(ptr noundef %62)
  %64 = icmp slt i32 %63, 0
  br i1 %64, label %65, label %66

65:                                               ; preds = %61
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %68

66:                                               ; preds = %61
  br label %8, !llvm.loop !19

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

68:                                               ; preds = %67, %65, %59, %53, %36, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %69 = load i32, ptr %3, align 4
  ret i32 %69
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 willreturn memory(none) "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 willreturn memory(none) }

!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, !10, i64 4}
!12 = !{!"ptrace_child", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !13, i64 24, !13, i64 32, !14, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = !{!"user_regs_struct", !15, i64 0, !15, i64 8, !15, i64 16, !15, i64 24, !15, i64 32, !15, i64 40, !15, i64 48, !15, i64 56, !15, i64 64, !15, i64 72, !15, i64 80, !15, i64 88, !15, i64 96, !15, i64 104, !15, i64 112, !15, i64 120, !15, i64 128, !15, i64 136, !15, i64 144, !15, i64 152, !15, i64 160, !15, i64 168, !15, i64 176, !15, i64 184, !15, i64 192, !15, i64 200, !15, i64 208}
!15 = !{!"long long", !7, i64 0}
!16 = !{!12, !10, i64 12}
!17 = !{!12, !10, i64 16}
!18 = !{!12, !10, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; 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
declare hidden i64 @__ptrace_command(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @kill(i32 noundef, i32 noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @ptrace_advance_to_state(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 4
  %4 = getelementptr inbounds i8, ptr %0, i64 12
  switch i32 %1, label %.split [
    i32 2, label %.split.us.preheader
    i32 1, label %.split.us.preheader
    i32 3, label %.split.us24
    i32 4, label %.split.us31
  ]

.split.us.preheader:                              ; preds = %2, %2
  br label %.split.us

.split.us:                                        ; preds = %.split.us.preheader, %.thread20.us
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %.not.us = icmp eq i32 %5, %1
  br i1 %.not.us, label %.loopexit, label %6

6:                                                ; preds = %.split.us
  %7 = load i32, ptr %4, align 4, !tbaa !13
  %8 = and i32 %7, 65535
  %or.cond.us = icmp eq i32 %8, 2943
  br i1 %or.cond.us, label %.split23.us, label %9

9:                                                ; preds = %6
  %10 = tail call i64 @__ptrace_command(ptr noundef nonnull %0, i32 noundef 24, ptr noundef null, ptr noundef null)
  %11 = trunc i64 %10 to i32
  %12 = icmp slt i32 %11, 0
  br i1 %12, label %.loopexit, label %.thread20.us

.thread20.us:                                     ; preds = %9
  %13 = tail call i32 @ptrace_wait(ptr noundef nonnull %0)
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %.loopexit, label %.split.us, !llvm.loop !14

.split.us24:                                      ; preds = %2
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %.not.us25 = icmp eq i32 %15, 3
  br i1 %.not.us25, label %.loopexit, label %.split30.us

.split.us31:                                      ; preds = %2, %.thread20.us33
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %.not.us32 = icmp eq i32 %16, 4
  br i1 %.not.us32, label %.loopexit, label %17

17:                                               ; preds = %.split.us31
  %18 = load i32, ptr %0, align 8, !tbaa !16
  %19 = tail call i32 @kill(i32 noundef %18, i32 noundef 19) #3
  %20 = icmp slt i32 %19, 0
  br i1 %20, label %.thread, label %.thread20.us33

.thread20.us33:                                   ; preds = %17
  %21 = tail call i32 @ptrace_wait(ptr noundef nonnull %0)
  %22 = icmp slt i32 %21, 0
  br i1 %22, label %.loopexit, label %.split.us31, !llvm.loop !14

.split:                                           ; preds = %2
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %.not = icmp eq i32 %23, %1
  br i1 %.not, label %.loopexit, label %30

.split23.us:                                      ; preds = %6
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 11, ptr %24, align 8, !tbaa !17
  br label %.loopexit

.split30.us:                                      ; preds = %.split.us24
  %25 = tail call i64 @__ptrace_command(ptr noundef nonnull %0, i32 noundef 7, ptr noundef null, ptr noundef null)
  %26 = trunc i64 %25 to i32
  br label %.loopexit

.thread:                                          ; preds = %17
  %27 = tail call ptr @__errno_location() #4
  %28 = load i32, ptr %27, align 4, !tbaa !18
  %29 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 %28, ptr %29, align 8, !tbaa !17
  br label %.loopexit

30:                                               ; preds = %.split
  %31 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 22, ptr %31, align 8, !tbaa !17
  br label %.loopexit

.loopexit:                                        ; preds = %.thread20.us33, %.split.us31, %.thread20.us, %9, %.split.us, %.split, %.split.us24, %.thread, %30, %.split30.us, %.split23.us
  %.015 = phi i32 [ -1, %30 ], [ %26, %.split30.us ], [ -1, %.split23.us ], [ %19, %.thread ], [ 0, %.split.us24 ], [ 0, %.split ], [ 0, %.split.us ], [ -1, %.thread20.us ], [ %11, %9 ], [ 0, %.split.us31 ], [ -1, %.thread20.us33 ]
  ret i32 %.015
}

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 = { 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 #2 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 }
attributes #4 = { nounwind willreturn memory(none) }

!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, !7, i64 4}
!6 = !{!"ptrace_child", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !10, i64 24, !10, i64 32, !11, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"user_regs_struct", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !12, i64 112, !12, i64 120, !12, i64 128, !12, i64 136, !12, i64 144, !12, i64 152, !12, i64 160, !12, i64 168, !12, i64 176, !12, i64 184, !12, i64 192, !12, i64 200, !12, i64 208}
!12 = !{!"long long", !8, i64 0}
!13 = !{!6, !7, i64 12}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !7, i64 0}
!17 = !{!6, !7, i64 16}
!18 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass