#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*/ time_t;
struct proc {
  int dummy;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ atexit(int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ atol(int /*<<< orphan*/);
int /*<<< orphan*/ closelog;
int /*<<< orphan*/ generate_coredump(char const *);
int /*<<< orphan*/ generate_report(char const *, struct proc *,
                                   int /*<<< orphan*/);
char *generate_tag(int /*<<< orphan*/);
int /*<<< orphan*/ install(char *, int);
int /*<<< orphan*/ open_std_output_streams();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
struct proc *proc_attach(int /*<<< orphan*/, int /*<<< orphan*/, int, int, int);
int /*<<< orphan*/ proc_detach(struct proc *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink_old_reports();
int /*<<< orphan*/ unwind(struct proc *);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  struct proc *p;
  int enable_coredump = 0;
  pid_t tid;
  pid_t pid;
  int sig;
  int uid;
  int gid;
  const char *tag;
  time_t t;
  if (argc >= 2 && !strcmp(argv[1], "--install")) {
    if (argc >= 3)
      enable_coredump = atoi(argv[2]);
    install(argv[0], enable_coredump);
    return EXIT_SUCCESS;
  } else if (argc == 2 && !strcmp(argv[1], "--version")) {
    puts("0"
         "."
         "2"
         "."
         "0");
    return EXIT_SUCCESS;
  } else if (argc != 7) {
    usage();
    return EXIT_FAILURE;
  }
  t = time(NULL);
  open_std_output_streams();
  openlog("corehandler", 0, LOG_DAEMON);
  (void)atexit(closelog);
  ++argv;
  enable_coredump = atoi(*argv++);
  tid = atol(*argv++);
  pid = atol(*argv++);
  sig = atoi(*argv++);
  uid = atoi(*argv++);
  gid = atoi(*argv++);
  tag = generate_tag(pid);
  if (enable_coredump)
    generate_coredump(tag);
  p = proc_attach(tid, pid, sig, uid, gid);
  unwind(p);
  generate_report(tag, p, t);
  proc_detach(p);
  unlink_old_reports();
  return EXIT_SUCCESS;
}


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)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     128e <main+0x7e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdba(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    128e <main+0x7e>
cmpl   $0x3,-0x8(%rbp)
jl     126e <main+0x5e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <install@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    144f <main+0x23f>
cmpl   $0x2,-0x8(%rbp)
jne    12d2 <main+0xc2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd63(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    12d2 <main+0xc2>
lea    0xd58(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    144f <main+0x23f>
cmpl   $0x7,-0x8(%rbp)
je     12f4 <main+0xe4>
mov    $0x0,%al
call   10b0 <usage@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    144f <main+0x23f>
jmp    12f9 <main+0xe9>
jmp    12fe <main+0xee>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <time@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1070 <open_std_output_streams@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edx
lea    0xcf9(%rip),%rdi
xor    %esi,%esi
call   1120 <openlog@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   1460 <atexit>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rax
mov    %eax,%edi
call   10d0 <atol@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rax
mov    %eax,%edi
call   10d0 <atol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1130 <generate_tag@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1401 <main+0x1f1>
mov    -0x38(%rbp),%rdi
call   1050 <generate_coredump@plt>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
call   10c0 <proc_attach@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <unwind@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1100 <generate_report@plt>
mov    -0x18(%rbp),%rdi
call   10e0 <proc_detach@plt>
mov    $0x0,%al
call   1030 <unlink_old_reports@plt>
mov    0x2b7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret
nopl   0x0(%rax,%rax,1)

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x2,%edi
jl     123d <main+0x2d>
mov    0x8(%rbx),%rdi
lea    0xdd0(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1343 <main+0x133>
cmp    $0x7,%ebp
je     1272 <main+0x62>
cmp    $0x2,%ebp
jne    125f <main+0x4f>
mov    0x8(%rbx),%rdi
lea    0xdb8(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     135f <main+0x14f>
xor    %eax,%eax
call   10b0 <usage@plt>
mov    0x2d63(%rip),%rax
jmp    1372 <main+0x162>
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1070 <open_std_output_streams@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
lea    0xd86(%rip),%rdi
xor    %esi,%esi
call   1120 <openlog@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1390 <atexit>
mov    0x8(%rbx),%rdi
call   1110 <atoi@plt>
mov    %eax,%r12d
mov    0x10(%rbx),%edi
call   10d0 <atol@plt>
mov    %eax,(%rsp)
mov    0x18(%rbx),%edi
call   10d0 <atol@plt>
mov    %eax,%ebp
mov    0x20(%rbx),%rdi
call   1110 <atoi@plt>
mov    %eax,%r13d
mov    0x28(%rbx),%rdi
call   1110 <atoi@plt>
mov    %eax,%r14d
mov    0x30(%rbx),%rdi
call   1110 <atoi@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1130 <generate_tag@plt>
mov    %rax,%rbx
test   %r12d,%r12d
je     1305 <main+0xf5>
mov    %rbx,%rdi
call   1050 <generate_coredump@plt>
mov    (%rsp),%edi
mov    %ebp,%esi
mov    %r13d,%edx
mov    %r14d,%ecx
mov    %r15d,%r8d
call   10c0 <proc_attach@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <unwind@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    0x4(%rsp),%edx
call   1100 <generate_report@plt>
mov    %rbp,%rdi
call   10e0 <proc_detach@plt>
xor    %eax,%eax
call   1030 <unlink_old_reports@plt>
jmp    136b <main+0x15b>
xor    %esi,%esi
cmp    $0x3,%ebp
jb     1355 <main+0x145>
mov    0x10(%rbx),%rdi
call   1110 <atoi@plt>
mov    %eax,%esi
mov    (%rbx),%rdi
call   1080 <install@plt>
jmp    136b <main+0x15b>
lea    0xcae(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)

LLVM IR (-O0):
%struct.proc = type { i32 }
@.str = [10 x i8] c"--install\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [10 x i8] c"--version\00"
@.str.2 = [6 x i8] c"0.2.0\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"corehandler\00"
@LOG_DAEMON = global i32 0
@closelog = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.proc*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%15 = load i32, i32* %4
%16 = icmp sge i32 %15, 2
br i1 %16, label %17, label %38
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @strcmp(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%22 = icmp ne i32 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %17
%24 = load i32, i32* %4
%25 = icmp sge i32 %24, 3
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %7
br label %31
31: ; preds = %26, %23
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i32, i32* %7
%36 = call i32 @install(i8* %34, i32 %35)
%37 = load i32, i32* @EXIT_SUCCESS
store i32 %37, i32* %3
br label %117
38: ; preds = %17, %2
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %50
41: ; preds = %38
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 @strcmp(i8* %44, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%46 = icmp ne i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %41
%48 = call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* @EXIT_SUCCESS
store i32 %49, i32* %3
br label %117
50: ; preds = %41, %38
%51 = load i32, i32* %4
%52 = icmp ne i32 %51, 7
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = call i32 (...) @usage()
%55 = load i32, i32* @EXIT_FAILURE
store i32 %55, i32* %3
br label %117
56: ; preds = %50
br label %57
57: ; preds = %56
br label %58
58: ; preds = %57
%59 = call i32 @time(i32* null)
store i32 %59, i32* %14
%60 = call i32 (...) @open_std_output_streams()
%61 = load i32, i32* @LOG_DAEMON
%62 = call i32 @openlog(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %61)
%63 = load i32, i32* @closelog
%64 = call i32 @atexit(i32 %63)
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i32 1
store i8** %66, i8*** %5
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i32 1
store i8** %68, i8*** %5
%69 = load i8*, i8** %67
%70 = call i32 @atoi(i8* %69)
store i32 %70, i32* %7
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i32 1
store i8** %72, i8*** %5
%73 = load i8*, i8** %71
%74 = ptrtoint i8* %73 to i32
%75 = call i32 @atol(i32 %74)
store i32 %75, i32* %8
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i32 1
store i8** %77, i8*** %5
%78 = load i8*, i8** %76
%79 = ptrtoint i8* %78 to i32
%80 = call i32 @atol(i32 %79)
store i32 %80, i32* %9
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i32 1
store i8** %82, i8*** %5
%83 = load i8*, i8** %81
%84 = call i32 @atoi(i8* %83)
store i32 %84, i32* %10
%85 = load i8**, i8*** %5
%86 = getelementptr inbounds i8*, i8** %85, i32 1
store i8** %86, i8*** %5
%87 = load i8*, i8** %85
%88 = call i32 @atoi(i8* %87)
store i32 %88, i32* %11
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i32 1
store i8** %90, i8*** %5
%91 = load i8*, i8** %89
%92 = call i32 @atoi(i8* %91)
store i32 %92, i32* %12
%93 = load i32, i32* %9
%94 = call i8* @generate_tag(i32 %93)
store i8* %94, i8** %13
%95 = load i32, i32* %7
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %100
97: ; preds = %58
%98 = load i8*, i8** %13
%99 = call i32 @generate_coredump(i8* %98)
br label %100
100: ; preds = %97, %58
%101 = load i32, i32* %8
%102 = load i32, i32* %9
%103 = load i32, i32* %10
%104 = load i32, i32* %11
%105 = load i32, i32* %12
%106 = call %struct.proc* @proc_attach(i32 %101, i32 %102, i32 %103, i32 %104, i32 %105)
store %struct.proc* %106, %struct.proc** %6
%107 = load %struct.proc*, %struct.proc** %6
%108 = call i32 @unwind(%struct.proc* %107)
%109 = load i8*, i8** %13
%110 = load %struct.proc*, %struct.proc** %6
%111 = load i32, i32* %14
%112 = call i32 @generate_report(i8* %109, %struct.proc* %110, i32 %111)
%113 = load %struct.proc*, %struct.proc** %6
%114 = call i32 @proc_detach(%struct.proc* %113)
%115 = call i32 (...) @unlink_old_reports()
%116 = load i32, i32* @EXIT_SUCCESS
store i32 %116, i32* %3
br label %117
117: ; preds = %100, %53, %47, %31
%118 = load i32, i32* %3
ret i32 %118
}

LLVM IR (-O2):
%struct.proc = type { i32 }
@.str = [10 x i8] c"--install\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [10 x i8] c"--version\00"
@.str.2 = [6 x i8] c"0.2.0\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"corehandler\00"
@LOG_DAEMON = global i32 0
@closelog = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %19
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @strcmp(i8* dereferenceable(1) %6, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %4
%10 = icmp ugt i32 %0, 2
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
br label %15
15: ; preds = %11, %9
%16 = phi i32 [ %14, %11 ], [ 0, %9 ]
%17 = load i8*, i8** %1
%18 = tail call i32 @install(i8* %17, i32 %16)
br label %68
19: ; preds = %4, %2
switch i32 %0, label %27 [
i32 2, label %20
i32 7, label %29
]
20: ; preds = %19
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i32 @strcmp(i8* dereferenceable(1) %22, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %68
27: ; preds = %20, %19
%28 = tail call i32 (...) @usage()
br label %68
29: ; preds = %19
%30 = tail call i32 @time(i32* null)
%31 = tail call i32 (...) @open_std_output_streams()
%32 = load i32, i32* @LOG_DAEMON
%33 = tail call i32 @openlog(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %32)
%34 = load i32, i32* @closelog
%35 = tail call i32 @atexit(i32 %34)
%36 = getelementptr inbounds i8*, i8** %1, i64 1
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %36
%39 = tail call i32 @atoi(i8* %38)
%40 = getelementptr inbounds i8*, i8** %1, i64 3
%41 = load i8*, i8** %37
%42 = ptrtoint i8* %41 to i64
%43 = trunc i64 %42 to i32
%44 = tail call i32 @atol(i32 %43)
%45 = getelementptr inbounds i8*, i8** %1, i64 4
%46 = load i8*, i8** %40
%47 = ptrtoint i8* %46 to i64
%48 = trunc i64 %47 to i32
%49 = tail call i32 @atol(i32 %48)
%50 = getelementptr inbounds i8*, i8** %1, i64 5
%51 = load i8*, i8** %45
%52 = tail call i32 @atoi(i8* %51)
%53 = getelementptr inbounds i8*, i8** %1, i64 6
%54 = load i8*, i8** %50
%55 = tail call i32 @atoi(i8* %54)
%56 = load i8*, i8** %53
%57 = tail call i32 @atoi(i8* %56)
%58 = tail call i8* @generate_tag(i32 %49)
%59 = icmp eq i32 %39, 0
br i1 %59, label %62, label %60
60: ; preds = %29
%61 = tail call i32 @generate_coredump(i8* %58)
br label %62
62: ; preds = %60, %29
%63 = tail call %struct.proc* @proc_attach(i32 %44, i32 %49, i32 %52, i32 %55, i32 %57)
%64 = tail call i32 @unwind(%struct.proc* %63)
%65 = tail call i32 @generate_report(i8* %58, %struct.proc* %63, i32 %30)
%66 = tail call i32 @proc_detach(%struct.proc* %63)
%67 = tail call i32 (...) @unlink_old_reports()
br label %68
68: ; preds = %62, %27, %25, %15
%69 = phi i32* [ @EXIT_FAILURE, %27 ], [ @EXIT_SUCCESS, %62 ], [ @EXIT_SUCCESS, %25 ], [ @EXIT_SUCCESS, %15 ]
%70 = load i32, i32* %69
ret i32 %70
}