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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGKILL;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ getpgid(int);
scalar_t__ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ rc_usleep(int);
int /*<<< orphan*/ remove(char *);
int rc_kill() {
  FILE *fd;
  int old_pid, i;
  if (access("/var/run/robotics_cape.pid", F_OK) != 0) {
    return 0;
  }
  fd = fopen("/var/run/robotics_cape.pid", "r");
  if (fd == NULL)
    return 0;
  fscanf(fd, "%d", &old_pid);
  fclose(fd);
  if (old_pid == 0) {
    remove("/var/run/robotics_cape.pid");
    return -2;
  }
  if (old_pid == (int)getpid())
    return 0;
  if (getpgid(old_pid) < 0) {
    remove("/var/run/robotics_cape.pid");
    return 0;
  }
  kill((pid_t)old_pid, SIGINT);
  for (i = 0; i < 30; i++) {
    if (getpgid(old_pid) >= 0)
      rc_usleep(100000);
    else {
      remove("/var/run/robotics_cape.pid");
      return 1;
    }
  }
  kill((pid_t)old_pid, SIGKILL);
  rc_usleep(500000);
  remove("/var/run/robotics_cape.pid");
  return -1;
}


x86 assembly (clang, -O0):
<rc_kill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1090 <access@plt>
cmp    $0x0,%rax
je     11c3 <rc_kill+0x33>
movl   $0x0,-0x4(%rbp)
jmp    131d <rc_kill+0x18d>
lea    0xe36(%rip),%rdi
lea    0xe4a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f1 <rc_kill+0x61>
movl   $0x0,-0x4(%rbp)
jmp    131d <rc_kill+0x18d>
mov    -0x10(%rbp),%rdi
lea    0xe21(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1230 <rc_kill+0xa0>
lea    0xde1(%rip),%rdi
call   1030 <remove@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    131d <rc_kill+0x18d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jne    1257 <rc_kill+0xc7>
movl   $0x0,-0x4(%rbp)
jmp    131d <rc_kill+0x18d>
mov    -0x14(%rbp),%edi
call   10b0 <getpgid@plt>
cmp    $0x0,%rax
jge    1281 <rc_kill+0xf1>
lea    0xd90(%rip),%rdi
call   1030 <remove@plt>
movl   $0x0,-0x4(%rbp)
jmp    131d <rc_kill+0x18d>
mov    -0x14(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x1e,-0x18(%rbp)
jge    12ef <rc_kill+0x15f>
mov    -0x14(%rbp),%edi
call   10b0 <getpgid@plt>
cmp    $0x0,%rax
jl     12c4 <rc_kill+0x134>
mov    $0x186a0,%edi
call   1080 <rc_usleep@plt>
jmp    12dc <rc_kill+0x14c>
lea    0xd35(%rip),%rdi
call   1030 <remove@plt>
movl   $0x1,-0x4(%rbp)
jmp    131d <rc_kill+0x18d>
jmp    12e1 <rc_kill+0x151>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1299 <rc_kill+0x109>
mov    -0x14(%rbp),%edi
mov    0x2cdf(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
mov    $0x7a120,%edi
call   1080 <rc_usleep@plt>
lea    0xcef(%rip),%rdi
call   1030 <remove@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rc_kill>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1090 <access@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    129c <rc_kill+0x10c>
lea    0xe41(%rip),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     129c <rc_kill+0x10c>
mov    %rax,%r14
lea    0xe3f(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0xc(%rsp),%ebp
test   %ebp,%ebp
je     1280 <rc_kill+0xf0>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %eax,%ebp
je     129c <rc_kill+0x10c>
mov    0xc(%rsp),%edi
call   10b0 <getpgid@plt>
test   %rax,%rax
js     1287 <rc_kill+0xf7>
mov    0xc(%rsp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
mov    $0x1e,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%edi
call   10b0 <getpgid@plt>
test   %rax,%rax
js     128b <rc_kill+0xfb>
mov    $0x186a0,%edi
call   1080 <rc_usleep@plt>
add    $0xffffffff,%ebx
jne    1240 <rc_kill+0xb0>
mov    0xc(%rsp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
mov    $0x7a120,%edi
call   1080 <rc_usleep@plt>
mov    $0xffffffff,%ebx
jmp    1290 <rc_kill+0x100>
mov    $0xfffffffe,%ebx
jmp    1290 <rc_kill+0x100>
xor    %ebx,%ebx
jmp    1290 <rc_kill+0x100>
mov    $0x1,%ebx
lea    0xd69(%rip),%rdi
call   1030 <remove@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"/var/run/robotics_cape.pid\00"
@F_OK = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@SIGINT = global i32 0
@SIGKILL = global i32 0
define i32 @rc_kill() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @F_OK
%6 = call i64 @access(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i32 0, i32* %1
br label %60
9: ; preds = %0
%10 = call i32* @fopen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 0, i32* %1
br label %60
14: ; preds = %9
%15 = load i32*, i32** %2
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%17 = load i32*, i32** %2
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @remove(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %1
br label %60
23: ; preds = %14
%24 = load i32, i32* %3
%25 = call i64 (...) @getpid()
%26 = trunc i64 %25 to i32
%27 = icmp eq i32 %24, %26
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 0, i32* %1
br label %60
29: ; preds = %23
%30 = load i32, i32* %3
%31 = call i64 @getpgid(i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @remove(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %60
35: ; preds = %29
%36 = load i32, i32* %3
%37 = load i32, i32* @SIGINT
%38 = call i32 @kill(i32 %36, i32 %37)
store i32 0, i32* %4
br label %39
39: ; preds = %51, %35
%40 = load i32, i32* %4
%41 = icmp slt i32 %40, 30
br i1 %41, label %42, label %54
42: ; preds = %39
%43 = load i32, i32* %3
%44 = call i64 @getpgid(i32 %43)
%45 = icmp sge i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @rc_usleep(i32 100000)
br label %50
48: ; preds = %42
%49 = call i32 @remove(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %60
50: ; preds = %46
br label %51
51: ; preds = %50
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %4
br label %39
54: ; preds = %39
%55 = load i32, i32* %3
%56 = load i32, i32* @SIGKILL
%57 = call i32 @kill(i32 %55, i32 %56)
%58 = call i32 @rc_usleep(i32 500000)
%59 = call i32 @remove(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %60
60: ; preds = %54, %48, %33, %28, %21, %13, %8
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@.str = [27 x i8] c"/var/run/robotics_cape.pid\00"
@F_OK = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@SIGINT = global i32 0
@SIGKILL = global i32 0
define i32 @rc_kill() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @F_OK
%4 = tail call i64 @access(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %43
6: ; preds = %0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([27 x i8], [27 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 %43, label %9
9: ; preds = %6
%10 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%11 = call i32 @fclose(i32* %7)
%12 = load i32, i32* %1
%13 = icmp eq i32 %12, 0
br i1 %13, label %40, label %14
14: ; preds = %9
%15 = call i64 (...) @getpid()
%16 = trunc i64 %15 to i32
%17 = icmp eq i32 %12, %16
br i1 %17, label %43, label %18
18: ; preds = %14
%19 = load i32, i32* %1
%20 = call i64 @getpgid(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %40, label %22
22: ; preds = %18
%23 = load i32, i32* %1
%24 = load i32, i32* @SIGINT
%25 = call i32 @kill(i32 %23, i32 %24)
br label %26
26: ; preds = %22, %31
%27 = phi i32 [ 0, %22 ], [ %33, %31 ]
%28 = load i32, i32* %1
%29 = call i64 @getpgid(i32 %28)
%30 = icmp sgt i64 %29, -1
br i1 %30, label %31, label %40
31: ; preds = %26
%32 = call i32 @rc_usleep(i32 100000)
%33 = add nuw nsw i32 %27, 1
%34 = icmp eq i32 %33, 30
br i1 %34, label %35, label %26
35: ; preds = %31
%36 = load i32, i32* %1
%37 = load i32, i32* @SIGKILL
%38 = call i32 @kill(i32 %36, i32 %37)
%39 = call i32 @rc_usleep(i32 500000)
br label %40
40: ; preds = %26, %18, %9, %35
%41 = phi i32 [ -1, %35 ], [ -2, %9 ], [ 0, %18 ], [ 1, %26 ]
%42 = call i32 @remove(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %40, %14, %6, %0
%44 = phi i32 [ 0, %0 ], [ 0, %6 ], [ 0, %14 ], [ %41, %40 ]
ret i32 %44
}