; LLVM IR from shixiongfei/nio4c :: nio4c_socket (nio_popen)

; 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.niosocket_s = type { i32 }

@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [3 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 uwtable
declare dso_local void @nio_destroysocket(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @nio_pipe(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @nio_popen(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca [2 x %struct.niosocket_s], align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %9 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 0
  %10 = call i32 @nio_pipe(ptr noundef %9)
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %51

13:                                               ; preds = %2
  %14 = call i32 @fork() #5
  store i32 %14, ptr %7, align 4, !tbaa !9
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %13
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %51

18:                                               ; preds = %13
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %21, label %28

21:                                               ; preds = %18
  %22 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 1
  call void @nio_destroysocket(ptr noundef %22)
  %23 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 0
  %24 = call i32 @nio_socketnonblock(ptr noundef %23, i32 noundef 1)
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %25, ptr align 4 %26, i64 4, i1 false), !tbaa.struct !11
  %27 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %27, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %51

28:                                               ; preds = %18
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = icmp eq i32 0, %29
  br i1 %30, label %31, label %48

31:                                               ; preds = %28
  %32 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 0
  call void @nio_destroysocket(ptr noundef %32)
  %33 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 1
  %34 = getelementptr inbounds %struct.niosocket_s, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 4, !tbaa !12
  %36 = call i32 @dup2(i32 noundef %35, i32 noundef 0) #5
  %37 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 1
  %38 = getelementptr inbounds %struct.niosocket_s, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !12
  %40 = call i32 @dup2(i32 noundef %39, i32 noundef 1) #5
  %41 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 1
  %42 = getelementptr inbounds %struct.niosocket_s, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 4, !tbaa !12
  %44 = call i32 @dup2(i32 noundef %43, i32 noundef 2) #5
  %45 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 1
  call void @nio_destroysocket(ptr noundef %45)
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = call i32 (ptr, ptr, ...) @execl(ptr noundef @.str.2, ptr noundef @.str.3, ptr noundef @.str.4, ptr noundef %46, ptr noundef null) #5
  call void @exit(i32 noundef 0) #6
  unreachable

48:                                               ; preds = %28
  %49 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 0
  call void @nio_destroysocket(ptr noundef %49)
  %50 = getelementptr inbounds [2 x %struct.niosocket_s], ptr %6, i64 0, i64 1
  call void @nio_destroysocket(ptr noundef %50)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %51

51:                                               ; preds = %48, %21, %17, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %52 = load i32, ptr %3, align 4
  ret i32 %52
}

; Function Attrs: nounwind
declare i32 @fork() #3

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

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 "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 = { noreturn 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 #5 = { nounwind }
attributes #6 = { noreturn 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{i64 0, i64 4, !9}
!12 = !{!13, !10, i64 0}
!13 = !{!"niosocket_s", !10, 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"

%struct.niosocket_s = type { i32 }

@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, -2147483648) i32 @nio_popen(ptr nocapture noundef writeonly %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca [2 x %struct.niosocket_s], align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  %4 = call i32 @nio_pipe(ptr noundef nonnull %3)
  %5 = icmp slt i32 %4, 0
  br i1 %5, label %23, label %6

6:                                                ; preds = %2
  %7 = call i32 @fork() #5
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %23, label %9

9:                                                ; preds = %6
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %14, label %10

10:                                               ; preds = %9
  %11 = getelementptr inbounds i8, ptr %3, i64 4
  call void @nio_destroysocket(ptr noundef nonnull %11)
  %12 = call i32 @nio_socketnonblock(ptr noundef nonnull %3, i32 noundef 1)
  %13 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %13, ptr %0, align 4, !tbaa !5
  br label %23

14:                                               ; preds = %9
  call void @nio_destroysocket(ptr noundef nonnull %3)
  %15 = getelementptr inbounds i8, ptr %3, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = call i32 @dup2(i32 noundef %16, i32 noundef 0) #5
  %18 = load i32, ptr %15, align 4, !tbaa !9
  %19 = call i32 @dup2(i32 noundef %18, i32 noundef 1) #5
  %20 = load i32, ptr %15, align 4, !tbaa !9
  %21 = call i32 @dup2(i32 noundef %20, i32 noundef 2) #5
  call void @nio_destroysocket(ptr noundef nonnull %15)
  %22 = call i32 (ptr, ptr, ...) @execl(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.4, ptr noundef %1, ptr noundef null) #5
  call void @exit(i32 noundef 0) #6
  unreachable

23:                                               ; preds = %6, %2, %10
  %.0 = phi i32 [ %7, %10 ], [ -1, %2 ], [ -1, %6 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  ret i32 %.0
}

; Function Attrs: nofree nounwind
declare i32 @fork() local_unnamed_addr #2

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

; Function Attrs: nofree nounwind
declare i32 @execl(ptr noundef, ptr noundef, ...) local_unnamed_addr #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress 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 = { nofree 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 "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 = { noreturn 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 #5 = { nounwind }
attributes #6 = { noreturn 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, !6, i64 0}
!10 = !{!"niosocket_s", !6, i64 0}


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