#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 /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  char *ch = (char *)malloc(64);
  memset(ch, 0, 64);
  if (argv[1] != NULL) {
    sprintf(ch, "/etc/init.d/routerctrl %s", argv[1]);
    if (argv[2] != NULL) {
      sprintf(ch, "%s %s", ch, argv[2]);
      if (argv[3] != NULL)
        sprintf(ch, "%s %s", ch, argv[3]);
    }
  }
  setuid(0);
  system(ch);
  free(ch);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x40,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1221 <main+0xc1>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     121c <main+0xbc>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1217 <main+0xb7>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rcx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    121c <main+0xbc>
jmp    1221 <main+0xc1>
xor    %edi,%edi
call   1080 <setuid@plt>
mov    -0x18(%rbp),%rdi
call   1040 <system@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    $0x40,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
mov    0x8(%r14),%rdx
test   %rdx,%rdx
je     11d7 <main+0x77>
lea    0xe6d(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    0x10(%r14),%rcx
test   %rcx,%rcx
je     11d7 <main+0x77>
lea    0xe6d(%rip),%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    0x18(%r14),%rcx
test   %rcx,%rcx
je     11d7 <main+0x77>
lea    0xe50(%rip),%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
xor    %edi,%edi
call   1080 <setuid@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/etc/init.d/routerctrl %s\00"
@.str.1 = [6 x i8] c"%s %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 @malloc(i32 64)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = call i32 @memset(i8* %9, i32 0, i32 64)
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %45
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %44
25: ; preds = %15
%26 = load i8*, i8** %6
%27 = load i8*, i8** %6
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %30)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %43
36: ; preds = %25
%37 = load i8*, i8** %6
%38 = load i8*, i8** %6
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
%42 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %41)
br label %43
43: ; preds = %36, %25
br label %44
44: ; preds = %43, %15
br label %45
45: ; preds = %44, %2
%46 = call i32 @setuid(i32 0)
%47 = load i8*, i8** %6
%48 = call i32 @system(i8* %47)
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
ret i32 0
}

LLVM IR (-O2):
@.str = [26 x i8] c"/etc/init.d/routerctrl %s\00"
@.str.1 = [6 x i8] c"%s %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @malloc(i32 64)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @memset(i8* %4, i32 0, i32 64)
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
br i1 %8, label %21, label %9
9: ; preds = %2
%10 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %7)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = icmp eq i8* %12, null
br i1 %13, label %21, label %14
14: ; preds = %9
%15 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %12)
%16 = getelementptr inbounds i8*, i8** %1, i64 3
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %17)
br label %21
21: ; preds = %9, %19, %14, %2
%22 = tail call i32 @setuid(i32 0)
%23 = tail call i32 @system(i8* %4)
%24 = tail call i32 @free(i8* %4)
ret i32 0
}