#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*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int read(int, char *, int);
int v_select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int loop_ptm(int ptm, int file, fd_set fds) {
  fd_set copy;
  char buf[4096];
  int ret;
  while (42) {
    copy = fds;
    if ((v_select((ptm + 1), &copy, NULL, NULL, NULL)) == -1)
      return (-1);
    if (FD_ISSET(0, &copy)) {
      if ((ret = read(0, buf, sizeof(buf))) <= 0)
        return (-1);
      (void)write(ptm, buf, ret);
    }
    if (FD_ISSET(ptm, &fds)) {
      if ((ret = read(ptm, buf, sizeof(buf))) <= 0)
        return (0);
      (void)write(1, buf, ret);
      (void)write(file, buf, ret);
    }
  }
}


x86 assembly (clang, -O0):
<loop_ptm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1060 <v_select@plt>
cmp    $0xffffffff,%eax
jne    1189 <loop_ptm+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <loop_ptm+0x117>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11e1 <loop_ptm+0xa1>
lea    -0x1020(%rbp),%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
jg     11cc <loop_ptm+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <loop_ptm+0x117>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1252 <loop_ptm+0x112>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
jg     1226 <loop_ptm+0xe6>
movl   $0x0,-0x4(%rbp)
jmp    1257 <loop_ptm+0x117>
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1030 <write@plt>
jmp    1154 <loop_ptm+0x14>
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop_ptm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    %edx,0xc(%rsp)
lea    0x1(%rbx),%r12d
mov    %edx,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <v_select@plt>
mov    $0xffffffff,%ecx
cmp    $0xffffffff,%eax
je     122f <loop_ptm+0xef>
lea    0x8(%rsp),%rbp
lea    0x10(%rsp),%r15
jmp    11af <loop_ptm+0x6f>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <v_select@plt>
cmp    $0xffffffff,%eax
je     1226 <loop_ptm+0xe6>
xor    %edi,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11dd <loop_ptm+0x9d>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1226 <loop_ptm+0xe6>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
lea    0xc(%rsp),%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1190 <loop_ptm+0x50>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    122d <loop_ptm+0xed>
mov    %eax,%r13d
mov    $0x1,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %r13d,%edx
call   1030 <write@plt>
jmp    1190 <loop_ptm+0x50>
mov    $0xffffffff,%ecx
jmp    122f <loop_ptm+0xef>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @loop_ptm(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
br label %11
11: ; preds = %3, %49
%12 = load i32, i32* %7
store i32 %12, i32* %8
%13 = load i32, i32* %5
%14 = add nsw i32 %13, 1
%15 = call i32 @v_select(i32 %14, i32* %8, i32* null, i32* null, i32* null)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 -1, i32* %4
br label %50
18: ; preds = %11
%19 = call i64 @FD_ISSET(i32 0, i32* %8)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%23 = call i32 @read(i32 0, i8* %22, i32 4096)
store i32 %23, i32* %10
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %4
br label %50
26: ; preds = %21
%27 = load i32, i32* %5
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%29 = load i32, i32* %10
%30 = call i32 @write(i32 %27, i8* %28, i32 %29)
br label %31
31: ; preds = %26, %18
%32 = load i32, i32* %5
%33 = call i64 @FD_ISSET(i32 %32, i32* %7)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %31
%36 = load i32, i32* %5
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 4096)
store i32 %38, i32* %10
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 0, i32* %4
br label %50
41: ; preds = %35
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%43 = load i32, i32* %10
%44 = call i32 @write(i32 1, i8* %42, i32 %43)
%45 = load i32, i32* %6
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%47 = load i32, i32* %10
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
br label %49
49: ; preds = %41, %31
br label %11
50: ; preds = %40, %25, %17
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
define i32 @loop_ptm(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i8]
store i32 %2, i32* %4
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%9 = add nsw i32 %0, 1
store i32 %2, i32* %5
%10 = call i32 @v_select(i32 %9, i32* %5, i32* null, i32* null, i32* null)
%11 = icmp eq i32 %10, -1
br i1 %11, label %33, label %12
12: ; preds = %3, %29
%13 = call i64 @FD_ISSET(i32 0, i32* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = call i32 @read(i32 0, i8* %8, i32 4096)
%17 = icmp slt i32 %16, 1
br i1 %17, label %33, label %18
18: ; preds = %15
%19 = call i32 @write(i32 %0, i8* %8, i32 %16)
br label %20
20: ; preds = %18, %12
%21 = call i64 @FD_ISSET(i32 %0, i32* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = call i32 @read(i32 %0, i8* %8, i32 4096)
%25 = icmp slt i32 %24, 1
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = call i32 @write(i32 1, i8* %8, i32 %24)
%28 = call i32 @write(i32 %1, i8* %8, i32 %24)
br label %29
29: ; preds = %26, %20
%30 = load i32, i32* %4
store i32 %30, i32* %5
%31 = call i32 @v_select(i32 %9, i32* %5, i32* null, i32* null, i32* null)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %12
33: ; preds = %29, %15, %23, %3
%34 = phi i32 [ -1, %3 ], [ 0, %23 ], [ -1, %15 ], [ -1, %29 ]
ret i32 %34
}