; LLVM IR from rprichard/winpty :: misc/UnixEcho.ll (Z7tty_rawi)

; 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.termios = type { i32, i32, i32, i32, i8, [32 x i8], i32, i32 }

@_ZL12save_termios = external hidden global %struct.termios, align 4
@_ZL10term_saved = external hidden global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z7tty_rawi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca %struct.termios, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 60, ptr %4) #4
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = call i32 @tcgetattr(i32 noundef %6, ptr noundef @_ZL12save_termios) #4
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %35

10:                                               ; preds = %1
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %4, ptr align 4 @_ZL12save_termios, i64 60, i1 false), !tbaa.struct !9
  %11 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 3
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = and i32 %12, -32780
  store i32 %13, ptr %11, align 4, !tbaa !11
  %14 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 0
  %15 = load i32, ptr %14, align 4, !tbaa !13
  %16 = and i32 %15, -1315
  store i32 %16, ptr %14, align 4, !tbaa !13
  %17 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 2
  %18 = load i32, ptr %17, align 4, !tbaa !14
  %19 = and i32 %18, -305
  store i32 %19, ptr %17, align 4, !tbaa !14
  %20 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !14
  %22 = or i32 %21, 48
  store i32 %22, ptr %20, align 4, !tbaa !14
  %23 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 1
  %24 = load i32, ptr %23, align 4, !tbaa !15
  %25 = and i32 %24, -2
  store i32 %25, ptr %23, align 4, !tbaa !15
  %26 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 5
  %27 = getelementptr inbounds [32 x i8], ptr %26, i64 0, i64 6
  store i8 1, ptr %27, align 1, !tbaa !10
  %28 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 5
  %29 = getelementptr inbounds [32 x i8], ptr %28, i64 0, i64 5
  store i8 0, ptr %29, align 1, !tbaa !10
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = call i32 @tcsetattr(i32 noundef %30, i32 noundef 2, ptr noundef %4) #4
  %32 = icmp slt i32 %31, 0
  br i1 %32, label %33, label %34

33:                                               ; preds = %10
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %35

34:                                               ; preds = %10
  store i32 1, ptr @_ZL10term_saved, align 4, !tbaa !5
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %35

35:                                               ; preds = %34, %33, %9
  call void @llvm.lifetime.end.p0(i64 60, ptr %4) #4
  %36 = load i32, ptr %2, align 4
  ret i32 %36
}

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

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

; 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) #3

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

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

attributes #0 = { mustprogress 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 "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 4, !5, i64 12, i64 4, !5, i64 16, i64 1, !10, i64 17, i64 32, !10, i64 52, i64 4, !5, i64 56, i64 4, !5}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !6, i64 12}
!12 = !{!"_ZTS7termios", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !7, i64 16, !7, i64 17, !6, i64 52, !6, i64 56}
!13 = !{!12, !6, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !6, i64 4}


; 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.termios = type { i32, i32, i32, i32, i8, [32 x i8], i32, i32 }

@_ZL12save_termios = external hidden global %struct.termios, align 4
@_ZL10term_saved = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef range(i32 -1, 1) i32 @_Z7tty_rawi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.termios, align 8
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %2) #4
  %3 = tail call i32 @tcgetattr(i32 noundef %0, ptr noundef nonnull @_ZL12save_termios) #4
  %4 = icmp slt i32 %3, 0
  br i1 %4, label %20, label %5

5:                                                ; preds = %1
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(60) %2, ptr noundef nonnull align 4 dereferenceable(60) @_ZL12save_termios, i64 60, i1 false), !tbaa.struct !5
  %6 = getelementptr inbounds i8, ptr %2, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = and i32 %7, -32780
  store i32 %8, ptr %6, align 4, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %2, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !13
  %11 = and i32 %10, -305
  %12 = or disjoint i32 %11, 48
  store i32 %12, ptr %9, align 8, !tbaa !13
  %13 = load <2 x i32>, ptr %2, align 8, !tbaa !6
  %14 = and <2 x i32> %13, <i32 -1315, i32 -2>
  store <2 x i32> %14, ptr %2, align 8, !tbaa !6
  %15 = getelementptr inbounds i8, ptr %2, i64 23
  store i8 1, ptr %15, align 1, !tbaa !10
  %16 = getelementptr inbounds i8, ptr %2, i64 22
  store i8 0, ptr %16, align 2, !tbaa !10
  %17 = call i32 @tcsetattr(i32 noundef %0, i32 noundef 2, ptr noundef nonnull %2) #4
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %20, label %19

19:                                               ; preds = %5
  store i32 1, ptr @_ZL10term_saved, align 4, !tbaa !6
  br label %20

20:                                               ; preds = %5, %1, %19
  %.0 = phi i32 [ 0, %19 ], [ -1, %1 ], [ -1, %5 ]
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %2) #4
  ret i32 %.0
}

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

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

; Function Attrs: mustprogress 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) #3

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

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

attributes #0 = { mustprogress 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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{i64 0, i64 4, !6, i64 4, i64 4, !6, i64 8, i64 4, !6, i64 12, i64 4, !6, i64 16, i64 1, !10, i64 17, i64 32, !10, i64 52, i64 4, !6, i64 56, i64 4, !6}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = !{!12, !7, i64 12}
!12 = !{!"_ZTS7termios", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !8, i64 16, !8, i64 17, !7, i64 52, !7, i64 56}
!13 = !{!12, !7, i64 8}


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