#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 */
scalar_t__ ESRCH;
scalar_t__ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ kill(int, int /*<<< orphan*/);
pid_t pid_read(char *filename) {
  FILE *file;
  pid_t pid;
  file = fopen(filename, "r");
  if (!file) {
    return (-1);
  }
  if (fscanf(file, "%d", &pid) != 1) {
    (void)fclose(file);
    return (-1);
  }
  if (fclose(file) != 0) {
    return (-1);
  }
  if (kill(pid, 0) != 0) {
    if (errno == ESRCH) {
      return (-1);
    }
  }
  return (pid);
}


x86 assembly (clang, -O0):
<pid_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <pid_read+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <pid_read+0xcd>
mov    -0x18(%rbp),%rdi
lea    0xe80(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11a9 <pid_read+0x69>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <pid_read+0xcd>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     11c8 <pid_read+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <pid_read+0xcd>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
cmp    $0x0,%rax
je     1207 <pid_read+0xc7>
mov    0x2dfd(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <pid_read+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <pid_read+0xcd>
jmp    1207 <pid_read+0xc7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11b5 <pid_read+0x75>
mov    %rax,%rbx
lea    0xe99(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%ebp
jne    11b5 <pid_read+0x75>
test   %rax,%rax
jne    11b5 <pid_read+0x75>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
test   %rax,%rax
je     11b0 <pid_read+0x70>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11b5 <pid_read+0x75>
mov    0xc(%rsp),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @pid_read(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 -1, i32* %2
br label %35
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32*, i32** %4
%17 = call i64 @fclose(i32* %16)
store i32 -1, i32* %2
br label %35
18: ; preds = %11
%19 = load i32*, i32** %4
%20 = call i64 @fclose(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %2
br label %35
23: ; preds = %18
%24 = load i32, i32* %5
%25 = call i64 @kill(i32 %24, i32 0)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @ESRCH
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %2
br label %35
32: ; preds = %27
br label %33
33: ; preds = %32, %23
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %31, %22, %15, %10
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@errno = global i64 0
@ESRCH = global i64 0
define i32 @pid_read(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %22, label %6
6: ; preds = %1
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%8 = icmp eq i32 %7, 1
%9 = call i64 @fclose(i32* %4)
%10 = icmp eq i64 %9, 0
%11 = and i1 %8, %10
br i1 %11, label %12, label %22
12: ; preds = %6
%13 = load i32, i32* %2
%14 = call i64 @kill(i32 %13, i32 0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @ESRCH
%19 = icmp eq i64 %17, %18
br i1 %19, label %22, label %20
20: ; preds = %16, %12
%21 = load i32, i32* %2
br label %22
22: ; preds = %6, %16, %1, %20
%23 = phi i32 [ %21, %20 ], [ -1, %1 ], [ -1, %16 ], [ -1, %6 ]
ret i32 %23
}