; LLVM IR from nelhage/reptyr :: attach (steal_cleanup_child)

; 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.steal_pty_state = type { %struct.proc_stat, i32, i32, %struct.fd_array, [4096 x i8], %union.anon.0, i32, %struct.ptrace_child, i64, i32, i32 }
%struct.proc_stat = type { i32, [17 x i8], i8, i32, i32, i32, i64 }
%struct.fd_array = type { ptr, i32, i32 }
%union.anon.0 = type { %struct.sockaddr_un }
%struct.sockaddr_un = type { i16, [108 x i8] }
%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 }
%struct.syscall_numbers = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }

@.str.31 = external hidden unnamed_addr constant [10 x i8], align 1

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

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

declare i32 @ptrace_restore_regs(ptr noundef) #1

declare i64 @ptrace_remote_syscall(ptr noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef) #1

declare ptr @ptrace_syscall_numbers(ptr noundef) #1

declare i32 @ptrace_detach_child(ptr noundef) #1

declare i32 @ptrace_wait(ptr noundef) #1

declare i32 @ptrace_memcpy_to_child(ptr noundef, i64 noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @do_dup2(ptr noundef, i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @steal_cleanup_child(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.steal_pty_state, ptr %7, i32 0, i32 7
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.steal_pty_state, ptr %9, i32 0, i32 8
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = call i32 @ptrace_memcpy_to_child(ptr noundef %8, i64 noundef %11, ptr noundef @.str.31, i64 noundef 10)
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %19

14:                                               ; preds = %1
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.steal_pty_state, ptr %15, i32 0, i32 7
  %17 = getelementptr inbounds %struct.ptrace_child, ptr %16, i32 0, i32 4
  %18 = load i32, ptr %17, align 8, !tbaa !18
  store i32 %18, ptr %2, align 4
  br label %98

19:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.steal_pty_state, ptr %20, i32 0, i32 7
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.steal_pty_state, ptr %22, i32 0, i32 7
  %24 = call ptr @ptrace_syscall_numbers(ptr noundef %23)
  %25 = getelementptr inbounds %struct.syscall_numbers, ptr %24, i32 0, i32 11
  %26 = load i64, ptr %25, align 8, !tbaa !19
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.steal_pty_state, ptr %27, i32 0, i32 8
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = call i64 @ptrace_remote_syscall(ptr noundef %21, i64 noundef %26, i64 noundef -1, i64 noundef %29, i64 noundef 2, i64 noundef 0, i64 noundef 0, i64 noundef 0)
  %31 = trunc i64 %30 to i32
  store i32 %31, ptr %4, align 4, !tbaa !21
  %32 = load i32, ptr %4, align 4, !tbaa !21
  %33 = icmp slt i32 %32, 0
  br i1 %33, label %34, label %39

34:                                               ; preds = %19
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.steal_pty_state, ptr %35, i32 0, i32 7
  %37 = getelementptr inbounds %struct.ptrace_child, ptr %36, i32 0, i32 4
  %38 = load i32, ptr %37, align 8, !tbaa !18
  store i32 %38, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %97

39:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !21
  br label %40

40:                                               ; preds = %60, %39
  %41 = load i32, ptr %6, align 4, !tbaa !21
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.steal_pty_state, ptr %42, i32 0, i32 3
  %44 = getelementptr inbounds %struct.fd_array, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 8, !tbaa !22
  %46 = icmp slt i32 %41, %45
  br i1 %46, label %47, label %63

47:                                               ; preds = %40
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.steal_pty_state, ptr %48, i32 0, i32 7
  %50 = load i32, ptr %4, align 4, !tbaa !21
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.steal_pty_state, ptr %51, i32 0, i32 3
  %53 = getelementptr inbounds %struct.fd_array, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !23
  %55 = load i32, ptr %6, align 4, !tbaa !21
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %54, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !21
  %59 = call i32 @do_dup2(ptr noundef %49, i32 noundef %50, i32 noundef %58)
  br label %60

60:                                               ; preds = %47
  %61 = load i32, ptr %6, align 4, !tbaa !21
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %6, align 4, !tbaa !21
  br label %40, !llvm.loop !24

63:                                               ; preds = %40
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.steal_pty_state, ptr %64, i32 0, i32 7
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.steal_pty_state, ptr %66, i32 0, i32 7
  %68 = call ptr @ptrace_syscall_numbers(ptr noundef %67)
  %69 = getelementptr inbounds %struct.syscall_numbers, ptr %68, i32 0, i32 12
  %70 = load i64, ptr %69, align 8, !tbaa !26
  %71 = load i32, ptr %4, align 4, !tbaa !21
  %72 = sext i32 %71 to i64
  %73 = call i64 @ptrace_remote_syscall(ptr noundef %65, i64 noundef %70, i64 noundef %72, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0)
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.steal_pty_state, ptr %74, i32 0, i32 7
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.steal_pty_state, ptr %76, i32 0, i32 7
  %78 = call ptr @ptrace_syscall_numbers(ptr noundef %77)
  %79 = getelementptr inbounds %struct.syscall_numbers, ptr %78, i32 0, i32 12
  %80 = load i64, ptr %79, align 8, !tbaa !26
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.steal_pty_state, ptr %81, i32 0, i32 9
  %83 = load i32, ptr %82, align 8, !tbaa !27
  %84 = sext i32 %83 to i64
  %85 = call i64 @ptrace_remote_syscall(ptr noundef %75, i64 noundef %80, i64 noundef %84, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0)
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.steal_pty_state, ptr %86, i32 0, i32 9
  store i32 0, ptr %87, align 8, !tbaa !27
  %88 = load ptr, ptr %3, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.steal_pty_state, ptr %88, i32 0, i32 7
  %90 = call i32 @ptrace_restore_regs(ptr noundef %89)
  %91 = load ptr, ptr %3, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.steal_pty_state, ptr %91, i32 0, i32 7
  %93 = call i32 @ptrace_detach_child(ptr noundef %92)
  %94 = load ptr, ptr %3, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.steal_pty_state, ptr %94, i32 0, i32 7
  %96 = call i32 @ptrace_wait(ptr noundef %95)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %97

97:                                               ; preds = %63, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %98

98:                                               ; preds = %97, %14
  %99 = load i32, ptr %2, align 4
  ret i32 %99
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { 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, !13, i64 4544}
!10 = !{!"steal_pty_state", !11, i64 0, !12, i64 48, !12, i64 52, !14, i64 56, !7, i64 72, !7, i64 4168, !12, i64 4280, !15, i64 4288, !13, i64 4544, !12, i64 4552, !12, i64 4556}
!11 = !{!"proc_stat", !12, i64 0, !7, i64 4, !7, i64 21, !12, i64 24, !12, i64 28, !12, i64 32, !13, i64 40}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"fd_array", !6, i64 0, !12, i64 8, !12, i64 12}
!15 = !{!"ptrace_child", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !13, i64 24, !13, i64 32, !16, i64 40}
!16 = !{!"user_regs_struct", !17, i64 0, !17, i64 8, !17, i64 16, !17, i64 24, !17, i64 32, !17, i64 40, !17, i64 48, !17, i64 56, !17, i64 64, !17, i64 72, !17, i64 80, !17, i64 88, !17, i64 96, !17, i64 104, !17, i64 112, !17, i64 120, !17, i64 128, !17, i64 136, !17, i64 144, !17, i64 152, !17, i64 160, !17, i64 168, !17, i64 176, !17, i64 184, !17, i64 192, !17, i64 200, !17, i64 208}
!17 = !{!"long long", !7, i64 0}
!18 = !{!10, !12, i64 4304}
!19 = !{!20, !13, i64 88}
!20 = !{!"syscall_numbers", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !13, i64 72, !13, i64 80, !13, i64 88, !13, i64 96, !13, i64 104, !13, i64 112, !13, i64 120, !13, i64 128, !13, i64 136, !13, i64 144, !13, i64 152}
!21 = !{!12, !12, i64 0}
!22 = !{!10, !12, i64 64}
!23 = !{!10, !6, i64 56}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!20, !13, i64 96}
!27 = !{!10, !12, i64 4552}


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

@.str.31 = external hidden unnamed_addr constant [10 x i8], align 1

declare i32 @ptrace_restore_regs(ptr noundef) local_unnamed_addr #0

declare i64 @ptrace_remote_syscall(ptr noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

declare ptr @ptrace_syscall_numbers(ptr noundef) local_unnamed_addr #0

declare i32 @ptrace_detach_child(ptr noundef) local_unnamed_addr #0

declare i32 @ptrace_wait(ptr noundef) local_unnamed_addr #0

declare i32 @ptrace_memcpy_to_child(ptr noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @steal_cleanup_child(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 4288
  %3 = getelementptr inbounds i8, ptr %0, i64 4544
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = tail call i32 @ptrace_memcpy_to_child(ptr noundef nonnull %2, i64 noundef %4, ptr noundef nonnull @.str.31, i64 noundef 10) #2
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %9, label %6

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 4304
  %8 = load i32, ptr %7, align 8, !tbaa !17
  br label %47

9:                                                ; preds = %1
  %10 = tail call ptr @ptrace_syscall_numbers(ptr noundef nonnull %2) #2
  %11 = getelementptr inbounds i8, ptr %10, i64 88
  %12 = load i64, ptr %11, align 8, !tbaa !18
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = tail call i64 @ptrace_remote_syscall(ptr noundef nonnull %2, i64 noundef %12, i64 noundef -1, i64 noundef %13, i64 noundef 2, i64 noundef 0, i64 noundef 0, i64 noundef 0) #2
  %15 = trunc i64 %14 to i32
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %21, label %.preheader

.preheader:                                       ; preds = %9
  %17 = getelementptr inbounds i8, ptr %0, i64 64
  %18 = load i32, ptr %17, align 8, !tbaa !20
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %20 = getelementptr inbounds i8, ptr %0, i64 56
  br label %24

21:                                               ; preds = %9
  %22 = getelementptr inbounds i8, ptr %0, i64 4304
  %23 = load i32, ptr %22, align 8, !tbaa !17
  br label %47

24:                                               ; preds = %.lr.ph, %24
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %24 ]
  %25 = load ptr, ptr %20, align 8, !tbaa !21
  %26 = getelementptr inbounds i32, ptr %25, i64 %indvars.iv
  %27 = load i32, ptr %26, align 4, !tbaa !22
  %28 = tail call i32 @do_dup2(ptr noundef nonnull %2, i32 noundef %15, i32 noundef %27)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %29 = load i32, ptr %17, align 8, !tbaa !20
  %30 = sext i32 %29 to i64
  %31 = icmp slt i64 %indvars.iv.next, %30
  br i1 %31, label %24, label %._crit_edge, !llvm.loop !23

._crit_edge:                                      ; preds = %24, %.preheader
  %32 = tail call ptr @ptrace_syscall_numbers(ptr noundef nonnull %2) #2
  %33 = getelementptr inbounds i8, ptr %32, i64 96
  %34 = load i64, ptr %33, align 8, !tbaa !25
  %35 = and i64 %14, 2147483647
  %36 = tail call i64 @ptrace_remote_syscall(ptr noundef nonnull %2, i64 noundef %34, i64 noundef %35, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0) #2
  %37 = tail call ptr @ptrace_syscall_numbers(ptr noundef nonnull %2) #2
  %38 = getelementptr inbounds i8, ptr %37, i64 96
  %39 = load i64, ptr %38, align 8, !tbaa !25
  %40 = getelementptr inbounds i8, ptr %0, i64 4552
  %41 = load i32, ptr %40, align 8, !tbaa !26
  %42 = sext i32 %41 to i64
  %43 = tail call i64 @ptrace_remote_syscall(ptr noundef nonnull %2, i64 noundef %39, i64 noundef %42, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0, i64 noundef 0) #2
  store i32 0, ptr %40, align 8, !tbaa !26
  %44 = tail call i32 @ptrace_restore_regs(ptr noundef nonnull %2) #2
  %45 = tail call i32 @ptrace_detach_child(ptr noundef nonnull %2) #2
  %46 = tail call i32 @ptrace_wait(ptr noundef nonnull %2) #2
  br label %47

47:                                               ; preds = %21, %._crit_edge, %6
  %.1 = phi i32 [ %8, %6 ], [ %23, %21 ], [ 0, %._crit_edge ]
  ret i32 %.1
}

attributes #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 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 }

!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, !11, i64 4544}
!6 = !{!"steal_pty_state", !7, i64 0, !8, i64 48, !8, i64 52, !12, i64 56, !9, i64 72, !9, i64 4168, !8, i64 4280, !14, i64 4288, !11, i64 4544, !8, i64 4552, !8, i64 4556}
!7 = !{!"proc_stat", !8, i64 0, !9, i64 4, !9, i64 21, !8, i64 24, !8, i64 28, !8, i64 32, !11, i64 40}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"fd_array", !13, i64 0, !8, i64 8, !8, i64 12}
!13 = !{!"any pointer", !9, i64 0}
!14 = !{!"ptrace_child", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12, !8, i64 16, !11, i64 24, !11, i64 32, !15, i64 40}
!15 = !{!"user_regs_struct", !16, i64 0, !16, i64 8, !16, i64 16, !16, i64 24, !16, i64 32, !16, i64 40, !16, i64 48, !16, i64 56, !16, i64 64, !16, i64 72, !16, i64 80, !16, i64 88, !16, i64 96, !16, i64 104, !16, i64 112, !16, i64 120, !16, i64 128, !16, i64 136, !16, i64 144, !16, i64 152, !16, i64 160, !16, i64 168, !16, i64 176, !16, i64 184, !16, i64 192, !16, i64 200, !16, i64 208}
!16 = !{!"long long", !9, i64 0}
!17 = !{!6, !8, i64 4304}
!18 = !{!19, !11, i64 88}
!19 = !{!"syscall_numbers", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !11, i64 80, !11, i64 88, !11, i64 96, !11, i64 104, !11, i64 112, !11, i64 120, !11, i64 128, !11, i64 136, !11, i64 144, !11, i64 152}
!20 = !{!6, !8, i64 64}
!21 = !{!6, !13, i64 56}
!22 = !{!8, !8, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!19, !11, i64 96}
!26 = !{!6, !8, i64 4552}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass