#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 */
int EscapeArgs(char *, int, char **);
int /*<<< orphan*/ MemError();
int ParamsLen(int, char **);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ geteuid();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Argument needed\n");
    return 1;
  } else if (argc > 11) {
    printf("Too much arguments\n");
    return 1;
  } else {
    if (!setuid(geteuid())) {
      char *parsed = NULL;
      char *str = NULL;
      int total_len = 0;
      total_len = ParamsLen((argc - 1), &argv[1]);
      parsed = (char *)malloc(total_len + 1);
      if (parsed == NULL)
        MemError();
      total_len = EscapeArgs(parsed, argc - 1, &argv[1]);
      total_len += 47 + 1;
      str = (char *)malloc(total_len);
      if (str == NULL)
        MemError();
      snprintf(str, total_len,
               "/bin/echo '%s > /dev/null 2>&1' | /usr/bin/at now", parsed);
      system(str);
      free(parsed);
      free(str);
    } else {
      printf("Couldn't set UID to effective UID\n");
      return 1;
    }
  }
  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    11d8 <main+0x38>
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x160>
cmpl   $0xb,-0x8(%rbp)
jle    11fa <main+0x5a>
lea    0xe28(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x160>
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    %eax,%edi
call   10c0 <setuid@plt>
cmp    $0x0,%eax
jne    12d7 <main+0x137>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10b0 <ParamsLen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125f <main+0xbf>
mov    $0x0,%al
call   1090 <MemError@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rdx
add    $0x8,%rdx
call   1060 <EscapeArgs@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a0 <main+0x100>
mov    $0x0,%al
call   1090 <MemError@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rcx
lea    0xd73(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <system@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12ef <main+0x14f>
lea    0xd79(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x160>
jmp    12f4 <main+0x154>
jmp    12f9 <main+0x159>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     11b2 <main+0x12>
lea    0xeb7(%rip),%rdi
jmp    11de <main+0x3e>
mov    %edi,%ebp
cmp    $0xc,%edi
jb     11c2 <main+0x22>
lea    0xe94(%rip),%rdi
jmp    11de <main+0x3e>
mov    %rsi,%rbx
xor    %eax,%eax
call   1080 <geteuid@plt>
mov    %eax,%edi
call   10c0 <setuid@plt>
test   %eax,%eax
je     11ed <main+0x4d>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
add    $0xffffffff,%ebp
add    $0x8,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <ParamsLen@plt>
lea    0x1(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1215 <main+0x75>
xor    %eax,%eax
call   1090 <MemError@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1060 <EscapeArgs@plt>
mov    %eax,%ebp
add    $0x30,%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    123d <main+0x9d>
xor    %eax,%eax
call   1090 <MemError@plt>
lea    0xdbc(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rcx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11e8 <main+0x48>

LLVM IR (-O0):
@.str = [17 x i8] c"Argument needed\0A\00"
@.str.1 = [20 x i8] c"Too much arguments\0A\00"
@.str.2 = [50 x i8] c"/bin/echo '%s > /dev/null 2>&1' | /usr/bin/at now\00"
@.str.3 = [35 x i8] c"Couldn't set UID to effective UID\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %68
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 11
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %68
18: ; preds = %13
%19 = call i32 (...) @geteuid()
%20 = call i32 @setuid(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %63, label %22
22: ; preds = %18
store i8* null, i8** %6
store i8* null, i8** %7
store i32 0, i32* %8
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 1
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = call i32 @ParamsLen(i32 %24, i8** %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = call i32 (...) @MemError()
br label %36
36: ; preds = %34, %22
%37 = load i8*, i8** %6
%38 = load i32, i32* %4
%39 = sub nsw i32 %38, 1
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = call i32 @EscapeArgs(i8* %37, i32 %39, i8** %41)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 48
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = call i64 @malloc(i32 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %7
%48 = load i8*, i8** %7
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %36
%51 = call i32 (...) @MemError()
br label %52
52: ; preds = %50, %36
%53 = load i8*, i8** %7
%54 = load i32, i32* %8
%55 = load i8*, i8** %6
%56 = call i32 @snprintf(i8* %53, i32 %54, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %7
%58 = call i32 @system(i8* %57)
%59 = load i8*, i8** %6
%60 = call i32 @free(i8* %59)
%61 = load i8*, i8** %7
%62 = call i32 @free(i8* %61)
br label %65
63: ; preds = %18
%64 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %68
65: ; preds = %52
br label %66
66: ; preds = %65
br label %67
67: ; preds = %66
store i32 0, i32* %3
br label %68
68: ; preds = %67, %63, %16, %11
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str.2 = [50 x i8] c"/bin/echo '%s > /dev/null 2>&1' | /usr/bin/at now\00"
@str = [34 x i8] c"Couldn't set UID to effective UID\00"
@str.4 = [19 x i8] c"Too much arguments\00"
@str.5 = [16 x i8] c"Argument needed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
br label %39
6: ; preds = %2
%7 = icmp ugt i32 %0, 11
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
br label %39
10: ; preds = %6
%11 = tail call i32 (...) @geteuid()
%12 = tail call i32 @setuid(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = add nsw i32 %0, -1
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = tail call i32 @ParamsLen(i32 %15, i8** %16)
%18 = add nsw i32 %17, 1
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = tail call i32 (...) @MemError()
br label %24
24: ; preds = %22, %14
%25 = tail call i32 @EscapeArgs(i8* %20, i32 %15, i8** %16)
%26 = add nsw i32 %25, 48
%27 = tail call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = tail call i32 (...) @MemError()
br label %32
32: ; preds = %30, %24
%33 = tail call i32 @snprintf(i8* %28, i32 %26, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%34 = tail call i32 @system(i8* %28)
%35 = tail call i32 @free(i8* %20)
%36 = tail call i32 @free(i8* %28)
br label %39
37: ; preds = %10
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %39
39: ; preds = %32, %37, %8, %4
%40 = phi i32 [ 1, %4 ], [ 1, %8 ], [ 1, %37 ], [ 0, %32 ]
ret i32 %40
}