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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int unlink(char *);
int main(void) {
  char a[10];
  pid_t pid;
  FILE *fd;
  int link;
  if (!(fd = freopen("/var/run/labb2.pid", "r+", stdin))) {
    syslog(LOG_ERR, "unable to open file, code %d (%s)", errno,
           strerror(errno));
    exit(1);
  }
  while (fgets(a, 10, fd)) {
    pid = atoi(a);
    if ((kill(pid, SIGTERM)))
      exit(0);
    memset(a, '\0', sizeof(a));
  }
  fclose(fd);
  if ((link = unlink("/var/run/labb2.pid")) == -1) {
    syslog(LOG_ERR, "unable to unlink file, code %d (%s)", errno,
           strerror(errno));
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
lea    0xe41(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1060 <freopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    121d <main+0x7d>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
lea    0xe08(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1222 <main+0x82>
lea    -0xe(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0xa,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1281 <main+0xe1>
lea    -0xe(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
cmp    $0x0,%rax
je     126c <main+0xcc>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    -0xe(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
jmp    1222 <main+0x82>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd6f(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    12e6 <main+0x146>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%edx
mov    %eax,%ecx
lea    0xd61(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
lea    0xe48(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1060 <freopen@plt>
test   %rax,%rax
je     1266 <main+0xc6>
mov    %rax,%r14
lea    0x6(%rsp),%rdi
mov    $0xa,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     123b <main+0x9b>
lea    0x6(%rsp),%rbx
mov    0x2df5(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   10a0 <atoi@plt>
mov    0x0(%rbp),%esi
mov    %eax,%edi
call   1080 <kill@plt>
test   %rax,%rax
jne    125f <main+0xbf>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1200 <main+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdb6(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0xe8>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xd90(%rip),%rsi
jmp    12a8 <main+0x108>
mov    0x2d31(%rip),%rax
mov    (%rax),%ebx
mov    0x2d38(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xd90(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %eax,%ecx
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/var/run/labb2.pid\00"
@.str.1 = [3 x i8] c"r+\00"
@stdin = global i32 0
@LOG_ERR = global i32 0
@.str.2 = [34 x i8] c"unable to open file, code %d (%s)\00"
@errno = global i32 0
@SIGTERM = global i32 0
@.str.3 = [36 x i8] c"unable to unlink file, code %d (%s)\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @stdin
%7 = call i32* @freopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32* %7, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %16, label %9
9: ; preds = %0
%10 = load i32, i32* @LOG_ERR
%11 = load i32, i32* @errno
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @syslog(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
br label %17
17: ; preds = %31, %16
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%19 = load i32*, i32** %4
%20 = call i64 @fgets(i8* %18, i32 10, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %17
%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 = load i32, i32* @SIGTERM
%27 = call i64 @kill(i32 %25, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %22
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i8 signext 0, i32 10)
br label %17
34: ; preds = %17
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
%37 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 %37, i32* %5
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %34
%40 = load i32, i32* @LOG_ERR
%41 = load i32, i32* @errno
%42 = load i32, i32* @errno
%43 = call i32 @strerror(i32 %42)
%44 = call i32 @syslog(i32 %40, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"/var/run/labb2.pid\00"
@.str.1 = [3 x i8] c"r+\00"
@stdin = global i32 0
@LOG_ERR = global i32 0
@.str.2 = [34 x i8] c"unable to open file, code %d (%s)\00"
@errno = global i32 0
@SIGTERM = global i32 0
@.str.3 = [36 x i8] c"unable to unlink file, code %d (%s)\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = tail call i32* @freopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = call i64 @fgets(i8* %2, i32 10, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %26, label %15
9: ; preds = %0
%10 = load i32, i32* @LOG_ERR
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @syslog(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %6, %22
%16 = call i32 @atoi(i8* %2)
%17 = load i32, i32* @SIGTERM
%18 = call i64 @kill(i32 %16, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %15
%23 = call i32 @memset(i8* %2, i8 signext 0, i32 10)
%24 = call i64 @fgets(i8* %2, i32 10, i32* %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %15
26: ; preds = %22, %6
%27 = call i32 @fclose(i32* %4)
%28 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* @LOG_ERR
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 @syslog(i32 %31, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %26
ret i32 0
}