#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 */
unsigned char *deobfuscate(unsigned char *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int obfuscate(char *, unsigned char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int main(int argc, char *argv[]) {
  unsigned char *buf, *test;
  int i, obf_len;
  if (argc < 2) {
    printf("Usage: %s <string>\n", argv[0]);
    return 0;
  }
  if (strlen(argv[1]) > 255) {
    printf("String too long\n");
    return 0;
  }
  obf_len = strlen(argv[1]) + 3;
  buf = (unsigned char *)malloc(obf_len);
  memset(buf, 0x00, obf_len);
  obf_len = obfuscate(argv[1], buf);
  obf_len += 3;
  printf("unsigned char obf_string[] = \"");
  for (i = 0; i < obf_len; i++) {
    printf("\\x");
    printf("%02x", buf[i]);
  }
  printf("\"; // \"%s\"\n", argv[1]);
  test = deobfuscate(buf);
  printf("Deobfuscated string: \"%s\"\n", test);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <main+0x159>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
cmp    $0xff,%eax
jle    11e3 <main+0x73>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <main+0x159>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
add    $0x3,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <obfuscate@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x28(%rbp)
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1284 <main+0x114>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1242 <main+0xd2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <deobfuscate@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     11a9 <main+0x29>
mov    (%r14),%rsi
lea    0xe63(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1285 <main+0x105>
mov    0x8(%r14),%rdi
call   1070 <strlen@plt>
cmp    $0x100,%eax
jl     11ca <main+0x4a>
lea    0xea2(%rip),%rdi
call   1060 <puts@plt>
jmp    1285 <main+0x105>
mov    0x8(%r14),%rdi
call   1070 <strlen@plt>
lea    0x3(%rax),%ebx
mov    %ebx,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1090 <memset@plt>
mov    0x8(%r14),%rdi
mov    %r15,%rsi
call   1030 <obfuscate@plt>
mov    %eax,%ebx
lea    0xe10(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
cmp    $0xfffffffe,%ebx
jl     1252 <main+0xd2>
add    $0x2,%ebx
test   %ebx,%ebx
cmovle %r12d,%ebx
add    $0x1,%ebx
lea    0xe10(%rip),%r12
lea    0xe0c(%rip),%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r12,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
movzbl (%r15,%rbp,1),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1230 <main+0xb0>
mov    0x8(%r14),%rsi
lea    0xdde(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15,%rdi
call   1040 <deobfuscate@plt>
lea    0xdd4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15,%rdi
call   1050 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Usage: %s <string>\0A\00"
@.str.1 = [17 x i8] c"String too long\0A\00"
@.str.2 = [31 x i8] c"unsigned char obf_string[] = \22\00"
@.str.3 = [3 x i8] c"\\x\00"
@.str.4 = [5 x i8] c"%02x\00"
@.str.5 = [12 x i8] c"\22; // \22%s\22\0A\00"
@.str.6 = [27 x i8] c"Deobfuscated string: \22%s\22\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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
br label %72
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @strlen(i8* %20)
%22 = icmp sgt i32 %21, 255
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %72
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %29, 3
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = load i32, i32* %9
%36 = call i32 @memset(i8* %34, i32 0, i32 %35)
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = load i8*, i8** %6
%41 = call i32 @obfuscate(i8* %39, i8* %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 3
store i32 %43, i32* %9
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
br label %45
45: ; preds = %58, %25
%46 = load i32, i32* %8
%47 = load i32, i32* %9
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %61
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%51 = load i8*, i8** %6
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %49
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
br label %45
61: ; preds = %45
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %64)
%66 = load i8*, i8** %6
%67 = call i8* @deobfuscate(i8* %66)
store i8* %67, i8** %7
%68 = load i8*, i8** %7
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %68)
%70 = load i8*, i8** %6
%71 = call i32 @free(i8* %70)
store i32 0, i32* %3
br label %72
72: ; preds = %61, %23, %12
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [20 x i8] c"Usage: %s <string>\0A\00"
@.str.2 = [31 x i8] c"unsigned char obf_string[] = \22\00"
@.str.3 = [3 x i8] c"\\x\00"
@.str.4 = [5 x i8] c"%02x\00"
@.str.5 = [12 x i8] c"\22; // \22%s\22\0A\00"
@.str.6 = [27 x i8] c"Deobfuscated string: \22%s\22\0A\00"
@str = [16 x i8] c"String too long\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %45
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strlen(i8* %9)
%11 = icmp sgt i32 %10, 255
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %45
14: ; preds = %7
%15 = load i8*, i8** %8
%16 = tail call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, 3
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @memset(i8* %19, i32 0, i32 %17)
%21 = load i8*, i8** %8
%22 = tail call i32 @obfuscate(i8* %21, i8* %19)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp sgt i32 %22, -3
br i1 %24, label %25, label %39
25: ; preds = %14
%26 = add i32 %22, 2
%27 = call i32 @llvm.smax.i32(i32 %26, i32 0)
%28 = add nuw i32 %27, 1
%29 = zext i32 %28 to i64
br label %30
30: ; preds = %25, %30
%31 = phi i64 [ 0, %25 ], [ %37, %30 ]
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = getelementptr inbounds i8, i8* %19, i64 %31
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = add nuw nsw i64 %31, 1
%38 = icmp eq i64 %37, %29
br i1 %38, label %39, label %30
39: ; preds = %30, %14
%40 = load i8*, i8** %8
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %40)
%42 = tail call i8* @deobfuscate(i8* %19)
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %42)
%44 = tail call i32 @free(i8* %19)
br label %45
45: ; preds = %39, %12, %4
ret i32 0
}