#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 char uint8_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
int strlen(char *);
uint8_t *xorstr(char *str, char *key) {
  int keylen;
  int keyoffset;
  int i;
  uint8_t *outbuf;
  outbuf = (uint8_t *)malloc(strlen(str) + 1);
  if (outbuf == NULL) {
    perror("can't allocate memory");
    exit(1);
  }
  memset(outbuf, 0x00, (strlen(str) + 1));
  keylen = strlen(key);
  keyoffset = 0;
  for (i = 0; i < strlen(str); i++) {
    outbuf[i] = str[i] ^ key[keyoffset++];
    if (keyoffset == keylen) {
      keyoffset = 0;
    }
  }
  return (outbuf);
}


x86 assembly (clang, -O0):
<xorstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1198 <xorstr+0x48>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
add    $0x1,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jge    1246 <xorstr+0xf6>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
mov    %edx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %edx,%rdx
movsbl (%rcx,%rdx,1),%ecx
xor    %ecx,%eax
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1233 <xorstr+0xe3>
movl   $0x0,-0x18(%rbp)
jmp    1238 <xorstr+0xe8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d3 <xorstr+0x83>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xorstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11f4 <xorstr+0xa4>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11e2 <xorstr+0x92>
xor    %ebp,%ebp
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
movslq %r12d,%rax
add    $0x1,%r12d
movzbl (%r14,%rax,1),%eax
xor    (%rbx,%rbp,1),%al
mov    %al,(%r15,%rbp,1)
cmp    %r13d,%r12d
mov    $0x0,%eax
cmove  %eax,%r12d
add    $0x1,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     11b0 <xorstr+0x60>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"can't allocate memory\00"
define i8* @xorstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8*, i8** %8
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* %21)
%23 = add nsw i32 %22, 1
%24 = call i32 @memset(i8* %20, i32 0, i32 %23)
%25 = load i8*, i8** %4
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %27
27: ; preds = %57, %19
%28 = load i32, i32* %7
%29 = load i8*, i8** %3
%30 = call i32 @strlen(i8* %29)
%31 = icmp slt i32 %28, %30
br i1 %31, label %32, label %60
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = load i8*, i8** %4
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = xor i32 %38, %45
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %8
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 %47, i8* %51
%52 = load i32, i32* %6
%53 = load i32, i32* %5
%54 = icmp eq i32 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %32
store i32 0, i32* %6
br label %56
56: ; preds = %55, %32
br label %57
57: ; preds = %56
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
store i32 %59, i32* %7
br label %27
60: ; preds = %27
%61 = load i8*, i8** %8
ret i8* %61
}

LLVM IR (-O2):
@.str = [22 x i8] c"can't allocate memory\00"
define i8* @xorstr(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 @strlen(i8* %0)
%13 = add nsw i32 %12, 1
%14 = tail call i32 @memset(i8* %6, i32 0, i32 %13)
%15 = tail call i32 @strlen(i8* %1)
%16 = tail call i32 @strlen(i8* %0)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %11, %18
%19 = phi i64 [ %31, %18 ], [ 0, %11 ]
%20 = phi i32 [ %30, %18 ], [ 0, %11 ]
%21 = getelementptr inbounds i8, i8* %0, i64 %19
%22 = load i8, i8* %21
%23 = add nsw i32 %20, 1
%24 = sext i32 %20 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = load i8, i8* %25
%27 = xor i8 %26, %22
%28 = getelementptr inbounds i8, i8* %6, i64 %19
store i8 %27, i8* %28
%29 = icmp eq i32 %23, %15
%30 = select i1 %29, i32 0, i32 %23
%31 = add nuw nsw i64 %19, 1
%32 = tail call i32 @strlen(i8* %0)
%33 = sext i32 %32 to i64
%34 = icmp slt i64 %31, %33
br i1 %34, label %18, label %35
35: ; preds = %18, %11
ret i8* %6
}