#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*/ buf;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strtol(char *, char **, int);
int tsd_pidfile_read(const char *path, pid_t *pidptr) {
  char buf[16], *endptr;
  int error, fd, i;
  fd = open(path, O_RDONLY);
  if (fd == -1)
    return (errno);
  i = read(fd, buf, sizeof(buf) - 1);
  error = errno;
  (void)close(fd);
  if (i == -1)
    return (error);
  buf[i] = '\0';
  *pidptr = strtol(buf, &endptr, 10);
  if (endptr != &buf[i])
    return (EINVAL);
  return (0);
}


x86 assembly (clang, -O0):
<tsd_pidfile_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1180 <tsd_pidfile_read+0x40>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <tsd_pidfile_read+0xd0>
mov    -0x40(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xf,%edx
call   1040 <read@plt>
mov    %eax,-0x44(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%edi
call   1030 <close@plt>
cmpl   $0xffffffff,-0x44(%rbp)
jne    11bd <tsd_pidfile_read+0x7d>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <tsd_pidfile_read+0xd0>
movslq -0x44(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
movslq -0x44(%rbp),%rdx
lea    -0x30(%rbp),%rcx
add    %rdx,%rcx
cmp    %rcx,%rax
je     1209 <tsd_pidfile_read+0xc9>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <tsd_pidfile_read+0xd0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tsd_pidfile_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11bc <tsd_pidfile_read+0x7c>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xf,%edx
call   1040 <read@plt>
mov    %eax,%r15d
mov    0x2e5d(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%r15d
je     11c5 <tsd_pidfile_read+0x85>
movslq %r15d,%rax
lea    (%rsp,%rax,1),%rbx
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,(%r14)
xor    %ebp,%ebp
cmp    %rbx,0x10(%rsp)
je     11c5 <tsd_pidfile_read+0x85>
mov    0x2e16(%rip),%rax
jmp    11c3 <tsd_pidfile_read+0x83>
mov    0x2e15(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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