#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*/ SIGINT;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ getpgid(int);
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ rc_usleep(int);
int /*<<< orphan*/ remove(char *);
int kill_existing_instance() {
  FILE *fd;
  int old_pid, i;
  fd = fopen("/var/run/rc_battery_monitor.pid", "r");
  if (fd == NULL) {
    return 0;
  }
  fscanf(fd, "%d", &old_pid);
  fclose(fd);
  if (old_pid == 0) {
    remove("/var/run/rc_battery_monitor.pid");
    return 1;
  }
  kill((pid_t)old_pid, SIGINT);
  for (i = 0; i < 20; i++) {
    if (getpgid(old_pid) >= 0)
      rc_usleep(100000);
    else {
      remove("/var/run/rc_battery_monitor.pid");
      return 1;
    }
  }
  kill((pid_t)old_pid, SIGKILL);
  remove("/var/run/rc_battery_monitor.pid");
  return -1;
}


x86 assembly (clang, -O0):
<kill_existing_instance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <kill_existing_instance+0x36>
movl   $0x0,-0x4(%rbp)
jmp    1277 <kill_existing_instance+0x107>
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11e5 <kill_existing_instance+0x75>
lea    0xe2c(%rip),%rdi
call   1030 <remove@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <kill_existing_instance+0x107>
mov    -0x14(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x14,-0x18(%rbp)
jge    1253 <kill_existing_instance+0xe3>
mov    -0x14(%rbp),%edi
call   1090 <getpgid@plt>
cmp    $0x0,%rax
jl     1228 <kill_existing_instance+0xb8>
mov    $0x186a0,%edi
call   1070 <rc_usleep@plt>
jmp    1240 <kill_existing_instance+0xd0>
lea    0xdd1(%rip),%rdi
call   1030 <remove@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <kill_existing_instance+0x107>
jmp    1245 <kill_existing_instance+0xd5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11fd <kill_existing_instance+0x8d>
mov    -0x14(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
lea    0xd95(%rip),%rdi
call   1030 <remove@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_existing_instance>:
push   %rbp
push   %rbx
push   %rax
lea    0xe86(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1212 <kill_existing_instance+0xa2>
mov    %rax,%rbx
lea    0xe89(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%ebx
test   %edi,%edi
je     1204 <kill_existing_instance+0x94>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    $0x14,%ebp
xchg   %ax,%ax
mov    0x4(%rsp),%edi
call   1090 <getpgid@plt>
test   %rax,%rax
js     1204 <kill_existing_instance+0x94>
mov    $0x186a0,%edi
call   1070 <rc_usleep@plt>
add    $0xffffffff,%ebp
jne    11d0 <kill_existing_instance+0x60>
mov    0x4(%rsp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    $0xffffffff,%ebx
lea    0xdf5(%rip),%rdi
call   1030 <remove@plt>
jmp    1214 <kill_existing_instance+0xa4>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [32 x i8] c"/var/run/rc_battery_monitor.pid\00"
@.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 @kill_existing_instance() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %29, label %5
5: ; preds = %0
%6 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%7 = call i32 @fclose(i32* %3)
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %5
%11 = load i32, i32* @SIGINT
%12 = call i32 @kill(i32 %8, i32 %11)
br label %13
13: ; preds = %10, %18
%14 = phi i32 [ 0, %10 ], [ %20, %18 ]
%15 = load i32, i32* %1
%16 = call i64 @getpgid(i32 %15)
%17 = icmp sgt i64 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = call i32 @rc_usleep(i32 100000)
%20 = add nuw nsw i32 %14, 1
%21 = icmp eq i32 %20, 20
br i1 %21, label %22, label %13
22: ; preds = %18
%23 = load i32, i32* %1
%24 = load i32, i32* @SIGKILL
%25 = call i32 @kill(i32 %23, i32 %24)
br label %26
26: ; preds = %13, %5, %22
%27 = phi i32 [ -1, %22 ], [ 1, %5 ], [ 1, %13 ]
%28 = call i32 @remove(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %26, %0
%30 = phi i32 [ 0, %0 ], [ %27, %26 ]
ret i32 %30
}