#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 EAGAIN;
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 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;
  close(fd);
  if (i == -1)
    return (error);
  else if (i == 0)
    return (EAGAIN);
  buf[i] = '\0';
  *pidptr = strtol(buf, &endptr, 10);
  if (endptr != &buf[i])
    return (EINVAL);
  return (0);
}


x86 assembly (clang, -O0):
<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 <pidfile_read+0x40>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1230 <pidfile_read+0xf0>
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 <pidfile_read+0x7d>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1230 <pidfile_read+0xf0>
cmpl   $0x0,-0x44(%rbp)
jne    11d8 <pidfile_read+0x98>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1230 <pidfile_read+0xf0>
jmp    11dd <pidfile_read+0x9d>
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     1229 <pidfile_read+0xe9>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1230 <pidfile_read+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<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     1198 <pidfile_read+0x58>
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     11d3 <pidfile_read+0x93>
test   %r15d,%r15d
jne    11a1 <pidfile_read+0x61>
mov    0x2e2a(%rip),%rax
jmp    11d1 <pidfile_read+0x91>
mov    0x2e39(%rip),%rax
jmp    11d1 <pidfile_read+0x91>
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     11d3 <pidfile_read+0x93>
mov    0x2dff(%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
@EAGAIN = global i32 0
@EINVAL = global i32 0
define i32 @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 %50
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 %50
29: ; preds = %18
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* @EAGAIN
store i32 %33, i32* %3
br label %50
34: ; preds = %29
br label %35
35: ; preds = %34
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%40 = call i32 @strtol(i8* %39, i8** %7, i32 10)
%41 = load i32*, i32** %5
store i32 %40, i32* %41
%42 = load i8*, i8** %7
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 %44
%46 = icmp ne i8* %42, %45
br i1 %46, label %47, label %49
47: ; preds = %35
%48 = load i32, i32* @EINVAL
store i32 %48, i32* %3
br label %50
49: ; preds = %35
store i32 0, i32* %3
br label %50
50: ; preds = %49, %47, %32, %27, %16
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@EAGAIN = global i32 0
@EINVAL = global i32 0
define i32 @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 %26
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)
switch i32 %13, label %18 [
i32 -1, label %26
i32 0, label %16
]
16: ; preds = %12
%17 = load i32, i32* @EAGAIN
br label %26
18: ; preds = %12
%19 = sext i32 %13 to i64
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @strtol(i8* %5, i8** %4, i32 10)
store i32 %21, i32* %1
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, %20
%24 = load i32, i32* @EINVAL
%25 = select i1 %23, i32 0, i32 %24
br label %26
26: ; preds = %18, %12, %16, %10
%27 = phi i32 [ %11, %10 ], [ %17, %16 ], [ %14, %12 ], [ %25, %18 ]
ret i32 %27
}