#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
unsigned int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t strlen(char *);
unsigned int myxor(FILE *input, FILE *output, char *key) {
  char buffer[1024];
  char key_item;
  size_t key_len;
  unsigned counter;
  unsigned size;
  fseek(input, 0L, SEEK_END);
  size = ftell(input);
  rewind(input);
  key_len = strlen(key);
  setbuf(output, NULL);
  do {
    counter = fread(buffer, 1, 1024, input);
    for (int i = 0; i < counter; i++) {
      key_item = key[i % key_len];
      buffer[i] ^= key_item;
    }
    fwrite(buffer, counter, 1, output);
  } while (counter > 0);
  return counter;
}


x86 assembly (clang, -O0):
<myxor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x438(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x434(%rbp)
movl   $0x0,-0x43c(%rbp)
mov    -0x43c(%rbp),%eax
cmp    -0x434(%rbp),%eax
jae    126e <myxor+0xfe>
mov    -0x18(%rbp),%rax
mov    %rax,-0x448(%rbp)
movslq -0x43c(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x430(%rbp)
mov    -0x448(%rbp),%rax
mov    (%rax,%rdx,1),%al
mov    %al,-0x421(%rbp)
movsbl -0x421(%rbp),%edx
movslq -0x43c(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%ecx
ff
xor    %edx,%ecx
mov    %cl,-0x420(%rbp,%rax,1)
mov    -0x43c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x43c(%rbp)
jmp    11fc <myxor+0x8c>
lea    -0x420(%rbp),%rdi
mov    -0x434(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmpl   $0x0,-0x434(%rbp)
ja     11d2 <myxor+0x62>
mov    -0x434(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myxor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %r15,%rdi
call   1060 <rewind@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <setbuf@plt>
mov    %rsp,%r13
jmp    11f9 <myxor+0x89>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rcx,%rax
xor    %edx,%edx
div    %rbx
mov    (%r12,%rdx,1),%al
xor    %al,(%rsp,%rcx,1)
mov    %r13,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
test   %ebp,%ebp
je     12b6 <myxor+0x146>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
je     12a4 <myxor+0x134>
mov    %ebp,%esi
cmp    $0x1,%ebp
jne    1240 <myxor+0xd0>
xor    %ecx,%ecx
test   $0x1,%sil
je     11df <myxor+0x6f>
mov    %rcx,%rax
or     %rbx,%rax
shr    $0x20,%rax
jne    11d0 <myxor+0x60>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
jmp    11d8 <myxor+0x68>
nopl   0x0(%rax,%rax,1)
mov    %esi,%edi
and    $0xfffffffe,%edi
xor    %ecx,%ecx
jmp    1267 <myxor+0xf7>
nopl   0x0(%rax)
xor    %edx,%edx
div    %rbx
movzbl (%r12,%rdx,1),%eax
xor    %al,0x1(%rsp,%rcx,1)
add    $0x2,%rcx
cmp    %rcx,%rdi
je     1221 <myxor+0xb1>
mov    %rcx,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     1280 <myxor+0x110>
mov    %rcx,%rax
xor    %edx,%edx
div    %rbx
jmp    1286 <myxor+0x116>
nopl   (%rax)
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
movzbl (%r12,%rdx,1),%eax
xor    %al,(%rsp,%rcx,1)
lea    0x1(%rcx),%rax
mov    %rax,%rdx
or     %rbx,%rdx
shr    $0x20,%rdx
jne    1250 <myxor+0xe0>
xor    %edx,%edx
div    %ebx
jmp    1255 <myxor+0xe5>
mov    %rsp,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
define i32 @myxor(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i8
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%13 = load i32*, i32** %4
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i64 0, i32 %14)
%16 = load i32*, i32** %4
%17 = call i32 @ftell(i32* %16)
store i32 %17, i32* %11
%18 = load i32*, i32** %4
%19 = call i32 @rewind(i32* %18)
%20 = load i8*, i8** %6
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %9
%22 = load i32*, i32** %5
%23 = call i32 @setbuf(i32* %22, i32* null)
br label %24
24: ; preds = %57, %3
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = load i32*, i32** %4
%27 = call i32 @fread(i8* %25, i32 1, i32 1024, i32* %26)
store i32 %27, i32* %10
store i32 0, i32* %12
br label %28
28: ; preds = %49, %24
%29 = load i32, i32* %12
%30 = load i32, i32* %10
%31 = icmp ult i32 %29, %30
br i1 %31, label %32, label %52
32: ; preds = %28
%33 = load i8*, i8** %6
%34 = load i32, i32* %12
%35 = sext i32 %34 to i64
%36 = load i64, i64* %9
%37 = urem i64 %35, %36
%38 = getelementptr inbounds i8, i8* %33, i64 %37
%39 = load i8, i8* %38
store i8 %39, i8* %8
%40 = load i8, i8* %8
%41 = sext i8 %40 to i32
%42 = load i32, i32* %12
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = xor i32 %46, %41
%48 = trunc i32 %47 to i8
store i8 %48, i8* %44
br label %49
49: ; preds = %32
%50 = load i32, i32* %12
%51 = add nsw i32 %50, 1
store i32 %51, i32* %12
br label %28
52: ; preds = %28
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%54 = load i32, i32* %10
%55 = load i32*, i32** %5
%56 = call i32 @fwrite(i8* %53, i32 %54, i32 1, i32* %55)
br label %57
57: ; preds = %52
%58 = load i32, i32* %10
%59 = icmp ugt i32 %58, 0
br i1 %59, label %24, label %60
60: ; preds = %57
%61 = load i32, i32* %10
ret i32 %61
}

LLVM IR (-O2):
@SEEK_END = global i32 0
define i32 @myxor(i32* nocapture %0, i32* nocapture %1, i8* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %0, i64 0, i32 %6)
%8 = tail call i32 @ftell(i32* %0)
%9 = tail call i32 @rewind(i32* %0)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%11 = tail call i32 @setbuf(i32* %1, i32* null)
br label %12
12: ; preds = %33, %3
%13 = call i32 @fread(i8* %5, i32 1, i32 1024, i32* %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @fwrite(i8* %5, i32 %13, i32 1, i32* %1)
br label %54
17: ; preds = %12
%18 = zext i32 %13 to i64
%19 = and i64 %18, 1
%20 = icmp eq i32 %13, 1
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = and i64 %18, 4294967294
br label %35
23: ; preds = %35, %17
%24 = phi i64 [ 0, %17 ], [ %51, %35 ]
%25 = icmp eq i64 %19, 0
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = urem i64 %24, %10
%28 = getelementptr inbounds i8, i8* %2, i64 %27
%29 = load i8, i8* %28
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %24
%31 = load i8, i8* %30
%32 = xor i8 %31, %29
store i8 %32, i8* %30
br label %33
33: ; preds = %23, %26
%34 = call i32 @fwrite(i8* %5, i32 %13, i32 1, i32* %1)
br i1 %14, label %54, label %12
35: ; preds = %35, %21
%36 = phi i64 [ 0, %21 ], [ %51, %35 ]
%37 = phi i64 [ 0, %21 ], [ %52, %35 ]
%38 = urem i64 %36, %10
%39 = getelementptr inbounds i8, i8* %2, i64 %38
%40 = load i8, i8* %39
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %36
%42 = load i8, i8* %41
%43 = xor i8 %42, %40
store i8 %43, i8* %41
%44 = or i64 %36, 1
%45 = urem i64 %44, %10
%46 = getelementptr inbounds i8, i8* %2, i64 %45
%47 = load i8, i8* %46
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %44
%49 = load i8, i8* %48
%50 = xor i8 %49, %47
store i8 %50, i8* %48
%51 = add nuw nsw i64 %36, 2
%52 = add i64 %37, 2
%53 = icmp eq i64 %52, %22
br i1 %53, label %23, label %35
54: ; preds = %33, %15
ret i32 0
}