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

/* Variables and functions */
int /*<<< orphan*/ attack_cipher(int /*<<< orphan*/ *, size_t,
                                 int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ read_file(int /*<<< orphan*/ **, size_t *, char *);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
int main(int argc, char *argv[]) {
  char *filename = NULL, *crib = NULL;
  uint8_t *ciphertext = NULL;
  size_t ciphertext_len, crib_len;
  if (argc < 3) {
    fprintf(stderr, "usage: roxor <file> <crib>\n");
    return 1;
  }
  filename = argv[1];
  crib = argv[2];
  crib_len = strlen(crib);
  if (crib_len < 2) {
    fprintf(stderr, "error: crib too short\n");
    return 1;
  }
  if (read_file(&ciphertext, &ciphertext_len, filename)) {
    fprintf(stderr, "%s: read error\n", filename);
    return 1;
  }
  if (ciphertext_len < crib_len) {
    fprintf(stderr, "error: ciphertext too short\n");
    free(ciphertext);
    return 1;
  }
  attack_cipher(ciphertext, ciphertext_len, (uint8_t *)crib, crib_len);
  free(ciphertext);
  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)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ab <main+0x5b>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x14f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x2,-0x38(%rbp)
jae    11fe <main+0xae>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x14f>
mov    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <read_file@plt>
cmp    $0x0,%rax
je     1240 <main+0xf0>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x14f>
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    127a <main+0x12a>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x14f>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1070 <attack_cipher@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
cmp    $0x2,%edi
jg     1178 <main+0x28>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    0xe8a(%rip),%rsi
jmp    119e <main+0x4e>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    $0x1,%rax
ja     11b4 <main+0x64>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rax,%rbx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %r14,%rdx
call   1060 <read_file@plt>
test   %rax,%rax
je     11e8 <main+0x98>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11a5 <main+0x55>
mov    0x8(%rsp),%rsi
cmp    %rbx,%rsi
jae    1214 <main+0xc4>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    11a5 <main+0x55>
mov    (%rsp),%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1070 <attack_cipher@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11aa <main+0x5a>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"usage: roxor <file> <crib>\0A\00"
@.str.1 = [23 x i8] c"error: crib too short\0A\00"
@.str.2 = [16 x i8] c"%s: read error\0A\00"
@.str.3 = [29 x i8] c"error: ciphertext too short\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i32* null, i32** %8
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %56
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = icmp ult i64 %25, 2
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %56
30: ; preds = %16
%31 = load i8*, i8** %6
%32 = call i64 @read_file(i32** %8, i64* %9, i8* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %6
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i32 1, i32* %3
br label %56
38: ; preds = %30
%39 = load i64, i64* %9
%40 = load i64, i64* %10
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32*, i32** %8
%46 = call i32 @free(i32* %45)
store i32 1, i32* %3
br label %56
47: ; preds = %38
%48 = load i32*, i32** %8
%49 = load i64, i64* %9
%50 = load i8*, i8** %7
%51 = bitcast i8* %50 to i32*
%52 = load i64, i64* %10
%53 = call i32 @attack_cipher(i32* %48, i64 %49, i32* %51, i64 %52)
%54 = load i32*, i32** %8
%55 = call i32 @free(i32* %54)
store i32 0, i32* %3
br label %56
56: ; preds = %47, %42, %34, %27, %13
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"usage: roxor <file> <crib>\0A\00"
@.str.1 = [23 x i8] c"error: crib too short\0A\00"
@.str.2 = [16 x i8] c"%s: read error\0A\00"
@.str.3 = [29 x i8] c"error: ciphertext too short\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%6 = bitcast i64* %4 to i8*
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %41
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%17 = icmp ult i64 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %41
21: ; preds = %11
%22 = call i64 @read_file(i32** %3, i64* %4, i8* %13)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %41
27: ; preds = %21
%28 = load i64, i64* %4
%29 = icmp ult i64 %28, %16
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32*, i32** %3
%34 = call i32 @free(i32* %33)
br label %41
35: ; preds = %27
%36 = load i32*, i32** %3
%37 = bitcast i8* %15 to i32*
%38 = call i32 @attack_cipher(i32* %36, i64 %28, i32* %37, i64 %16)
%39 = load i32*, i32** %3
%40 = call i32 @free(i32* %39)
br label %41
41: ; preds = %35, %30, %24, %18, %8
%42 = phi i32 [ 1, %8 ], [ 1, %18 ], [ 1, %24 ], [ 1, %30 ], [ 0, %35 ]
ret i32 %42
}