#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 pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int sscanf(char *, char *, int *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ unlink(char *);
void kill_the_rival(void) {
  char rival[64];
  char buf[256];
  pid_t pid;
  FILE *fp;
  int size;
  fp = fopen("/var/run/rasp4you.pid", "r");
  if (fp == NULL)
    return;
  if (fgets(buf, 128, fp) != NULL && sscanf(buf, "%u", &pid) == 1) {
    sprintf(rival, "/proc/%u/exe", pid);
    size = readlink(rival, buf, 128);
    if (size > 0) {
      buf[size] = 0;
      if (strcmp(buf, "/usr/sbin/rasp4you") == 0) {
        if (pid == getpid()) {
          fclose(fp);
          return;
        }
        kill(pid, SIGKILL);
      }
    }
  }
  fclose(fp);
  unlink("/var/run/rasp4you.pid");
  return;
}


x86 assembly (clang, -O0):
<kill_the_rival>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
lea    0xe4e(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x150(%rbp)
cmpq   $0x0,-0x150(%rbp)
jne    11d8 <kill_the_rival+0x38>
jmp    12f1 <kill_the_rival+0x151>
lea    -0x140(%rbp),%rdi
mov    -0x150(%rbp),%rdx
mov    $0x80,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12d9 <kill_the_rival+0x139>
lea    -0x140(%rbp),%rdi
lea    0xe10(%rip),%rsi
lea    -0x144(%rbp),%rdx
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
jne    12d9 <kill_the_rival+0x139>
lea    -0x40(%rbp),%rdi
mov    -0x144(%rbp),%edx
lea    0xded(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    -0x140(%rbp),%rsi
mov    $0x80,%edx
call   1040 <readlink@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0x0,-0x154(%rbp)
jle    12d4 <kill_the_rival+0x134>
movslq -0x154(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12cf <kill_the_rival+0x12f>
mov    -0x144(%rbp),%eax
mov    %eax,-0x158(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%ecx
mov    -0x158(%rbp),%eax
cmp    %ecx,%eax
jne    12bb <kill_the_rival+0x11b>
mov    -0x150(%rbp),%rdi
call   1060 <fclose@plt>
jmp    12f1 <kill_the_rival+0x151>
mov    -0x144(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
jmp    12d4 <kill_the_rival+0x134>
jmp    12d9 <kill_the_rival+0x139>
mov    -0x150(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xd14(%rip),%rdi
call   1030 <unlink@plt>
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_the_rival>:
push   %rbp
push   %r14
push   %rbx
sub    $0x150,%rsp
lea    0xe4e(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     128d <kill_the_rival+0xed>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1279 <kill_the_rival+0xd9>
lea    0xe2c(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
jne    1279 <kill_the_rival+0xd9>
mov    0xc(%rsp),%edx
lea    0xe10(%rip),%rsi
lea    0x110(%rsp),%r14
mov    %r14,%rdi
call   10c0 <sprintf@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    $0x80,%edx
call   1040 <readlink@plt>
test   %eax,%eax
jle    1279 <kill_the_rival+0xd9>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xde9(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1279 <kill_the_rival+0xd9>
mov    0xc(%rsp),%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
cmp    %eax,%ebp
jne    1267 <kill_the_rival+0xc7>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    128d <kill_the_rival+0xed>
mov    0xc(%rsp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xd78(%rip),%rdi
call   1030 <unlink@plt>
add    $0x150,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/var/run/rasp4you.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [13 x i8] c"/proc/%u/exe\00"
@.str.4 = [19 x i8] c"/usr/sbin/rasp4you\00"
@SIGKILL = global i32 0
define void @kill_the_rival() {
%1 = alloca [64 x i8]
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
br label %52
10: ; preds = %0
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = load i32*, i32** %4
%13 = call i32* @fgets(i8* %11, i32 128, i32* %12)
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %48
15: ; preds = %10
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %48
19: ; preds = %15
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%21 = load i32, i32* %3
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%25 = call i32 @readlink(i8* %23, i8* %24, i32 128)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %47
28: ; preds = %19
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %30
store i8 0, i8* %31
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %46
35: ; preds = %28
%36 = load i32, i32* %3
%37 = call i32 (...) @getpid()
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
br label %52
42: ; preds = %35
%43 = load i32, i32* %3
%44 = load i32, i32* @SIGKILL
%45 = call i32 @kill(i32 %43, i32 %44)
br label %46
46: ; preds = %42, %28
br label %47
47: ; preds = %46, %19
br label %48
48: ; preds = %47, %15, %10
%49 = load i32*, i32** %4
%50 = call i32 @fclose(i32* %49)
%51 = call i32 @unlink(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %52
52: ; preds = %48, %39, %9
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"/var/run/rasp4you.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [13 x i8] c"/proc/%u/exe\00"
@.str.4 = [19 x i8] c"/usr/sbin/rasp4you\00"
@SIGKILL = global i32 0
define void @kill_the_rival() {
%1 = alloca [64 x i8]
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %38, label %9
9: ; preds = %0
%10 = call i32* @fgets(i8* %5, i32 128, i32* %7)
%11 = icmp eq i32* %10, null
br i1 %11, label %35, label %12
12: ; preds = %9
%13 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %35
15: ; preds = %12
%16 = load i32, i32* %3
%17 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 @readlink(i8* %4, i8* %5, i32 128)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %35
20: ; preds = %15
%21 = zext i32 %18 to i64
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %20
%26 = load i32, i32* %3
%27 = call i32 (...) @getpid()
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @fclose(i32* %7)
br label %38
31: ; preds = %25
%32 = load i32, i32* %3
%33 = load i32, i32* @SIGKILL
%34 = call i32 @kill(i32 %32, i32 %33)
br label %35
35: ; preds = %15, %31, %20, %12, %9
%36 = call i32 @fclose(i32* %7)
%37 = call i32 @unlink(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %0, %35, %29
ret void
}