#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 counthex(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int maxlinelength(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int scorearray(char *);
int /*<<< orphan*/ str2hex(char *, unsigned char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ xorarray(unsigned char *, int, unsigned char, char *);
void challenge4() {
  printf("\nchallenge 4:\n");
  FILE *input = fopen("4.txt", "r");
  if (input == NULL) {
    printf("unable to open file 4.txt\n");
    exit(1);
  }
  int max = maxlinelength(input);
  char line[max];
  int topscore = 0;
  char top[max];
  while (fscanf(input, "%s", line) == 1) {
    int size = counthex(line);
    unsigned char hex[size];
    str2hex(line, hex);
    unsigned char c;
    for (c = 0; c <= 254; c++) {
      char current[size];
      xorarray(hex, size, c, current);
      int score = scorearray(current);
      if (score > topscore) {
        topscore = score;
        strcpy(top, current);
      }
    }
  }
  fclose(input);
  printf("%d\n", topscore);
  printf("%s\n", top);
}


x86 assembly (clang, -O0):
<challenge4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe3f(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1203 <challenge4+0x53>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1070 <maxlinelength@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x78(%rbp),%rdx
mov    -0x8(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1080 <fscanf@plt>
cmp    $0x1,%eax
jne    1350 <challenge4+0x1a0>
mov    -0x78(%rbp),%rdi
call   1090 <counthex@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x80(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x48(%rbp)
call   10a0 <str2hex@plt>
movb   $0x0,-0x49(%rbp)
movzbl -0x49(%rbp),%eax
cmp    $0xfe,%eax
jg     1344 <challenge4+0x194>
mov    -0x80(%rbp),%rdi
mov    -0x34(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
mov    -0x34(%rbp),%esi
movzbl -0x49(%rbp),%edx
call   10d0 <xorarray@plt>
mov    -0x88(%rbp),%rdi
call   1060 <scorearray@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    1330 <challenge4+0x180>
mov    -0x88(%rbp),%rsi
mov    -0x70(%rbp),%rdi
mov    -0x64(%rbp),%eax
mov    %eax,-0x24(%rbp)
call   1030 <strcpy@plt>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x49(%rbp),%al
add    $0x1,%al
mov    %al,-0x49(%rbp)
jmp    12b7 <challenge4+0x107>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
jmp    125b <challenge4+0xab>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%esi
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x70(%rbp),%rsi
lea    0xcc4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<challenge4>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xe31(%rip),%rdi
call   1040 <puts@plt>
lea    0xe47(%rip),%rdi
lea    0xe15(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     133e <challenge4+0x17e>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <maxlinelength@plt>
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
lea    0xdd7(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1090 <fscanf@plt>
movl   $0x0,-0x2c(%rbp)
cmp    $0x1,%eax
jne    130d <challenge4+0x14d>
movl   $0x0,-0x2c(%rbp)
mov    %r14,-0x48(%rbp)
mov    %rbx,-0x40(%rbp)
jmp    1287 <challenge4+0xc7>
nopw   0x0(%rax,%rax,1)
mov    -0x50(%rbp),%rsp
mov    -0x48(%rbp),%r14
mov    %r14,%rdi
lea    0xd90(%rip),%rsi
mov    -0x40(%rbp),%rbx
mov    %rbx,%rdx
call   1090 <fscanf@plt>
cmp    $0x1,%eax
jne    130d <challenge4+0x14d>
mov    %rbx,%rdi
call   10a0 <counthex@plt>
mov    %rsp,-0x50(%rbp)
mov    %eax,%eax
mov    %rsp,%r14
mov    %rax,-0x60(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rax,-0x58(%rbp)
sub    %rax,%r14
mov    %r14,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <str2hex@plt>
xor    %r12d,%r12d
jmp    12cd <challenge4+0x10d>
xchg   %ax,%ax
mov    %r13,%rsp
add    $0x1,%r12b
cmp    $0xff,%r12b
je     1260 <challenge4+0xa0>
mov    %rsp,%r13
mov    %rsp,%r15
sub    -0x58(%rbp),%r15
mov    %r15,%rsp
movzbl %r12b,%edx
mov    %r14,%rdi
mov    -0x60(%rbp),%rsi
mov    %r15,%rcx
call   10e0 <xorarray@plt>
mov    %r15,%rdi
call   1070 <scorearray@plt>
cmp    -0x2c(%rbp),%eax
jle    12c0 <challenge4+0x100>
mov    %eax,%ebx
mov    -0x38(%rbp),%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %ebx,-0x2c(%rbp)
jmp    12c0 <challenge4+0x100>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xce9(%rip),%rdi
mov    -0x2c(%rbp),%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <puts@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd2(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"\0Achallenge 4:\0A\00"
@.str.1 = [6 x i8] c"4.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [27 x i8] c"unable to open file 4.txt\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define void @challenge4() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%15 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %1
%16 = load i32*, i32** %1
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = load i32*, i32** %1
%23 = call i32 @maxlinelength(i32* %22)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = zext i32 %24 to i64
%26 = call i8* @llvm.stacksave()
store i8* %26, i8** %3
%27 = alloca i8, i64 %25
store i64 %25, i64* %4
store i32 0, i32* %5
%28 = load i32, i32* %2
%29 = zext i32 %28 to i64
%30 = alloca i8, i64 %29
store i64 %29, i64* %6
br label %31
31: ; preds = %66, %21
%32 = load i32*, i32** %1
%33 = call i32 @fscanf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %27)
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %68
35: ; preds = %31
%36 = call i32 @counthex(i8* %27)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = zext i32 %37 to i64
%39 = call i8* @llvm.stacksave()
store i8* %39, i8** %8
%40 = alloca i8, i64 %38
store i64 %38, i64* %9
%41 = call i32 @str2hex(i8* %27, i8* %40)
store i8 0, i8* %10
br label %42
42: ; preds = %63, %35
%43 = load i8, i8* %10
%44 = zext i8 %43 to i32
%45 = icmp sle i32 %44, 254
br i1 %45, label %46, label %66
46: ; preds = %42
%47 = load i32, i32* %7
%48 = zext i32 %47 to i64
%49 = call i8* @llvm.stacksave()
store i8* %49, i8** %11
%50 = alloca i8, i64 %48
store i64 %48, i64* %12
%51 = load i32, i32* %7
%52 = load i8, i8* %10
%53 = call i32 @xorarray(i8* %40, i32 %51, i8 zeroext %52, i8* %50)
%54 = call i32 @scorearray(i8* %50)
store i32 %54, i32* %13
%55 = load i32, i32* %13
%56 = load i32, i32* %5
%57 = icmp sgt i32 %55, %56
br i1 %57, label %58, label %61
58: ; preds = %46
%59 = load i32, i32* %13
store i32 %59, i32* %5
%60 = call i32 @strcpy(i8* %30, i8* %50)
br label %61
61: ; preds = %58, %46
%62 = load i8*, i8** %11
br label %63
63: ; preds = %61
%64 = load i8, i8* %10
%65 = add i8 %64, 1
store i8 %65, i8* %10
br label %42
66: ; preds = %42
%67 = load i8*, i8** %8
br label %31
68: ; preds = %31
%69 = load i32*, i32** %1
%70 = call i32 @fclose(i32* %69)
%71 = load i32, i32* %5
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %71)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %30)
%74 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"4.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@str = [14 x i8] c"\0Achallenge 4:\00"
@str.7 = [26 x i8] c"unable to open file 4.txt\00"
define void @challenge4() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @maxlinelength(i32* %2)
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = alloca i8, i64 %9
%12 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %10)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %38
14: ; preds = %7, %35
%15 = phi i32 [ %32, %35 ], [ 0, %7 ]
%16 = call i32 @counthex(i8* %10)
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
%19 = alloca i8, i64 %17
%20 = call i32 @str2hex(i8* %10, i8* %19)
br label %21
21: ; preds = %14, %31
%22 = phi i32 [ %15, %14 ], [ %32, %31 ]
%23 = phi i8 [ 0, %14 ], [ %33, %31 ]
%24 = call i8* @llvm.stacksave()
%25 = alloca i8, i64 %17
%26 = call i32 @xorarray(i8* %19, i32 %16, i8 zeroext %23, i8* %25)
%27 = call i32 @scorearray(i8* %25)
%28 = icmp sgt i32 %27, %22
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 @strcpy(i8* %11, i8* %25)
br label %31
31: ; preds = %29, %21
%32 = phi i32 [ %27, %29 ], [ %22, %21 ]
%33 = add nuw i8 %23, 1
%34 = icmp eq i8 %33, -1
br i1 %34, label %35, label %21
35: ; preds = %31
%36 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %10)
%37 = icmp eq i32 %36, 1
br i1 %37, label %14, label %38
38: ; preds = %35, %7
%39 = phi i32 [ 0, %7 ], [ %32, %35 ]
%40 = call i32 @fclose(i32* %2)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%42 = call i32 @puts(i8* %11)
ret void
}