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

/* Variables and functions */
int PATH_MAX;
int chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ realpath(char const *, char *);
int seteuid(int /*<<< orphan*/);
int /*<<< orphan*/ strstr(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    return 1;
  }
  const char *path = argv[1];
  char realPath[PATH_MAX];
  if (!realpath(path, realPath)) {
    return 2;
  }
  if (!strstr(realPath, "/private/var/mobile/Media") &&
      !strstr(realPath, "/private/var/mobile/Documents") &&
      !strstr(realPath, "/private/var/mobile/Library/Preferences")) {
    return 3;
  }
  uid_t realUID = getuid();
  if (seteuid(geteuid()) == -1) {
  }
  if (chown(realPath, realUID, realUID) == -1) {
    return 4;
  }
  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)
cmpl   $0x2,-0x8(%rbp)
jge    118c <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    12ad <main+0x14d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <realpath@plt>
cmp    $0x0,%eax
jne    11e9 <main+0x89>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12a6 <main+0x146>
mov    -0x38(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%eax
jne    1247 <main+0xe7>
mov    -0x38(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%eax
jne    1247 <main+0xe7>
mov    -0x38(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%eax
jne    1247 <main+0xe7>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12a6 <main+0x146>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %eax,%edi
call   1080 <seteuid@plt>
cmp    $0xffffffff,%eax
jne    126d <main+0x10d>
jmp    126d <main+0x10d>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1060 <chown@plt>
cmp    $0xffffffff,%eax
jne    1298 <main+0x138>
movl   $0x4,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12a6 <main+0x146>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
mov    $0x1,%eax
cmp    $0x2,%edi
jl     1225 <main+0xc5>
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsp,%r15
mov    0x8(%rsi),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1050 <realpath@plt>
test   %eax,%eax
je     1213 <main+0xb3>
lea    0xe54(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %eax,%eax
jne    11de <main+0x7e>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %eax,%eax
jne    11de <main+0x7e>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %eax,%eax
je     1226 <main+0xc6>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %eax,%edi
call   1080 <seteuid@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r14d,%edx
call   1060 <chown@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
shl    $0x2,%eax
jmp    1218 <main+0xb8>
mov    $0x2,%eax
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%eax
jmp    1218 <main+0xb8>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [26 x i8] c"/private/var/mobile/Media\00"
@.str.1 = [30 x i8] c"/private/var/mobile/Documents\00"
@.str.2 = [40 x i8] c"/private/var/mobile/Library/Preferences\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 i64
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 1, i32* %3
br label %51
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %7
%21 = alloca i8, i64 %19
store i64 %19, i64* %8
%22 = load i8*, i8** %6
%23 = call i32 @realpath(i8* %22, i8* %21)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %14
store i32 2, i32* %3
store i32 1, i32* %9
br label %49
26: ; preds = %14
%27 = call i32 @strstr(i8* %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %26
%30 = call i32 @strstr(i8* %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %29
%33 = call i32 @strstr(i8* %21, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %32
store i32 3, i32* %3
store i32 1, i32* %9
br label %49
36: ; preds = %32, %29, %26
%37 = call i32 (...) @getuid()
store i32 %37, i32* %10
%38 = call i32 (...) @geteuid()
%39 = call i32 @seteuid(i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %36
br label %42
42: ; preds = %41, %36
%43 = load i32, i32* %10
%44 = load i32, i32* %10
%45 = call i32 @chown(i8* %21, i32 %43, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 4, i32* %3
store i32 1, i32* %9
br label %49
48: ; preds = %42
store i32 0, i32* %3
store i32 1, i32* %9
br label %49
49: ; preds = %48, %47, %35, %25
%50 = load i8*, i8** %7
br label %51
51: ; preds = %49, %13
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [26 x i8] c"/private/var/mobile/Media\00"
@.str.1 = [30 x i8] c"/private/var/mobile/Documents\00"
@.str.2 = [40 x i8] c"/private/var/mobile/Library/Preferences\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %31, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @realpath(i8* %6, i8* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %29, label %13
13: ; preds = %4
%14 = call i32 @strstr(i8* %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = call i32 @strstr(i8* %10, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @strstr(i8* %10, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %19, %16, %13
%23 = call i32 (...) @getuid()
%24 = call i32 (...) @geteuid()
%25 = call i32 @seteuid(i32 %24)
%26 = call i32 @chown(i8* %10, i32 %23, i32 %23)
%27 = icmp eq i32 %26, -1
%28 = select i1 %27, i32 4, i32 0
br label %29
29: ; preds = %19, %4, %22
%30 = phi i32 [ %28, %22 ], [ 2, %4 ], [ 3, %19 ]
br label %31
31: ; preds = %2, %29
%32 = phi i32 [ %30, %29 ], [ 1, %2 ]
ret i32 %32
}