#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 scalar_t__ ssize_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ memset(void *, int, size_t);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, void *, size_t);
scalar_t__ strtol(char *, char **, int);
pid_t pidfile_getpid(const char *path) {
  int pid = 0;
  char pid_str[64];
  int fd = 0;
  int old_errno = 0;
  ssize_t sz = 0;
  memset((void *)pid_str, (int)NULL, (size_t)64);
  fd = open(path, O_RDONLY);
  if (fd > 0) {
    sz = read(fd, (void *)pid_str, (size_t)64);
    if (sz > 0) {
      old_errno = errno;
      errno = 0;
      pid = (pid_t)strtol(pid_str, (char **)NULL, 10);
      if (errno == 0) {
        errno = old_errno;
        close(fd);
        return (pid_t)pid;
      }
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<pidfile_getpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x64(%rbp)
movl   $0x0,-0x68(%rbp)
movq   $0x0,-0x70(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jle    1232 <pidfile_getpid+0xe2>
mov    -0x64(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jle    122a <pidfile_getpid+0xda>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2dfd(%rip),%rax
movl   $0x0,(%rax)
lea    -0x60(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    0x2ddb(%rip),%rax
cmpl   $0x0,(%rax)
jne    1225 <pidfile_getpid+0xd5>
mov    -0x68(%rbp),%ecx
mov    0x2dc8(%rip),%rax
mov    %ecx,(%rax)
mov    -0x64(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <pidfile_getpid+0xe9>
jmp    122a <pidfile_getpid+0xda>
mov    -0x64(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfile_getpid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %r14d,%r14d
mov    $0x40,%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
jle    11d5 <pidfile_getpid+0x85>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    $0x40,%edx
mov    %eax,%edi
call   1050 <read@plt>
test   %rax,%rax
jle    11cb <pidfile_getpid+0x7b>
mov    0x2e37(%rip),%r15
mov    (%r15),%ebp
movl   $0x0,(%r15)
xor    %r14d,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
cmpl   $0x0,(%r15)
jne    11ce <pidfile_getpid+0x7e>
mov    %ebp,(%r15)
mov    %eax,%r14d
jmp    11ce <pidfile_getpid+0x7e>
xor    %r14d,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @pidfile_getpid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %6
store i32 0, i32* %7
store i64 0, i64* %8
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i64 64)
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %38
16: ; preds = %1
%17 = load i32, i32* %6
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%19 = call i64 @read(i32 %17, i8* %18, i64 64)
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %16
%23 = load i32, i32* @errno
store i32 %23, i32* %7
store i32 0, i32* @errno
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%25 = call i64 @strtol(i8* %24, i8** null, i32 10)
%26 = trunc i64 %25 to i32
store i32 %26, i32* %4
%27 = load i32, i32* @errno
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %22
%30 = load i32, i32* %7
store i32 %30, i32* @errno
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %39
34: ; preds = %22
br label %35
35: ; preds = %34, %16
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %1
store i32 0, i32* %2
br label %39
39: ; preds = %38, %29
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @pidfile_getpid(i8* nocapture %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i64 64)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %0, i32 %5)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = call i64 @read(i32 %6, i8* %3, i64 64)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = load i32, i32* @errno
store i32 0, i32* @errno
%13 = call i64 @strtol(i8* nocapture %3, i8** null, i32 10)
%14 = load i32, i32* @errno
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = trunc i64 %13 to i32
store i32 %12, i32* @errno
br label %18
18: ; preds = %8, %11, %16
%19 = phi i32 [ %17, %16 ], [ 0, %11 ], [ 0, %8 ]
%20 = call i32 @close(i32 %6)
br label %21
21: ; preds = %18, %1
%22 = phi i32 [ 0, %1 ], [ %19, %18 ]
ret i32 %22
}