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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ isalnum(char const);
int rand();
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, const char **argv) {
  if (argc != 2) {
    fprintf(stderr, "usage: rich-core-helper report_label\n");
    return EXIT_FAILURE;
  }
  const char *label = argv[1];
  size_t label_len = strlen(label);
  if (label_len == 0) {
    goto invalid_label;
  }
  size_t i;
  for (i = 0; i != label_len; ++i) {
    if (!isalnum(label[i]) && !(label[i] == '_') && !(label[i] == '-')) {
      goto invalid_label;
    }
  }
  srand(time(NULL));
  int randomid = rand();
  char command[128];
  snprintf(command, sizeof(command),
           "/usr/sbin/rich-core-dumper --name=%s --signal=%d", label, randomid);
  setuid(0);
  char *args[] = {"/bin/env", "-i", "/bin/bash", "-c", command, NULL};
  if (execvp(args[0], args)) {
    fprintf(stderr, "Couldn't invoke rich-core-dumper\n");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
invalid_label:
  fprintf(stderr, "Invalid crash report label: %s\n", label);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11db <main+0x4b>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1388 <main+0x1f8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1204 <main+0x74>
jmp    1361 <main+0x1d1>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1279 <main+0xe9>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%eax
jne    1263 <main+0xd3>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5f,%eax
je     1263 <main+0xd3>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2d,%eax
je     1263 <main+0xd3>
jmp    1361 <main+0x1d1>
jmp    1268 <main+0xd8>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    120c <main+0x7c>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x0,%al
call   10b0 <rand@plt>
mov    %eax,-0x2c(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
mov    $0x80,%esi
lea    0xd78(%rip),%rdx
call   1050 <snprintf@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
lea    0xd96(%rip),%rax
mov    %rax,-0xe0(%rbp)
lea    0xd91(%rip),%rax
mov    %rax,-0xd8(%rbp)
lea    0xd86(%rip),%rax
mov    %rax,-0xd0(%rbp)
lea    0xd82(%rip),%rax
mov    %rax,-0xc8(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
movq   $0x0,-0xb8(%rbp)
mov    -0xe0(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
call   1090 <execvp@plt>
cmp    $0x0,%rax
je     1350 <main+0x1c0>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1388 <main+0x1f8>
mov    0x2c79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1388 <main+0x1f8>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd1d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xb0,%rsp
cmp    $0x2,%edi
jne    1203 <main+0x73>
mov    0x8(%rsi),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     11e4 <main+0x54>
mov    %rax,%r15
xor    %ebx,%ebx
jmp    11c9 <main+0x39>
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    %rbx,%r15
je     1218 <main+0x88>
movsbl (%r14,%rbx,1),%edi
call   1040 <isalnum@plt>
test   %eax,%eax
jne    11c0 <main+0x30>
movzbl (%r14,%rbx,1),%eax
cmp    $0x5f,%al
je     11c0 <main+0x30>
cmp    $0x2d,%al
je     11c0 <main+0x30>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12b4 <main+0x124>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
jmp    12ad <main+0x11d>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
lea    0xdf2(%rip),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rcx
mov    %eax,%r8d
call   1050 <snprintf@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
lea    0xdfd(%rip),%rdi
mov    %rdi,(%rsp)
lea    0xdfb(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xdf2(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xdf0(%rip),%rax
mov    %rax,0x18(%rsp)
mov    %rbx,0x20(%rsp)
movq   $0x0,0x28(%rsp)
mov    %rsp,%rsi
call   1090 <execvp@plt>
test   %rax,%rax
je     12ca <main+0x13a>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2cff(%rip),%rax
jmp    12bb <main+0x12b>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"usage: rich-core-helper report_label\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [49 x i8] c"/usr/sbin/rich-core-dumper --name=%s --signal=%d\00"
@.str.2 = [9 x i8] c"/bin/env\00"
@.str.3 = [3 x i8] c"-i\00"
@.str.4 = [10 x i8] c"/bin/bash\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [34 x i8] c"Couldn't invoke rich-core-dumper\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.7 = [32 x i8] c"Invalid crash report label: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca [128 x i8]
%11 = alloca [6 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %3
br label %90
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %7
%24 = load i64, i64* %7
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %18
br label %85
27: ; preds = %18
store i64 0, i64* %8
br label %28
28: ; preds = %55, %27
%29 = load i64, i64* %8
%30 = load i64, i64* %7
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %58
32: ; preds = %28
%33 = load i8*, i8** %6
%34 = load i64, i64* %8
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = load i8, i8* %35
%37 = call i32 @isalnum(i8 signext %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %54, label %39
39: ; preds = %32
%40 = load i8*, i8** %6
%41 = load i64, i64* %8
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 95
br i1 %45, label %54, label %46
46: ; preds = %39
%47 = load i8*, i8** %6
%48 = load i64, i64* %8
%49 = getelementptr inbounds i8, i8* %47, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 45
br i1 %52, label %54, label %53
53: ; preds = %46
br label %85
54: ; preds = %46, %39, %32
br label %55
55: ; preds = %54
%56 = load i64, i64* %8
%57 = add i64 %56, 1
store i64 %57, i64* %8
br label %28
58: ; preds = %28
%59 = call i32 @time(i32* null)
%60 = call i32 @srand(i32 %59)
%61 = call i32 (...) @rand()
store i32 %61, i32* %9
%62 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%63 = load i8*, i8** %6
%64 = load i32, i32* %9
%65 = call i32 @snprintf(i8* %62, i32 128, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %63, i32 %64)
%66 = call i32 @setuid(i32 0)
%67 = getelementptr inbounds [6 x i8*], [6 x i8*]* %11, i64 0, i64 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %67
%68 = getelementptr inbounds i8*, i8** %67, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %68
%69 = getelementptr inbounds i8*, i8** %68, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %69
%70 = getelementptr inbounds i8*, i8** %69, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8** %70
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
store i8* %72, i8** %71
%73 = getelementptr inbounds i8*, i8** %71, i64 1
store i8* null, i8** %73
%74 = getelementptr inbounds [6 x i8*], [6 x i8*]* %11, i64 0, i64 0
%75 = load i8*, i8** %74
%76 = getelementptr inbounds [6 x i8*], [6 x i8*]* %11, i64 0, i64 0
%77 = call i64 @execvp(i8* %75, i8** %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %83
79: ; preds = %58
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%82 = load i32, i32* @EXIT_FAILURE
store i32 %82, i32* %3
br label %90
83: ; preds = %58
%84 = load i32, i32* @EXIT_SUCCESS
store i32 %84, i32* %3
br label %90
85: ; preds = %53, %26
%86 = load i32, i32* @stderr
%87 = load i8*, i8** %6
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %87)
%89 = load i32, i32* @EXIT_FAILURE
store i32 %89, i32* %3
br label %90
90: ; preds = %85, %83, %79, %14
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"usage: rich-core-helper report_label\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [49 x i8] c"/usr/sbin/rich-core-dumper --name=%s --signal=%d\00"
@.str.2 = [9 x i8] c"/bin/env\00"
@.str.3 = [3 x i8] c"-i\00"
@.str.4 = [10 x i8] c"/bin/bash\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [34 x i8] c"Couldn't invoke rich-core-dumper\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.7 = [32 x i8] c"Invalid crash report label: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = alloca [6 x i8*]
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %46
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %43, label %14
14: ; preds = %9, %22
%15 = phi i64 [ %23, %22 ], [ 0, %9 ]
%16 = getelementptr inbounds i8, i8* %11, i64 %15
%17 = load i8, i8* %16
%18 = tail call i32 @isalnum(i8 signext %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i8, i8* %16
switch i8 %21, label %43 [
i8 95, label %22
i8 45, label %22
]
22: ; preds = %20, %20, %14
%23 = add nuw i64 %15, 1
%24 = icmp eq i64 %23, %12
br i1 %24, label %25, label %14
25: ; preds = %22
%26 = tail call i32 @time(i32* null)
%27 = tail call i32 @srand(i32 %26)
%28 = tail call i32 (...) @rand()
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%30 = call i32 @snprintf(i8* %29, i32 128, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 %28)
%31 = call i32 @setuid(i32 0)
%32 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %32
%33 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %33
%34 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %34
%35 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8** %35
%36 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 4
store i8* %29, i8** %36
%37 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 5
store i8* null, i8** %37
%38 = call i64 @execvp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %32)
%39 = icmp eq i64 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %25
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
br label %46
43: ; preds = %20, %9
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %11)
br label %46
46: ; preds = %40, %43, %25, %6
%47 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_FAILURE, %43 ], [ @EXIT_FAILURE, %40 ], [ @EXIT_SUCCESS, %25 ]
%48 = load i32, i32* %47
ret i32 %48
}