#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ pt2_name;
typedef int /*<<< orphan*/ pt1_name;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
scalar_t__ pts_copy(int, int);
int pts_open();
int /*<<< orphan*/ ptsname_r(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int pt1, pt2;
  char pt1_name[128], pt2_name[128];
  if ((pt1 = pts_open()) < 0)
    return -1;
  if ((pt2 = pts_open()) < 0)
    return -1;
  ptsname_r(pt1, pt1_name, sizeof(pt1_name));
  ptsname_r(pt2, pt2_name, sizeof(pt2_name));
  printf("%s <=> %s\n", pt1_name, pt2_name);
  while (1) {
    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(pt1, &rfds);
    FD_SET(pt2, &rfds);
    if (select(pt2 + 1, &rfds, NULL, NULL, NULL) < 0) {
      perror("select");
      return -1;
    }
    if (FD_ISSET(pt1, &rfds)) {
      if (pts_copy(pt1, pt2) < 0)
        break;
    }
    if (FD_ISSET(pt2, &rfds)) {
      if (pts_copy(pt2, pt1) < 0)
        break;
    }
  }
  close(pt1);
  close(pt2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <pts_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d8 <main+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <main+0x18e>
mov    $0x0,%al
call   1090 <pts_open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11f7 <main+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <main+0x18e>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <ptsname_r@plt>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x80,%edx
call   1060 <ptsname_r@plt>
lea    -0xa0(%rbp),%rsi
lea    -0x120(%rbp),%rdx
lea    0xdcc(%rip),%rdi
call   1040 <printf@plt>
lea    -0x124(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x124(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
lea    -0x124(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
lea    -0x124(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
cmp    $0x0,%rax
jge    12a2 <main+0x102>
lea    0xd7a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <main+0x18e>
mov    -0x14(%rbp),%edi
lea    -0x124(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12da <main+0x13a>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10a0 <pts_copy@plt>
cmp    $0x0,%rax
jge    12d5 <main+0x135>
jmp    1317 <main+0x177>
jmp    12da <main+0x13a>
mov    -0x18(%rbp),%edi
lea    -0x124(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1312 <main+0x172>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   10a0 <pts_copy@plt>
cmp    $0x0,%rax
jge    130d <main+0x16d>
jmp    1317 <main+0x177>
jmp    1312 <main+0x172>
jmp    1239 <main+0x99>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
xor    %eax,%eax
call   1090 <pts_open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     12f3 <main+0x153>
mov    %eax,%r12d
xor    %eax,%eax
call   1090 <pts_open@plt>
test   %eax,%eax
js     12f3 <main+0x153>
mov    %eax,%ebx
lea    0x90(%rsp),%r15
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x80,%edx
call   1060 <ptsname_r@plt>
lea    0x10(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <ptsname_r@plt>
lea    0xdf5(%rip),%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1040 <printf@plt>
mov    %ebx,%r15d
add    $0x1,%r15d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
js     1306 <main+0x166>
lea    0xc(%rsp),%rbp
jmp    12a4 <main+0x104>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
js     1306 <main+0x166>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12c3 <main+0x123>
mov    %r12d,%edi
mov    %ebx,%esi
call   10a0 <pts_copy@plt>
test   %rax,%rax
js     12e1 <main+0x141>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1270 <main+0xd0>
mov    %ebx,%edi
mov    %r12d,%esi
call   10a0 <pts_copy@plt>
test   %rax,%rax
jns    1270 <main+0xd0>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfe(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f3 <main+0x153>

LLVM IR (-O0):
@.str = [11 x i8] c"%s <=> %s\0A\00"
@.str.1 = [7 x i8] c"select\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
%9 = alloca [128 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @pts_open()
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %68
14: ; preds = %2
%15 = call i32 (...) @pts_open()
store i32 %15, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 -1, i32* %3
br label %68
18: ; preds = %14
%19 = load i32, i32* %6
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%21 = call i32 @ptsname_r(i32 %19, i8* %20, i32 128)
%22 = load i32, i32* %7
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%24 = call i32 @ptsname_r(i32 %22, i8* %23, i32 128)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%27 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26)
br label %28
28: ; preds = %18, %62
%29 = call i32 @FD_ZERO(i32* %10)
%30 = load i32, i32* %6
%31 = call i32 @FD_SET(i32 %30, i32* %10)
%32 = load i32, i32* %7
%33 = call i32 @FD_SET(i32 %32, i32* %10)
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
%36 = call i64 @select(i32 %35, i32* %10, i32* null, i32* null, i32* null)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
40: ; preds = %28
%41 = load i32, i32* %6
%42 = call i64 @FD_ISSET(i32 %41, i32* %10)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %40
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = call i64 @pts_copy(i32 %45, i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
br label %63
50: ; preds = %44
br label %51
51: ; preds = %50, %40
%52 = load i32, i32* %7
%53 = call i64 @FD_ISSET(i32 %52, i32* %10)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %51
%56 = load i32, i32* %7
%57 = load i32, i32* %6
%58 = call i64 @pts_copy(i32 %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
br label %63
61: ; preds = %55
br label %62
62: ; preds = %61, %51
br label %28
63: ; preds = %60, %49
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
store i32 0, i32* %3
br label %68
68: ; preds = %63, %38, %17, %13
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s <=> %s\0A\00"
@.str.1 = [7 x i8] c"select\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = alloca [128 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = tail call i32 (...) @pts_open()
%9 = icmp slt i32 %8, 0
br i1 %9, label %47, label %10
10: ; preds = %2
%11 = tail call i32 (...) @pts_open()
%12 = icmp slt i32 %11, 0
br i1 %12, label %47, label %13
13: ; preds = %10
%14 = call i32 @ptsname_r(i32 %8, i8* %6, i32 128)
%15 = call i32 @ptsname_r(i32 %11, i8* %7, i32 128)
%16 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %7)
%17 = bitcast i32* %5 to i8*
%18 = add nuw nsw i32 %11, 1
%19 = call i32 @FD_ZERO(i32* %5)
%20 = call i32 @FD_SET(i32 %8, i32* %5)
%21 = call i32 @FD_SET(i32 %11, i32* %5)
%22 = call i64 @select(i32 %18, i32* %5, i32* null, i32* null, i32* null)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %38, %13
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %47
26: ; preds = %13, %38
%27 = call i64 @FD_ISSET(i32 %8, i32* %5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i64 @pts_copy(i32 %8, i32 %11)
%31 = icmp slt i64 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %29, %26
%33 = call i64 @FD_ISSET(i32 %11, i32* %5)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = call i64 @pts_copy(i32 %11, i32 %8)
%37 = icmp slt i64 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %32, %35
%39 = call i32 @FD_ZERO(i32* %5)
%40 = call i32 @FD_SET(i32 %8, i32* %5)
%41 = call i32 @FD_SET(i32 %11, i32* %5)
%42 = call i64 @select(i32 %18, i32* %5, i32* null, i32* null, i32* null)
%43 = icmp slt i64 %42, 0
br i1 %43, label %24, label %26
44: ; preds = %29, %35
%45 = call i32 @close(i32 %8)
%46 = call i32 @close(i32 %11)
br label %47
47: ; preds = %24, %10, %2, %44
%48 = phi i32 [ 0, %44 ], [ -1, %2 ], [ -1, %10 ], [ -1, %24 ]
ret i32 %48
}