#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 */
struct utsname {
  char *sysname;
  char *release;
  char *version;
  char *machine;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int /*<<< orphan*/ uname(struct utsname *);
int main(int argc, char *argv[]) {
  printf("Welcome to the iOS Reverse Engineering Level 3! \n");
  printf("Created by GeoSn0w (@FCE365)\n\n");
  printf("The challenge is to hack this application so that you can use it "
         "without any serial number and obtain the flag! \n\n");
  printf("**************** START **************** \n\n");
  if (argc == 2) {
    printf("Preparing to check access key: %s\n", argv[1]);
    int hex = 0;
    for (int access = 0; access < strlen(argv[1]); access++) {
      hex += (int)argv[1][access];
    }
    if (hex == 1268) {
      printf("Welcome! F.C.E. 365 Tool v36.1\n");
      printf("\n");
      printf("Blimey! You're not a good man! You've hacked the app!\n \n");
      printf("Flag: 0x038948FFFF83--DS \n");
      printf("System Identity:\n");
      struct utsname u;
      uname(&u);
      printf("%s Release %s (Version %s) on %s\n", u.sysname, u.release,
             u.version, u.machine);
      return 0;
    } else {
      printf("The serial number is not correct!\n");
      printf("The application is locked!\n");
    }
  } else {
    printf("This application requires a serial number to work! You can "
           "validate it with <key>\n");
    printf("Or you can do what hackers do the best...\n");
  }
  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)
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xed7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xee8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1297 <main+0x167>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xf58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cmp    %ecx,%eax
jge    11f0 <main+0xc0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ac <main+0x7c>
cmpl   $0x4f4,-0x14(%rbp)
jne    1276 <main+0x146>
lea    0xf0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x38(%rbp),%rdi
call   1050 <uname@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xf35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ba <main+0x18a>
lea    0xf3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b3 <main+0x183>
lea    0xf5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xfa0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xee1(%rip),%rdi
call   1040 <puts@plt>
lea    0xf06(%rip),%rdi
call   1040 <puts@plt>
lea    0xf18(%rip),%rdi
call   1040 <puts@plt>
lea    0xf7e(%rip),%rdi
call   1040 <puts@plt>
cmp    $0x2,%ebp
jne    1247 <main+0xf7>
mov    0x8(%r14),%rsi
lea    0xe5f(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%r14),%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    125c <main+0x10c>
xor    %ebx,%ebx
nopl   (%rax)
mov    0x8(%r14),%rdi
movsbl (%rdi,%rbx,1),%eax
add    %eax,%ebp
add    $0x1,%rbx
call   1050 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11c0 <main+0x70>
cmp    $0x4f4,%ebp
jne    125c <main+0x10c>
lea    0x1000(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x1009(%rip),%rdi
call   1040 <puts@plt>
lea    0x1035(%rip),%rdi
call   1040 <puts@plt>
lea    0x1043(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rdi
call   1070 <uname@plt>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    0x10(%rsp),%rcx
mov    0x18(%rsp),%r8
lea    0xde5(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1274 <main+0x124>
lea    0xee2(%rip),%rdi
call   1040 <puts@plt>
lea    0xf28(%rip),%rdi
jmp    126f <main+0x11f>
lea    0xf49(%rip),%rdi
call   1040 <puts@plt>
lea    0xf5f(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.utsname = type { i8*, i8*, i8*, i8* }
@.str = [50 x i8] c"Welcome to the iOS Reverse Engineering Level 3! \0A\00"
@.str.1 = [31 x i8] c"Created by GeoSn0w (@FCE365)\0A\0A\00"
@.str.2 = [115 x i8] c"The challenge is to hack this application so that you can use it without any serial number and obtain the flag! \0A\0A\00"
@.str.3 = [43 x i8] c"**************** START **************** \0A\0A\00"
@.str.4 = [35 x i8] c"Preparing to check access key: %s\0A\00"
@.str.5 = [32 x i8] c"Welcome! F.C.E. 365 Tool v36.1\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [57 x i8] c"Blimey! You're not a good man! You've hacked the app!\0A \0A\00"
@.str.8 = [27 x i8] c"Flag: 0x038948FFFF83--DS \0A\00"
@.str.9 = [18 x i8] c"System Identity:\0A\00"
@.str.10 = [34 x i8] c"%s Release %s (Version %s) on %s\0A\00"
@.str.11 = [35 x i8] c"The serial number is not correct!\0A\00"
@.str.12 = [28 x i8] c"The application is locked!\0A\00"
@.str.13 = [83 x i8] c"This application requires a serial number to work! You can validate it with <key>\0A\00"
@.str.14 = [43 x i8] c"Or you can do what hackers do the best...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.utsname
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([115 x i8], [115 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %64
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %18)
store i32 0, i32* %6
store i32 0, i32* %7
br label %20
20: ; preds = %38, %15
%21 = load i32, i32* %7
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @strlen(i8* %24)
%26 = icmp slt i32 %21, %25
br i1 %26, label %27, label %41
27: ; preds = %20
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = load i32, i32* %6
%37 = add nsw i32 %36, %35
store i32 %37, i32* %6
br label %38
38: ; preds = %27
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %20
41: ; preds = %20
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 1268
br i1 %43, label %44, label %60
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.7, i64 0, i64 0))
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%50 = call i32 @uname(%struct.utsname* %8)
%51 = getelementptr inbounds %struct.utsname, %struct.utsname* %8, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = getelementptr inbounds %struct.utsname, %struct.utsname* %8, i32 0, i32 1
%54 = load i8*, i8** %53
%55 = getelementptr inbounds %struct.utsname, %struct.utsname* %8, i32 0, i32 2
%56 = load i8*, i8** %55
%57 = getelementptr inbounds %struct.utsname, %struct.utsname* %8, i32 0, i32 3
%58 = load i8*, i8** %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.10, i64 0, i64 0), i8* %52, i8* %54, i8* %56, i8* %58)
store i32 0, i32* %3
br label %68
60: ; preds = %41
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.11, i64 0, i64 0))
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.12, i64 0, i64 0))
br label %63
63: ; preds = %60
br label %67
64: ; preds = %2
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.13, i64 0, i64 0))
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.14, i64 0, i64 0))
br label %67
67: ; preds = %64, %63
store i32 0, i32* %3
br label %68
68: ; preds = %67, %44
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.utsname = type { i8*, i8*, i8*, i8* }
@.str.4 = [35 x i8] c"Preparing to check access key: %s\0A\00"
@.str.10 = [34 x i8] c"%s Release %s (Version %s) on %s\0A\00"
@str = [49 x i8] c"Welcome to the iOS Reverse Engineering Level 3! \00"
@str.15 = [30 x i8] c"Created by GeoSn0w (@FCE365)\0A\00"
@str.16 = [114 x i8] c"The challenge is to hack this application so that you can use it without any serial number and obtain the flag! \0A\00"
@str.17 = [42 x i8] c"**************** START **************** \0A\00"
@str.18 = [82 x i8] c"This application requires a serial number to work! You can validate it with <key>\00"
@str.19 = [42 x i8] c"Or you can do what hackers do the best...\00"
@str.20 = [34 x i8] c"The serial number is not correct!\00"
@str.21 = [27 x i8] c"The application is locked!\00"
@str.22 = [31 x i8] c"Welcome! F.C.E. 365 Tool v36.1\00"
@str.23 = [56 x i8] c"Blimey! You're not a good man! You've hacked the app!\0A \00"
@str.24 = [26 x i8] c"Flag: 0x038948FFFF83--DS \00"
@str.25 = [17 x i8] c"System Identity:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.utsname
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.15, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([114 x i8], [114 x i8]* @str.16, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.17, i64 0, i64 0))
%8 = icmp eq i32 %0, 2
br i1 %8, label %9, label %50
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%13 = load i8*, i8** %10
%14 = tail call i32 @strlen(i8* %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %18, label %47
16: ; preds = %18
%17 = icmp eq i32 %25, 1268
br i1 %17, label %30, label %47
18: ; preds = %9, %18
%19 = phi i64 [ %26, %18 ], [ 0, %9 ]
%20 = phi i32 [ %25, %18 ], [ 0, %9 ]
%21 = load i8*, i8** %10
%22 = getelementptr inbounds i8, i8* %21, i64 %19
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = add nsw i32 %20, %24
%26 = add nuw nsw i64 %19, 1
%27 = tail call i32 @strlen(i8* %21)
%28 = sext i32 %27 to i64
%29 = icmp slt i64 %26, %28
br i1 %29, label %18, label %16
30: ; preds = %16
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.22, i64 0, i64 0))
%32 = tail call i32 @putchar(i32 10)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.23, i64 0, i64 0))
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.24, i64 0, i64 0))
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.25, i64 0, i64 0))
%36 = bitcast %struct.utsname* %3 to i8*
%37 = call i32 @uname(%struct.utsname* %3)
%38 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 0
%39 = load i8*, i8** %38
%40 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 1
%41 = load i8*, i8** %40
%42 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 2
%43 = load i8*, i8** %42
%44 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 3
%45 = load i8*, i8** %44
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.10, i64 0, i64 0), i8* %39, i8* %41, i8* %43, i8* %45)
br label %53
47: ; preds = %9, %16
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.20, i64 0, i64 0))
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.21, i64 0, i64 0))
br label %53
50: ; preds = %2
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @str.18, i64 0, i64 0))
%52 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.19, i64 0, i64 0))
br label %53
53: ; preds = %30, %47, %50
ret i32 0
}