#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 O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int);
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 | O_CLOEXEC);
  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
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1189 <pidfile_read+0x49>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <pidfile_read+0xf9>
mov    -0x40(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xf,%edx
call   1040 <read@plt>
mov    %eax,-0x44(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%edi
call   1030 <close@plt>
cmpl   $0xffffffff,-0x44(%rbp)
jne    11c6 <pidfile_read+0x86>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <pidfile_read+0xf9>
cmpl   $0x0,-0x44(%rbp)
jne    11e1 <pidfile_read+0xa1>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <pidfile_read+0xf9>
jmp    11e6 <pidfile_read+0xa6>
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     1232 <pidfile_read+0xf2>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <pidfile_read+0xf9>
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    0x2e65(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a1 <pidfile_read+0x61>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xf,%edx
call   1040 <read@plt>
mov    %eax,%r15d
mov    0x2e54(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%r15d
je     11dc <pidfile_read+0x9c>
test   %r15d,%r15d
jne    11aa <pidfile_read+0x6a>
mov    0x2e19(%rip),%rax
jmp    11da <pidfile_read+0x9a>
mov    0x2e30(%rip),%rax
jmp    11da <pidfile_read+0x9a>
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     11dc <pidfile_read+0x9c>
mov    0x2df6(%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
@O_CLOEXEC = 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 = load i32, i32* @O_CLOEXEC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %11, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i32, i32* @errno
store i32 %19, i32* %3
br label %52
20: ; preds = %2
%21 = load i32, i32* %9
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 15)
store i32 %23, i32* %10
%24 = load i32, i32* @errno
store i32 %24, i32* %8
%25 = load i32, i32* %9
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %10
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = load i32, i32* %8
store i32 %30, i32* %3
br label %52
31: ; preds = %20
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* @EAGAIN
store i32 %35, i32* %3
br label %52
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%42 = call i32 @strtol(i8* %41, i8** %7, i32 10)
%43 = load i32*, i32** %5
store i32 %42, i32* %43
%44 = load i8*, i8** %7
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 %46
%48 = icmp ne i8* %44, %47
br i1 %48, label %49, label %51
49: ; preds = %37
%50 = load i32, i32* @EINVAL
store i32 %50, i32* %3
br label %52
51: ; preds = %37
store i32 0, i32* %3
br label %52
52: ; preds = %51, %49, %34, %29, %18
%53 = load i32, i32* %3
ret i32 %53
}

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