#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SHUT_RD;
int /*<<< orphan*/ SHUT_WR;
scalar_t__ S_ISSOCK(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ fstat(int, struct stat *);
scalar_t__ read(int, char *, int);
scalar_t__ set_block(int);
scalar_t__ set_nonblock(int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
scalar_t__ write(int, char *, scalar_t__);
int transpose_data(int iInput, int oOutput) {
  if (iInput < 0 || oOutput < 0)
    return 1;
  static char input_data[1024];
  ssize_t read_size = 0, write_size = 0;
  while (1)
    if ((read_size = read(iInput, input_data, 1024)) > 0 &&
        read_size != (ssize_t)-1) {
      if (set_nonblock(iInput) < 0)
        break;
      while ((write_size = write(oOutput, input_data, read_size)) ==
             (ssize_t)-1)
        if (errno != EINTR)
          break;
      if (write_size == (ssize_t)-1)
        break;
    } else {
      if (read_size == 0)
        break;
      if (set_block(iInput) < 0)
        break;
    }
  struct stat current_state;
  if (fstat(iInput, &current_state) >= 0 && S_ISSOCK(current_state.st_mode))
    shutdown(iInput, SHUT_RD);
  if (fstat(oOutput, &current_state) >= 0 && S_ISSOCK(current_state.st_mode))
    shutdown(oOutput, SHUT_WR);
  return 0;
}


x86 assembly (clang, -O0):
<transpose_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1192 <transpose_data+0x22>
cmpl   $0x0,-0xc(%rbp)
jge    119e <transpose_data+0x2e>
movl   $0x1,-0x4(%rbp)
jmp    12fb <transpose_data+0x18b>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    0x2eb8(%rip),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jle    1251 <transpose_data+0xe1>
cmpq   $0xffffffffffffffff,-0x18(%rbp)
je     1251 <transpose_data+0xe1>
mov    -0x8(%rbp),%edi
call   1070 <set_nonblock@plt>
cmp    $0x0,%rax
jge    11f2 <transpose_data+0x82>
jmp    1282 <transpose_data+0x112>
jmp    11f7 <transpose_data+0x87>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    0x2e6b(%rip),%rsi
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    123c <transpose_data+0xcc>
mov    0x2db1(%rip),%rax
mov    (%rax),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
je     1237 <transpose_data+0xc7>
jmp    123c <transpose_data+0xcc>
jmp    11f7 <transpose_data+0x87>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    124c <transpose_data+0xdc>
jmp    1282 <transpose_data+0x112>
jmp    127d <transpose_data+0x10d>
cmpq   $0x0,-0x18(%rbp)
jne    1261 <transpose_data+0xf1>
jmp    1282 <transpose_data+0x112>
mov    -0x8(%rbp),%edi
call   1080 <set_block@plt>
cmp    $0x0,%rax
jge    1278 <transpose_data+0x108>
jmp    1282 <transpose_data+0x112>
jmp    127d <transpose_data+0x10d>
jmp    11ae <transpose_data+0x3e>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jl     12bb <transpose_data+0x14b>
mov    -0x28(%rbp),%edi
call   1050 <S_ISSOCK@plt>
cmp    $0x0,%rax
je     12bb <transpose_data+0x14b>
mov    -0x8(%rbp),%edi
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jl     12f4 <transpose_data+0x184>
mov    -0x28(%rbp),%edi
call   1050 <S_ISSOCK@plt>
cmp    $0x0,%rax
je     12f4 <transpose_data+0x184>
mov    -0xc(%rbp),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transpose_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%eax
or     %edi,%eax
js     125d <transpose_data+0xed>
mov    %esi,%ebx
mov    %edi,%r14d
lea    0x2edf(%rip),%r15
mov    0x2e38(%rip),%r12
mov    0x2e29(%rip),%r13
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    11f0 <transpose_data+0x80>
mov    %rax,%rbp
mov    %r14d,%edi
call   1070 <set_nonblock@plt>
test   %rax,%rax
js     11ff <transpose_data+0x8f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    119f <transpose_data+0x2f>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11d0 <transpose_data+0x60>
jmp    11ff <transpose_data+0x8f>
nop
je     11ff <transpose_data+0x8f>
mov    %r14d,%edi
call   1080 <set_block@plt>
test   %rax,%rax
jns    119f <transpose_data+0x2f>
mov    %rsp,%rsi
mov    %r14d,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     122d <transpose_data+0xbd>
mov    (%rsp),%edi
call   1050 <S_ISSOCK@plt>
test   %rax,%rax
je     122d <transpose_data+0xbd>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <shutdown@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     1259 <transpose_data+0xe9>
mov    (%rsp),%edi
call   1050 <S_ISSOCK@plt>
test   %rax,%rax
je     1259 <transpose_data+0xe9>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
xor    %eax,%eax
jmp    1262 <transpose_data+0xf2>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@transpose_data.input_data = internal global [1024 x i8] zeroinitializer
@errno = global i64 0
@EINTR = global i64 0
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
define i32 @transpose_data(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.stat
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %2
store i32 1, i32* %3
br label %83
15: ; preds = %11
store i64 0, i64* %6
store i64 0, i64* %7
br label %16
16: ; preds = %15, %55
%17 = load i32, i32* %4
%18 = call i64 @read(i32 %17, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @transpose_data.input_data, i64 0, i64 0), i32 1024)
store i64 %18, i64* %6
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %45
20: ; preds = %16
%21 = load i64, i64* %6
%22 = icmp ne i64 %21, -1
br i1 %22, label %23, label %45
23: ; preds = %20
%24 = load i32, i32* %4
%25 = call i64 @set_nonblock(i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %56
28: ; preds = %23
br label %29
29: ; preds = %39, %28
%30 = load i32, i32* %5
%31 = load i64, i64* %6
%32 = call i64 @write(i32 %30, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @transpose_data.input_data, i64 0, i64 0), i64 %31)
store i64 %32, i64* %7
%33 = icmp eq i64 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
br label %40
39: ; preds = %34
br label %29
40: ; preds = %38, %29
%41 = load i64, i64* %7
%42 = icmp eq i64 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %40
br label %56
44: ; preds = %40
br label %55
45: ; preds = %20, %16
%46 = load i64, i64* %6
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
br label %56
49: ; preds = %45
%50 = load i32, i32* %4
%51 = call i64 @set_block(i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
br label %56
54: ; preds = %49
br label %55
55: ; preds = %54, %44
br label %16
56: ; preds = %53, %48, %43, %27
%57 = load i32, i32* %4
%58 = call i64 @fstat(i32 %57, %struct.stat* %8)
%59 = icmp sge i64 %58, 0
br i1 %59, label %60, label %69
60: ; preds = %56
%61 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i64 @S_ISSOCK(i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %60
%66 = load i32, i32* %4
%67 = load i32, i32* @SHUT_RD
%68 = call i32 @shutdown(i32 %66, i32 %67)
br label %69
69: ; preds = %65, %60, %56
%70 = load i32, i32* %5
%71 = call i64 @fstat(i32 %70, %struct.stat* %8)
%72 = icmp sge i64 %71, 0
br i1 %72, label %73, label %82
73: ; preds = %69
%74 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i64 @S_ISSOCK(i32 %75)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %73
%79 = load i32, i32* %5
%80 = load i32, i32* @SHUT_WR
%81 = call i32 @shutdown(i32 %79, i32 %80)
br label %82
82: ; preds = %78, %73, %69
store i32 0, i32* %3
br label %83
83: ; preds = %82, %14
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@transpose_data.input_data = internal global [1024 x i8] zeroinitializer
@errno = global i64 0
@EINTR = global i64 0
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
define i32 @transpose_data(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = or i32 %1, %0
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %49
6: ; preds = %2, %24
%7 = tail call i64 @read(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @transpose_data.input_data, i64 0, i64 0), i32 1024)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = tail call i64 @set_nonblock(i32 %0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %9, %15
%13 = tail call i64 @write(i32 %1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @transpose_data.input_data, i64 0, i64 0), i64 %7)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %12, label %25
19: ; preds = %6
%20 = icmp eq i64 %7, 0
br i1 %20, label %25, label %21
21: ; preds = %19
%22 = tail call i64 @set_block(i32 %0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %12, %21
br label %6
25: ; preds = %21, %19, %9, %15
%26 = bitcast %struct.stat* %3 to i8*
%27 = call i64 @fstat(i32 %0, %struct.stat* %3)
%28 = icmp sgt i64 %27, -1
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i64 @S_ISSOCK(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32, i32* @SHUT_RD
%36 = tail call i32 @shutdown(i32 %0, i32 %35)
br label %37
37: ; preds = %34, %29, %25
%38 = call i64 @fstat(i32 %1, %struct.stat* %3)
%39 = icmp sgt i64 %38, -1
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = tail call i64 @S_ISSOCK(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %40
%46 = load i32, i32* @SHUT_WR
%47 = tail call i32 @shutdown(i32 %1, i32 %46)
br label %48
48: ; preds = %45, %40, %37
br label %49
49: ; preds = %2, %48
%50 = phi i32 [ 0, %48 ], [ 1, %2 ]
ret i32 %50
}