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

/* Variables and functions */
int counthex(char const *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ printhex(unsigned char *, int);
int /*<<< orphan*/ str2hex(char const *, unsigned char *);
void challenge2() {
  int size;
  const char one[] = "1c0111001f010100061a024b53535009181c";
  const char two[] = "686974207468652062756c6c277320657965";
  printf("\nchallenge 2:\n");
  size = counthex(one);
  unsigned char hex1[size];
  str2hex(one, hex1);
  size = counthex(two);
  unsigned char hex2[size];
  str2hex(two, hex2);
  printhex(hex2, size);
  printf("xor = '");
  int i;
  for (i = 0; i < size; i++)
    printf("%c", hex1[i] ^ hex2[i]);
  printf("'\n");
}


x86 assembly (clang, -O0):
<challenge2>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movaps 0xebe(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
movaps 0xea3(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
movabs $0x63313831393030,%rax
mov    %rax,-0x13(%rbp)
movabs $0x35363937353630,%rax
mov    %rax,-0x43(%rbp)
movaps 0xebc(%rip),%xmm0
movaps %xmm0,-0x50(%rbp)
movaps 0xea1(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
lea    0xebb(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    %rdi,-0x98(%rbp)
call   1040 <counthex@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x90(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x70(%rbp)
call   1050 <str2hex@plt>
lea    -0x60(%rbp),%rdi
call   1040 <counthex@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x88(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x78(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <str2hex@plt>
mov    -0x88(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1060 <printhex@plt>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x7c(%rbp)
mov    -0x7c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    128c <challenge2+0x14c>
mov    -0x88(%rbp),%rax
mov    -0x90(%rbp),%rcx
movslq -0x7c(%rbp),%rdx
movzbl (%rcx,%rdx,1),%esi
movslq -0x7c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
xor    %eax,%esi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    1244 <challenge2+0x104>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x68(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<challenge2>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
movaps 0xe98(%rip),%xmm0
movaps %xmm0,-0x80(%rbp)
movaps 0xe7d(%rip),%xmm0
movaps %xmm0,-0x90(%rbp)
movabs $0x63313831393030,%rax
mov    %rax,-0x73(%rbp)
movaps 0xea1(%rip),%xmm0
movaps %xmm0,-0x50(%rbp)
movaps 0xe86(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
movabs $0x35363937353630,%rax
mov    %rax,-0x43(%rbp)
lea    0xe9a(%rip),%rdi
call   1040 <puts@plt>
lea    -0x90(%rbp),%rbx
mov    %rbx,%rdi
call   1060 <counthex@plt>
mov    %rsp,-0x30(%rbp)
mov    %eax,%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <str2hex@plt>
lea    -0x60(%rbp),%rbx
mov    %rbx,%rdi
call   1060 <counthex@plt>
mov    %eax,%r15d
mov    %eax,%r13d
mov    %rsp,%r12
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <str2hex@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1080 <printhex@plt>
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r15d,%r15d
jle    126a <challenge2+0x10a>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r12,%rbx,1),%eax
xor    (%r14,%rbx,1),%al
movzbl %al,%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1250 <challenge2+0xf0>
lea    0xdea(%rip),%rdi
call   1040 <puts@plt>
mov    -0x30(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.challenge2.one = [37 x i8] c"1c0111001f010100061a024b53535009181c\00"
@__const.challenge2.two = [37 x i8] c"686974207468652062756c6c277320657965\00"
@.str = [15 x i8] c"\0Achallenge 2:\0A\00"
@.str.1 = [8 x i8] c"xor = '\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [3 x i8] c"'\0A\00"
define void @challenge2() {
%1 = alloca i32
%2 = alloca [37 x i8]
%3 = alloca [37 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = bitcast [37 x i8]* %2 to i8*
%9 = bitcast [37 x i8]* %3 to i8*
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [37 x i8], [37 x i8]* %2, i64 0, i64 0
%12 = call i32 @counthex(i8* %11)
store i32 %12, i32* %1
%13 = load i32, i32* %1
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %4
%16 = alloca i8, i64 %14
store i64 %14, i64* %5
%17 = getelementptr inbounds [37 x i8], [37 x i8]* %2, i64 0, i64 0
%18 = call i32 @str2hex(i8* %17, i8* %16)
%19 = getelementptr inbounds [37 x i8], [37 x i8]* %3, i64 0, i64 0
%20 = call i32 @counthex(i8* %19)
store i32 %20, i32* %1
%21 = load i32, i32* %1
%22 = zext i32 %21 to i64
%23 = alloca i8, i64 %22
store i64 %22, i64* %6
%24 = getelementptr inbounds [37 x i8], [37 x i8]* %3, i64 0, i64 0
%25 = call i32 @str2hex(i8* %24, i8* %23)
%26 = load i32, i32* %1
%27 = call i32 @printhex(i8* %23, i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %7
br label %29
29: ; preds = %46, %0
%30 = load i32, i32* %7
%31 = load i32, i32* %1
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %49
33: ; preds = %29
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %16, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %23, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = xor i32 %38, %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %33
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %29
49: ; preds = %29
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%51 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
@__const.challenge2.one = [37 x i8] c"1c0111001f010100061a024b53535009181c\00"
@__const.challenge2.two = [37 x i8] c"686974207468652062756c6c277320657965\00"
@.str.1 = [8 x i8] c"xor = '\00"
@str = [14 x i8] c"\0Achallenge 2:\00"
@str.4 = [2 x i8] c"'\00"
define void @challenge2() {
%1 = alloca [37 x i8]
%2 = alloca [37 x i8]
%3 = getelementptr inbounds [37 x i8], [37 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [37 x i8], [37 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%6 = call i32 @counthex(i8* %3)
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @str2hex(i8* %3, i8* %9)
%11 = call i32 @counthex(i8* %4)
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
%14 = call i32 @str2hex(i8* %4, i8* %13)
%15 = call i32 @printhex(i8* %13, i32 %11)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp sgt i32 %11, 0
br i1 %17, label %18, label %29
18: ; preds = %0, %18
%19 = phi i64 [ %27, %18 ], [ 0, %0 ]
%20 = getelementptr inbounds i8, i8* %9, i64 %19
%21 = load i8, i8* %20
%22 = getelementptr inbounds i8, i8* %13, i64 %19
%23 = load i8, i8* %22
%24 = xor i8 %23, %21
%25 = zext i8 %24 to i32
%26 = call i32 @putchar(i32 %25)
%27 = add nuw nsw i64 %19, 1
%28 = icmp eq i64 %27, %12
br i1 %28, label %29, label %18
29: ; preds = %18, %0
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.4, i64 0, i64 0))
ret void
}