#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_str;

/* Variables and functions */
int /*<<< orphan*/ F_TEST;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SIGTERM;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
void kill_previous_instance() {
  int lock_file;
  char pid_str[10];
  int pid, i;
  errno = 0;
  lock_file = open("/tmp/stressgen.lock", O_RDONLY);
  if (0 < lock_file) {
    if (0 != lockf(lock_file, F_TEST, 0)) {
      memset(pid_str, 0, sizeof(pid_str));
      i = read(lock_file, pid_str, sizeof(pid_str));
      if (0 < i) {
        pid = atoi(pid_str);
        if (pid) {
          (void)kill(pid, SIGTERM);
          i = 7;
          while (0 < i) {
            if (0 == lockf(lock_file, F_TEST, 0)) {
              close(lock_file);
              return;
            }
            sleep(1);
            i -= 1;
          }
          close(lock_file);
          printf("Error killing previous instance %d\nPossibly started by "
                 "other user\n",
                 pid);
          exit(1);
        }
      }
      close(lock_file);
      printf("Error reading lock file %s\nTerminating\n",
             "/tmp/stressgen.lock");
      exit(1);
    } else {
    }
  }
}


x86 assembly (clang, -O0):
<kill_previous_instance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e19(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
cmp    -0x4(%rbp),%eax
jge    1301 <kill_previous_instance+0x161>
mov    -0x4(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12f7 <kill_previous_instance+0x157>
lea    -0xe(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jge    12d0 <kill_previous_instance+0x130>
lea    -0xe(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12cb <kill_previous_instance+0x12b>
mov    -0x14(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
movl   $0x7,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jge    12a8 <kill_previous_instance+0x108>
mov    -0x4(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1290 <kill_previous_instance+0xf0>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    1301 <kill_previous_instance+0x161>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1257 <kill_previous_instance+0xb7>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%esi
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12d0 <kill_previous_instance+0x130>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
lea    0xd78(%rip),%rdi
lea    0xd1a(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12fc <kill_previous_instance+0x15c>
jmp    1301 <kill_previous_instance+0x161>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_previous_instance>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e19(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
jle    1302 <kill_previous_instance+0x162>
mov    %eax,%ebx
mov    0x2dfc(%rip),%r14
mov    (%r14),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
je     1302 <kill_previous_instance+0x162>
lea    0x6(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    130b <kill_previous_instance+0x16b>
lea    0x6(%rsp),%rdi
call   10a0 <atoi@plt>
test   %eax,%eax
je     130b <kill_previous_instance+0x16b>
mov    %eax,%ebp
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1080 <kill@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
je     12fb <kill_previous_instance+0x15b>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
je     12fb <kill_previous_instance+0x15b>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
je     12fb <kill_previous_instance+0x15b>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
je     12fb <kill_previous_instance+0x15b>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
je     12fb <kill_previous_instance+0x15b>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
je     12fb <kill_previous_instance+0x15b>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
jne    1331 <kill_previous_instance+0x191>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xd3e(%rip),%rdi
lea    0xce0(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xccb(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@.str = [20 x i8] c"/tmp/stressgen.lock\00"
@O_RDONLY = global i32 0
@F_TEST = global i32 0
@SIGTERM = global i32 0
@.str.1 = [67 x i8] c"Error killing previous instance %d\0APossibly started by other user\0A\00"
@.str.2 = [40 x i8] c"Error reading lock file %s\0ATerminating\0A\00"
define void @kill_previous_instance() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = alloca i32
store i64 0, i64* @errno
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 0, %7
br i1 %8, label %9, label %60
9: ; preds = %0
%10 = load i32, i32* %1
%11 = load i32, i32* @F_TEST
%12 = call i64 @lockf(i32 %10, i32 %11, i32 0)
%13 = icmp ne i64 0, %12
br i1 %13, label %14, label %58
14: ; preds = %9
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 10)
%17 = load i32, i32* %1
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 10)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 0, %20
br i1 %21, label %22, label %53
22: ; preds = %14
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %52
27: ; preds = %22
%28 = load i32, i32* %3
%29 = load i32, i32* @SIGTERM
%30 = call i32 @kill(i32 %28, i32 %29)
store i32 7, i32* %4
br label %31
31: ; preds = %42, %27
%32 = load i32, i32* %4
%33 = icmp slt i32 0, %32
br i1 %33, label %34, label %46
34: ; preds = %31
%35 = load i32, i32* %1
%36 = load i32, i32* @F_TEST
%37 = call i64 @lockf(i32 %35, i32 %36, i32 0)
%38 = icmp eq i64 0, %37
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i32, i32* %1
%41 = call i32 @close(i32 %40)
br label %60
42: ; preds = %34
%43 = call i32 @sleep(i32 1)
%44 = load i32, i32* %4
%45 = sub nsw i32 %44, 1
store i32 %45, i32* %4
br label %31
46: ; preds = %31
%47 = load i32, i32* %1
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %3
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %22
br label %53
53: ; preds = %52, %14
%54 = load i32, i32* %1
%55 = call i32 @close(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %9
br label %59
59: ; preds = %58
br label %60
60: ; preds = %39, %59, %0
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [20 x i8] c"/tmp/stressgen.lock\00"
@O_RDONLY = global i32 0
@F_TEST = global i32 0
@SIGTERM = global i32 0
@.str.1 = [67 x i8] c"Error killing previous instance %d\0APossibly started by other user\0A\00"
@.str.2 = [40 x i8] c"Error reading lock file %s\0ATerminating\0A\00"
define void @kill_previous_instance() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
store i64 0, i64* @errno
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %64
6: ; preds = %0
%7 = load i32, i32* @F_TEST
%8 = tail call i64 @lockf(i32 %4, i32 %7, i32 0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %64, label %10
10: ; preds = %6
%11 = call i32 @memset(i8* %2, i32 0, i32 10)
%12 = call i32 @read(i32 %4, i8* %2, i32 10)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %60
14: ; preds = %10
%15 = call i32 @atoi(i8* %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %60, label %17
17: ; preds = %14
%18 = load i32, i32* @SIGTERM
%19 = call i32 @kill(i32 %15, i32 %18)
%20 = load i32, i32* @F_TEST
%21 = call i64 @lockf(i32 %4, i32 %20, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %50, %45, %40, %35, %30, %25, %17
%24 = call i32 @close(i32 %4)
br label %64
25: ; preds = %17
%26 = call i32 @sleep(i32 1)
%27 = load i32, i32* @F_TEST
%28 = call i64 @lockf(i32 %4, i32 %27, i32 0)
%29 = icmp eq i64 %28, 0
br i1 %29, label %23, label %30
30: ; preds = %25
%31 = call i32 @sleep(i32 1)
%32 = load i32, i32* @F_TEST
%33 = call i64 @lockf(i32 %4, i32 %32, i32 0)
%34 = icmp eq i64 %33, 0
br i1 %34, label %23, label %35
35: ; preds = %30
%36 = call i32 @sleep(i32 1)
%37 = load i32, i32* @F_TEST
%38 = call i64 @lockf(i32 %4, i32 %37, i32 0)
%39 = icmp eq i64 %38, 0
br i1 %39, label %23, label %40
40: ; preds = %35
%41 = call i32 @sleep(i32 1)
%42 = load i32, i32* @F_TEST
%43 = call i64 @lockf(i32 %4, i32 %42, i32 0)
%44 = icmp eq i64 %43, 0
br i1 %44, label %23, label %45
45: ; preds = %40
%46 = call i32 @sleep(i32 1)
%47 = load i32, i32* @F_TEST
%48 = call i64 @lockf(i32 %4, i32 %47, i32 0)
%49 = icmp eq i64 %48, 0
br i1 %49, label %23, label %50
50: ; preds = %45
%51 = call i32 @sleep(i32 1)
%52 = load i32, i32* @F_TEST
%53 = call i64 @lockf(i32 %4, i32 %52, i32 0)
%54 = icmp eq i64 %53, 0
br i1 %54, label %23, label %55
55: ; preds = %50
%56 = call i32 @sleep(i32 1)
%57 = call i32 @close(i32 %4)
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %14, %10
%61 = call i32 @close(i32 %4)
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %0, %6, %23
ret void
}