#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 EOF;
int /*<<< orphan*/ SEEK_END;
int atoi(char *);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  FILE *in, *out;
  int offset;
  int size = -1;
  int val;
  int index;
  char *fileData;
  if (argc != 4) {
    printf("Usage: xor inputfile offset outfile\n");
    return -1;
  }
  if ((in = fopen(argv[1], "rb")) == NULL) {
    printf("Cant't open file name %s\n", argv[1]);
    return -1;
  }
  offset = atoi(argv[2]);
  if ((out = fopen(argv[3], "wb")) == NULL) {
    printf("Cant't open file name %s\n", argv[3]);
    fclose(in);
    return -1;
  }
  printf("offset %i bytes\n", offset);
  fseek(in, 0, SEEK_END);
  size = ftell(in);
  printf("filesize %i bytes\n", size);
  fileData = (char *)calloc(size, sizeof(char));
  rewind(in);
  index = 0;
  while ((val = fgetc(in)) != EOF) {
    fileData[index++] = val;
  }
  fclose(in);
  for (index = 0; index < size; index++) {
    fputc(fileData[index] ^ fileData[(index + offset) % size], out);
  }
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11e1 <main+0x41>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <main+0x1ee>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe35(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1225 <main+0x85>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <main+0x1ee>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdfe(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1282 <main+0xe2>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <main+0x1ee>
mov    -0x24(%rbp),%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2ce8(%rip),%rcx
cmp    (%rcx),%eax
je     131f <main+0x17f>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x30(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    12e5 <main+0x145>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    137e <main+0x1de>
mov    -0x38(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%eax
add    -0x24(%rbp),%eax
cltd
idivl  -0x28(%rbp)
mov    -0x3c(%rbp),%eax
movslq %edx,%rdx
movsbl (%rcx,%rdx,1),%ecx
xor    %ecx,%eax
mov    -0x20(%rbp),%rsi
movsbl %al,%edi
call   1070 <fputc@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    132f <main+0x18f>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    12e4 <main+0x134>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0xe2e(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12f2 <main+0x142>
mov    %rax,%r12
mov    0x10(%rbp),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r15d
mov    0x18(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1306 <main+0x156>
lea    0xe0d(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r12,%rdi
call   10a0 <ftell@plt>
mov    %eax,%ebp
mov    %eax,%r13d
lea    0xded(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1060 <rewind@plt>
mov    %r12,%rdi
call   1070 <fgetc@plt>
mov    0x2d6d(%rip),%rbx
cmp    (%rbx),%eax
je     1293 <main+0xe3>
mov    %r14,%rbp
nopw   0x0(%rax,%rax,1)
mov    %al,0x0(%rbp)
mov    %r12,%rdi
call   1070 <fgetc@plt>
add    $0x1,%rbp
cmp    (%rbx),%eax
jne    1280 <main+0xd0>
mov    %r12,%rdi
call   1040 <fclose@plt>
test   %r13d,%r13d
mov    (%rsp),%rbp
jle    12d8 <main+0x128>
mov    %r15d,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    (%r15,%rbx,1),%eax
cltd
idiv   %r13d
movslq %edx,%rax
movzbl (%r14,%rax,1),%eax
xor    (%r14,%rbx,1),%al
movsbl %al,%edi
mov    %rbp,%rsi
call   1080 <fputc@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    12b0 <main+0x100>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1325 <main+0x175>
lea    0xd59(%rip),%rdi
call   1030 <puts@plt>
jmp    1320 <main+0x170>
mov    0x8(%rbp),%rsi
lea    0xd06(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1320 <main+0x170>
mov    0x18(%rbp),%rsi
lea    0xcf2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Usage: xor inputfile offset outfile\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Cant't open file name %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [17 x i8] c"offset %i bytes\0A\00"
@SEEK_END = global i32 0
@.str.5 = [19 x i8] c"filesize %i bytes\0A\00"
@EOF = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %9
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %107
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 -1, i32* %3
br label %107
28: ; preds = %17
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %8
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 3
%35 = load i8*, i8** %34
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %36, i32** %7
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %45
38: ; preds = %28
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
store i32 -1, i32* %3
br label %107
45: ; preds = %28
%46 = load i32, i32* %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = load i32*, i32** %6
%49 = load i32, i32* @SEEK_END
%50 = call i32 @fseek(i32* %48, i32 0, i32 %49)
%51 = load i32*, i32** %6
%52 = call i32 @ftell(i32* %51)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %9
%56 = call i64 @calloc(i32 %55, i32 1)
%57 = inttoptr i64 %56 to i8*
store i8* %57, i8** %12
%58 = load i32*, i32** %6
%59 = call i32 @rewind(i32* %58)
store i32 0, i32* %11
br label %60
60: ; preds = %65, %45
%61 = load i32*, i32** %6
%62 = call i32 @fgetc(i32* %61)
store i32 %62, i32* %10
%63 = load i32, i32* @EOF
%64 = icmp ne i32 %62, %63
br i1 %64, label %65, label %73
65: ; preds = %60
%66 = load i32, i32* %10
%67 = trunc i32 %66 to i8
%68 = load i8*, i8** %12
%69 = load i32, i32* %11
%70 = add nsw i32 %69, 1
store i32 %70, i32* %11
%71 = sext i32 %69 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
store i8 %67, i8* %72
br label %60
73: ; preds = %60
%74 = load i32*, i32** %6
%75 = call i32 @fclose(i32* %74)
store i32 0, i32* %11
br label %76
76: ; preds = %101, %73
%77 = load i32, i32* %11
%78 = load i32, i32* %9
%79 = icmp slt i32 %77, %78
br i1 %79, label %80, label %104
80: ; preds = %76
%81 = load i8*, i8** %12
%82 = load i32, i32* %11
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %81, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = load i8*, i8** %12
%88 = load i32, i32* %11
%89 = load i32, i32* %8
%90 = add nsw i32 %88, %89
%91 = load i32, i32* %9
%92 = srem i32 %90, %91
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8, i8* %87, i64 %93
%95 = load i8, i8* %94
%96 = sext i8 %95 to i32
%97 = xor i32 %86, %96
%98 = trunc i32 %97 to i8
%99 = load i32*, i32** %7
%100 = call i32 @fputc(i8 signext %98, i32* %99)
br label %101
101: ; preds = %80
%102 = load i32, i32* %11
%103 = add nsw i32 %102, 1
store i32 %103, i32* %11
br label %76
104: ; preds = %76
%105 = load i32*, i32** %7
%106 = call i32 @fclose(i32* %105)
store i32 0, i32* %3
br label %107
107: ; preds = %104, %38, %23, %15
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Cant't open file name %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [17 x i8] c"offset %i bytes\0A\00"
@SEEK_END = global i32 0
@.str.5 = [19 x i8] c"filesize %i bytes\0A\00"
@EOF = global i32 0
@str = [36 x i8] c"Usage: xor inputfile offset outfile\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %68
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %68
14: ; preds = %6
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = load i8*, i8** %18
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = tail call i32 @fclose(i32* %9)
br label %68
26: ; preds = %14
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%28 = load i32, i32* @SEEK_END
%29 = tail call i32 @fseek(i32* %9, i32 0, i32 %28)
%30 = tail call i32 @ftell(i32* %9)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = tail call i64 @calloc(i32 %30, i32 1)
%33 = inttoptr i64 %32 to i8*
%34 = tail call i32 @rewind(i32* %9)
%35 = tail call i32 @fgetc(i32* %9)
%36 = load i32, i32* @EOF
%37 = icmp eq i32 %35, %36
br i1 %37, label %47, label %38
38: ; preds = %26, %38
%39 = phi i64 [ %42, %38 ], [ 0, %26 ]
%40 = phi i32 [ %44, %38 ], [ %35, %26 ]
%41 = trunc i32 %40 to i8
%42 = add nuw i64 %39, 1
%43 = getelementptr inbounds i8, i8* %33, i64 %39
store i8 %41, i8* %43
%44 = tail call i32 @fgetc(i32* %9)
%45 = load i32, i32* @EOF
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %38
47: ; preds = %38, %26
%48 = tail call i32 @fclose(i32* %9)
%49 = icmp sgt i32 %30, 0
br i1 %49, label %50, label %66
50: ; preds = %47
%51 = zext i32 %30 to i64
br label %52
52: ; preds = %50, %52
%53 = phi i64 [ 0, %50 ], [ %64, %52 ]
%54 = getelementptr inbounds i8, i8* %33, i64 %53
%55 = load i8, i8* %54
%56 = trunc i64 %53 to i32
%57 = add i32 %17, %56
%58 = srem i32 %57, %30
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %33, i64 %59
%61 = load i8, i8* %60
%62 = xor i8 %61, %55
%63 = tail call i32 @fputc(i8 signext %62, i32* %20)
%64 = add nuw nsw i64 %53, 1
%65 = icmp eq i64 %64, %51
br i1 %65, label %66, label %52
66: ; preds = %52, %47
%67 = tail call i32 @fclose(i32* %20)
br label %68
68: ; preds = %66, %22, %11, %4
%69 = phi i32 [ -1, %4 ], [ -1, %11 ], [ -1, %22 ], [ 0, %66 ]
ret i32 %69
}