#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 */
typedef struct TYPE_10__ TYPE_2__;
typedef struct TYPE_9__ TYPE_1__;

/* Type definitions */
typedef scalar_t__ ssize_t;
struct TYPE_9__ {
  int /*<<< orphan*/ member_0;
};
typedef TYPE_1__ fd_set;
struct TYPE_10__ {
  int /*<<< orphan*/ member_0;
};
typedef TYPE_2__ RC4_KEY;

/* Variables and functions */
int EXIT_FAILURE;
scalar_t__ FD_ISSET(int, TYPE_1__ *);
int /*<<< orphan*/ FD_SET(int, TYPE_1__ *);
int /*<<< orphan*/ FD_ZERO(TYPE_1__ *);
int /*<<< orphan*/ RC4(TYPE_2__ *, scalar_t__, char *, char *);
int /*<<< orphan*/ RC4_set_key(TYPE_2__ *, int, char *);
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ fsync(int);
scalar_t__ read(int, char *, int);
scalar_t__ select(int, TYPE_1__ *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int copyLoop(int s, char *pKey, int nKeyLen) {
  int nRetVal = EXIT_FAILURE;
  RC4_KEY sEKey = {0};
  RC4_KEY sDKey = {0};
  char pPlain[(1024)] = {0};
  char pCipher[(1024)] = {0};
  if (pKey && nKeyLen) {
    RC4_set_key(&sEKey, nKeyLen, pKey);
    RC4_set_key(&sDKey, nKeyLen, pKey);
    while (true) {
      fd_set set = {0};
      int from = -1, to = -1;
      FD_ZERO(&set);
      FD_SET(STDIN_FILENO, &set);
      FD_SET(s, &set);
      if (0 < select(s + 1, &set, NULL, NULL, NULL)) {
        if (FD_ISSET(STDIN_FILENO, &set)) {
          from = STDIN_FILENO;
          to = s;
        } else if (FD_ISSET(s, &set)) {
          from = s;
          to = STDOUT_FILENO;
        }
      }
      if (-1 != from && -1 != to) {
        ssize_t len = 0;
        bzero(pPlain, (1024));
        bzero(pCipher, (1024));
        len = read(from, pPlain, (1024));
        if (len) {
          RC4((to == s) ? &sEKey : &sDKey, len, pPlain, pCipher);
          write(to, pCipher, len);
          fsync(to);
        } else {
          break;
        }
      }
    }
  }
  return nRetVal;
}


x86 assembly (clang, -O0):
<copyLoop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x830(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
je     144d <copyLoop+0x29d>
cmpl   $0x0,-0x14(%rbp)
je     144d <copyLoop+0x29d>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   10a0 <RC4_set_key@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   10a0 <RC4_set_key@plt>
lea    -0x838(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
movl   $0xffffffff,-0x83c(%rbp)
ff ff ff
movl   $0xffffffff,-0x840(%rbp)
ff ff ff
lea    -0x838(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    -0x838(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x838(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x838(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jge    1345 <copyLoop+0x195>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    -0x838(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     130a <copyLoop+0x15a>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x83c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x840(%rbp)
jmp    1340 <copyLoop+0x190>
mov    -0x4(%rbp),%edi
lea    -0x838(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     133b <copyLoop+0x18b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x83c(%rbp)
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x840(%rbp)
jmp    1340 <copyLoop+0x190>
jmp    1345 <copyLoop+0x195>
mov    $0xffffffff,%eax
cmp    -0x83c(%rbp),%eax
je     1443 <copyLoop+0x293>
mov    $0xffffffff,%eax
cmp    -0x840(%rbp),%eax
je     1443 <copyLoop+0x293>
movq   $0x0,-0x848(%rbp)
lea    -0x430(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
lea    -0x830(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    -0x83c(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %rax,-0x848(%rbp)
cmpq   $0x0,-0x848(%rbp)
je     1439 <copyLoop+0x289>
mov    -0x840(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    13df <copyLoop+0x22f>
lea    -0x20(%rbp),%rax
mov    %rax,-0x850(%rbp)
jmp    13ef <copyLoop+0x23f>
lea    -0x28(%rbp),%rax
mov    %rax,-0x850(%rbp)
jmp    13ef <copyLoop+0x23f>
mov    -0x850(%rbp),%rdi
mov    -0x848(%rbp),%rsi
lea    -0x430(%rbp),%rdx
lea    -0x830(%rbp),%rcx
call   1050 <RC4@plt>
mov    -0x840(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x848(%rbp),%rdx
call   1030 <write@plt>
mov    -0x840(%rbp),%edi
call   10b0 <fsync@plt>
jmp    143e <copyLoop+0x28e>
jmp    1448 <copyLoop+0x298>
jmp    1443 <copyLoop+0x293>
jmp    124c <copyLoop+0x9c>
jmp    144d <copyLoop+0x29d>
mov    -0x18(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyLoop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2e07(%rip),%rax
mov    (%rax),%ebx
movl   $0x0,0x18(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x420(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0x20(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
test   %rbp,%rbp
je     1379 <copyLoop+0x1c9>
test   %r14d,%r14d
je     1379 <copyLoop+0x1c9>
mov    %ebx,0x1c(%rsp)
lea    0x18(%rsp),%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   10a0 <RC4_set_key@plt>
lea    0x10(%rsp),%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   10a0 <RC4_set_key@plt>
lea    0x1(%r15),%r13d
lea    0x8(%rsp),%rbp
mov    0x2d82(%rip),%rbx
xchg   %ax,%ax
movl   $0x0,0x8(%rsp)
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    (%rbx),%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %rax,%rax
jle    1250 <copyLoop+0xa0>
mov    (%rbx),%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12b0 <copyLoop+0x100>
mov    (%rbx),%r12d
mov    %r15d,%r14d
cmp    $0xffffffff,%r12d
jne    12dc <copyLoop+0x12c>
jmp    1250 <copyLoop+0xa0>
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x1,%rax
sbb    %r14d,%r14d
mov    %r14d,%r12d
or     %r15d,%r12d
mov    0x2d21(%rip),%rax
or     (%rax),%r14d
cmp    $0xffffffff,%r12d
je     1250 <copyLoop+0xa0>
cmp    $0xffffffff,%r14d
je     1250 <copyLoop+0xa0>
mov    %r15,%rbp
mov    %rbx,%r15
lea    0x420(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    %r12d,%edi
mov    %rbx,%r12
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
je     1375 <copyLoop+0x1c5>
mov    %rax,%rbx
cmp    %ebp,%r14d
lea    0x10(%rsp),%rdi
lea    0x18(%rsp),%rax
cmove  %rax,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
lea    0x20(%rsp),%r12
mov    %r12,%rcx
call   1050 <RC4@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %r14d,%edi
call   10b0 <fsync@plt>
mov    %r15,%rbx
mov    %rbp,%r15
lea    0x8(%rsp),%rbp
jmp    1250 <copyLoop+0xa0>
mov    0x1c(%rsp),%ebx
mov    %ebx,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_10__ = type { i32 }
%struct.TYPE_9__ = type { i32 }
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @copyLoop(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.TYPE_10__
%9 = alloca %struct.TYPE_10__
%10 = alloca [1024 x i8]
%11 = alloca [1024 x i8]
%12 = alloca %struct.TYPE_9__
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %7
%17 = bitcast %struct.TYPE_10__* %8 to i8*
%18 = bitcast %struct.TYPE_10__* %9 to i8*
%19 = bitcast [1024 x i8]* %10 to i8*
%20 = bitcast [1024 x i8]* %11 to i8*
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %98
23: ; preds = %3
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %98
26: ; preds = %23
%27 = load i32, i32* %6
%28 = load i8*, i8** %5
%29 = call i32 @RC4_set_key(%struct.TYPE_10__* %8, i32 %27, i8* %28)
%30 = load i32, i32* %6
%31 = load i8*, i8** %5
%32 = call i32 @RC4_set_key(%struct.TYPE_10__* %9, i32 %30, i8* %31)
br label %33
33: ; preds = %26, %96
%34 = bitcast %struct.TYPE_9__* %12 to i8*
store i32 -1, i32* %13
store i32 -1, i32* %14
%35 = call i32 @FD_ZERO(%struct.TYPE_9__* %12)
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @FD_SET(i32 %36, %struct.TYPE_9__* %12)
%38 = load i32, i32* %4
%39 = call i32 @FD_SET(i32 %38, %struct.TYPE_9__* %12)
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
%42 = call i64 @select(i32 %41, %struct.TYPE_9__* %12, i32* null, i32* null, i32* null)
%43 = icmp slt i64 0, %42
br i1 %43, label %44, label %60
44: ; preds = %33
%45 = load i32, i32* @STDIN_FILENO
%46 = call i64 @FD_ISSET(i32 %45, %struct.TYPE_9__* %12)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* @STDIN_FILENO
store i32 %49, i32* %13
%50 = load i32, i32* %4
store i32 %50, i32* %14
br label %59
51: ; preds = %44
%52 = load i32, i32* %4
%53 = call i64 @FD_ISSET(i32 %52, %struct.TYPE_9__* %12)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* %4
store i32 %56, i32* %13
%57 = load i32, i32* @STDOUT_FILENO
store i32 %57, i32* %14
br label %58
58: ; preds = %55, %51
br label %59
59: ; preds = %58, %48
br label %60
60: ; preds = %59, %33
%61 = load i32, i32* %13
%62 = icmp ne i32 -1, %61
br i1 %62, label %63, label %96
63: ; preds = %60
%64 = load i32, i32* %14
%65 = icmp ne i32 -1, %64
br i1 %65, label %66, label %96
66: ; preds = %63
store i64 0, i64* %15
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%68 = call i32 @bzero(i8* %67, i32 1024)
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%70 = call i32 @bzero(i8* %69, i32 1024)
%71 = load i32, i32* %13
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%73 = call i64 @read(i32 %71, i8* %72, i32 1024)
store i64 %73, i64* %15
%74 = load i64, i64* %15
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %94
76: ; preds = %66
%77 = load i32, i32* %14
%78 = load i32, i32* %4
%79 = icmp eq i32 %77, %78
br i1 %79, label %80, label %81
80: ; preds = %76
br label %82
81: ; preds = %76
br label %82
82: ; preds = %81, %80
%83 = phi %struct.TYPE_10__* [ %8, %80 ], [ %9, %81 ]
%84 = load i64, i64* %15
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%87 = call i32 @RC4(%struct.TYPE_10__* %83, i64 %84, i8* %85, i8* %86)
%88 = load i32, i32* %14
%89 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%90 = load i64, i64* %15
%91 = call i32 @write(i32 %88, i8* %89, i64 %90)
%92 = load i32, i32* %14
%93 = call i32 @fsync(i32 %92)
br label %95
94: ; preds = %66
br label %97
95: ; preds = %82
br label %96
96: ; preds = %95, %63, %60
br label %33
97: ; preds = %94
br label %98
98: ; preds = %97, %23, %3
%99 = load i32, i32* %7
ret i32 %99
}

LLVM IR (-O2):
%struct.TYPE_10__ = type { i32 }
%struct.TYPE_9__ = type { i32 }
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @copyLoop(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.TYPE_10__
%5 = alloca %struct.TYPE_10__
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca %struct.TYPE_9__
%9 = load i32, i32* @EXIT_FAILURE
%10 = bitcast %struct.TYPE_10__* %4 to i8*
%11 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %4, i64 0, i32 0
store i32 0, i32* %11
%12 = bitcast %struct.TYPE_10__* %5 to i8*
%13 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %5, i64 0, i32 0
store i32 0, i32* %13
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%16 = icmp ne i8* %1, null
%17 = icmp ne i32 %2, 0
%18 = and i1 %16, %17
br i1 %18, label %19, label %63
19: ; preds = %3
%20 = call i32 @RC4_set_key(%struct.TYPE_10__* %4, i32 %2, i8* %1)
%21 = call i32 @RC4_set_key(%struct.TYPE_10__* %5, i32 %2, i8* %1)
%22 = bitcast %struct.TYPE_9__* %8 to i8*
%23 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %8, i64 0, i32 0
%24 = add nsw i32 %0, 1
br label %25
25: ; preds = %62, %19
store i32 0, i32* %23
%26 = call i32 @FD_ZERO(%struct.TYPE_9__* %8)
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @FD_SET(i32 %27, %struct.TYPE_9__* %8)
%29 = call i32 @FD_SET(i32 %0, %struct.TYPE_9__* %8)
%30 = call i64 @select(i32 %24, %struct.TYPE_9__* %8, i32* null, i32* null, i32* null)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %62
32: ; preds = %25
%33 = load i32, i32* @STDIN_FILENO
%34 = call i64 @FD_ISSET(i32 %33, %struct.TYPE_9__* %8)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = load i32, i32* @STDIN_FILENO
br label %44
38: ; preds = %32
%39 = call i64 @FD_ISSET(i32 %0, %struct.TYPE_9__* %8)
%40 = icmp eq i64 %39, 0
%41 = load i32, i32* @STDOUT_FILENO
%42 = select i1 %40, i32 -1, i32 %0
%43 = select i1 %40, i32 -1, i32 %41
br label %44
44: ; preds = %38, %36
%45 = phi i32 [ %37, %36 ], [ %42, %38 ]
%46 = phi i32 [ %0, %36 ], [ %43, %38 ]
%47 = icmp ne i32 %45, -1
%48 = icmp ne i32 %46, -1
%49 = select i1 %47, i1 %48, i1 false
br i1 %49, label %50, label %62
50: ; preds = %44
%51 = call i32 @bzero(i8* %14, i32 1024)
%52 = call i32 @bzero(i8* %15, i32 1024)
%53 = call i64 @read(i32 %45, i8* %14, i32 1024)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %63
56: ; preds = %50
%57 = icmp eq i32 %46, %0
%58 = select i1 %57, %struct.TYPE_10__* %4, %struct.TYPE_10__* %5
%59 = call i32 @RC4(%struct.TYPE_10__* %58, i64 %53, i8* %14, i8* %15)
%60 = call i32 @write(i32 %46, i8* %15, i64 %53)
%61 = call i32 @fsync(i32 %46)
br label %62
62: ; preds = %25, %44, %56
br label %25
63: ; preds = %55, %3
ret i32 %9
}