#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ geteuid();
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ mount(char *, char *, char *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ umount2(char *, int /*<<< orphan*/);
int main(int argc, char **argv, char **env) {
  char ssn[13] = {0};
  FILE *outputFile;
  printf("\n[**] Ratchet - 'Unknown serial' fix for Asus Transformer Prime "
         "TF201.\n");
  printf("[**] (C) 2012 Androidroot.mobi. All rights reserved.\n\n");
  if (geteuid() != 0) {
    printf("[-] This utility needs to be run as root. Ensure you have a root "
           "shell (run mempodroid?) and try again.\n");
    return -1;
  }
  printf("[?] Enter your serial number. This is located on the packaging, and "
         "stickers on your tablet: ");
  fgets(ssn, 13, stdin);
  FILE *cardhu =
      fopen("/sys/devices/platform/cardhu_misc/cardhu_backup_chipid", "rb");
  char chipid[17] = {0};
  fgets(chipid, 17, cardhu);
  fclose(cardhu);
  printf("\n[+] Mounting configuration partition (P5).\n");
  mkdir("/Removable/p5", 0777);
  mount("/dev/block/mmcblk0p5", "/Removable/p5", "vfat", 0, 0);
  printf("[+] Writing serial number.\n");
  outputFile = fopen("/Removable/p5/SSN", "w");
  fwrite(ssn, 1, 12, outputFile);
  fclose(outputFile);
  outputFile = fopen("/Removable/p5/UUID", "w");
  fwrite(chipid, 1, 16, outputFile);
  fclose(outputFile);
  umount2("/Removable/p5", 0);
  printf("[=] Configuration data written, all done! Reboot for the changes to "
         "take effect.\n\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x25(%rbp),%rdi
xor    %esi,%esi
mov    $0xd,%edx
call   1080 <memset@plt>
lea    0xe2f(%rip),%rdi
call   1070 <printf@plt>
lea    0xe6b(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1090 <geteuid@plt>
cmp    $0x0,%rax
je     120b <main+0x6b>
lea    0xe85(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <main+0x1a9>
lea    0xed6(%rip),%rdi
call   1070 <printf@plt>
lea    -0x25(%rbp),%rdi
mov    0x2dc6(%rip),%rax
mov    (%rax),%rdx
mov    $0xd,%esi
call   10a0 <fgets@plt>
lea    0xf10(%rip),%rdi
lea    0xf40(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1080 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x11,%esi
call   10a0 <fgets@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xf08(%rip),%rdi
call   1070 <printf@plt>
lea    0xf29(%rip),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
lea    0xf26(%rip),%rdi
lea    0xf11(%rip),%rsi
lea    0xf2d(%rip),%rdx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1060 <mount@plt>
lea    0xf20(%rip),%rdi
call   1070 <printf@plt>
lea    0xf30(%rip),%rdi
lea    0xf3b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x25(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0xc,%edx
call   10c0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xf0d(%rip),%rdi
lea    0xf04(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   10c0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xe7e(%rip),%rdi
xor    %esi,%esi
call   1040 <umount2@plt>
lea    0xedb(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x0,0x5(%rsp)
movq   $0x0,(%rsp)
lea    0xf21(%rip),%rdi
call   1040 <puts@plt>
lea    0xf5c(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <geteuid@plt>
test   %rax,%rax
je     11fb <main+0x5b>
lea    0x1011(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1323 <main+0x183>
lea    0xdfe(%rip),%rdi
call   1080 <printf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xd,%esi
call   10a0 <fgets@plt>
lea    0xe36(%rip),%rdi
lea    0xe66(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x20(%rsp)
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x11,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xefc(%rip),%rdi
call   1040 <puts@plt>
lea    0xe24(%rip),%r12
mov    %r12,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
lea    0xe1e(%rip),%rdi
lea    0xe2c(%rip),%rdx
mov    %r12,%rsi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <mount@plt>
lea    0xeed(%rip),%rdi
call   1040 <puts@plt>
lea    0xe11(%rip),%rdi
lea    0xe1c(%rip),%r13
mov    %r13,%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0xc,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xdef(%rip),%rdi
mov    %r13,%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <umount2@plt>
lea    0xe8d(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [72 x i8] c"\0A[**] Ratchet - 'Unknown serial' fix for Asus Transformer Prime TF201.\0A\00"
@.str.1 = [55 x i8] c"[**] (C) 2012 Androidroot.mobi. All rights reserved.\0A\0A\00"
@.str.2 = [105 x i8] c"[-] This utility needs to be run as root. Ensure you have a root shell (run mempodroid?) and try again.\0A\00"
@.str.3 = [94 x i8] c"[?] Enter your serial number. This is located on the packaging, and stickers on your tablet: \00"
@stdin = global i32* null
@.str.4 = [55 x i8] c"/sys/devices/platform/cardhu_misc/cardhu_backup_chipid\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.6 = [45 x i8] c"\0A[+] Mounting configuration partition (P5).\0A\00"
@.str.7 = [14 x i8] c"/Removable/p5\00"
@.str.8 = [21 x i8] c"/dev/block/mmcblk0p5\00"
@.str.9 = [5 x i8] c"vfat\00"
@.str.10 = [28 x i8] c"[+] Writing serial number.\0A\00"
@.str.11 = [18 x i8] c"/Removable/p5/SSN\00"
@.str.12 = [2 x i8] c"w\00"
@.str.13 = [19 x i8] c"/Removable/p5/UUID\00"
@.str.14 = [83 x i8] c"[=] Configuration data written, all done! Reboot for the changes to take effect.\0A\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [13 x i8]
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca [17 x i8]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = bitcast [13 x i8]* %8 to i8*
%13 = call i32 @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%15 = call i64 (...) @geteuid()
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @printf(i8* getelementptr inbounds ([105 x i8], [105 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %49
19: ; preds = %3
%20 = call i32 @printf(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.3, i64 0, i64 0))
%21 = getelementptr inbounds [13 x i8], [13 x i8]* %8, i64 0, i64 0
%22 = load i32*, i32** @stdin
%23 = call i32 @fgets(i8* %21, i32 13, i32* %22)
%24 = call i32* @fopen(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %24, i32** %10
%25 = bitcast [17 x i8]* %11 to i8*
%26 = getelementptr inbounds [17 x i8], [17 x i8]* %11, i64 0, i64 0
%27 = load i32*, i32** %10
%28 = call i32 @fgets(i8* %26, i32 17, i32* %27)
%29 = load i32*, i32** %10
%30 = call i32 @fclose(i32* %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 @mkdir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 511)
%33 = call i32 @mount(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i32 0, i32 0)
%34 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%35 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
store i32* %35, i32** %9
%36 = getelementptr inbounds [13 x i8], [13 x i8]* %8, i64 0, i64 0
%37 = load i32*, i32** %9
%38 = call i32 @fwrite(i8* %36, i32 1, i32 12, i32* %37)
%39 = load i32*, i32** %9
%40 = call i32 @fclose(i32* %39)
%41 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.13, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
store i32* %41, i32** %9
%42 = getelementptr inbounds [17 x i8], [17 x i8]* %11, i64 0, i64 0
%43 = load i32*, i32** %9
%44 = call i32 @fwrite(i8* %42, i32 1, i32 16, i32* %43)
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
%47 = call i32 @umount2(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 0)
%48 = call i32 @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.14, i64 0, i64 0))
store i32 0, i32* %4
br label %49
49: ; preds = %19, %17
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str.3 = [94 x i8] c"[?] Enter your serial number. This is located on the packaging, and stickers on your tablet: \00"
@stdin = global i32* null
@.str.4 = [55 x i8] c"/sys/devices/platform/cardhu_misc/cardhu_backup_chipid\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.7 = [14 x i8] c"/Removable/p5\00"
@.str.8 = [21 x i8] c"/dev/block/mmcblk0p5\00"
@.str.9 = [5 x i8] c"vfat\00"
@.str.11 = [18 x i8] c"/Removable/p5/SSN\00"
@.str.12 = [2 x i8] c"w\00"
@.str.13 = [19 x i8] c"/Removable/p5/UUID\00"
@str = [71 x i8] c"\0A[**] Ratchet - 'Unknown serial' fix for Asus Transformer Prime TF201.\00"
@str.15 = [54 x i8] c"[**] (C) 2012 Androidroot.mobi. All rights reserved.\0A\00"
@str.16 = [44 x i8] c"\0A[+] Mounting configuration partition (P5).\00"
@str.17 = [27 x i8] c"[+] Writing serial number.\00"
@str.18 = [82 x i8] c"[=] Configuration data written, all done! Reboot for the changes to take effect.\0A\00"
@str.19 = [104 x i8] c"[-] This utility needs to be run as root. Ensure you have a root shell (run mempodroid?) and try again.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca [13 x i8]
%5 = alloca [17 x i8]
%6 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([71 x i8], [71 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.15, i64 0, i64 0))
%9 = tail call i64 (...) @geteuid()
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([104 x i8], [104 x i8]* @str.19, i64 0, i64 0))
br label %33
13: ; preds = %3
%14 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([94 x i8], [94 x i8]* @.str.3, i64 0, i64 0))
%15 = load i32*, i32** @stdin
%16 = call i32 @fgets(i8* %6, i32 13, i32* %15)
%17 = call i32* @fopen(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%18 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%19 = call i32 @fgets(i8* %18, i32 17, i32* %17)
%20 = call i32 @fclose(i32* %17)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.16, i64 0, i64 0))
%22 = call i32 @mkdir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 511)
%23 = call i32 @mount(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i32 0, i32 0)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.17, i64 0, i64 0))
%25 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%26 = call i32 @fwrite(i8* %6, i32 1, i32 12, i32* %25)
%27 = call i32 @fclose(i32* %25)
%28 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.13, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%29 = call i32 @fwrite(i8* %18, i32 1, i32 16, i32* %28)
%30 = call i32 @fclose(i32* %28)
%31 = call i32 @umount2(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 0)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @str.18, i64 0, i64 0))
br label %33
33: ; preds = %13, %11
%34 = phi i32 [ -1, %11 ], [ 0, %13 ]
ret i32 %34
}