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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, unsigned char *, int);
int read_pid(const char *pidfile) {
  int pid = 0, fd;
  unsigned char c;
  fd = open(pidfile, O_RDONLY);
  if (fd == -1)
    return 0;
  while ((read(fd, &c, 1)) > 0) {
    if (c == '\n')
      break;
    pid = (pid * 10) + (c - '0');
  }
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<read_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    116e <read_pid+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <read_pid+0x93>
jmp    1173 <read_pid+0x43>
mov    -0x18(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    11b5 <read_pid+0x85>
movzbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11a0 <read_pid+0x70>
jmp    11b5 <read_pid+0x85>
imul   $0xa,-0x14(%rbp),%eax
movzbl -0x19(%rbp),%ecx
sub    $0x30,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1173 <read_pid+0x43>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b2 <read_pid+0x82>
mov    %eax,%ebp
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %ebx,%ebx
test   %rax,%rax
jle    11a9 <read_pid+0x79>
mov    0xf(%rsp),%al
cmp    $0xa,%al
je     11a9 <read_pid+0x79>
xor    %ebx,%ebx
lea    0xf(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl %al,%eax
lea    (%rbx,%rbx,4),%ecx
lea    (%rax,%rcx,2),%ebx
add    $0xffffffd0,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11a9 <read_pid+0x79>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
jne    1180 <read_pid+0x50>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11b4 <read_pid+0x84>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @read_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 0, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %34
13: ; preds = %1
br label %14
14: ; preds = %23, %13
%15 = load i32, i32* %5
%16 = call i64 @read(i32 %15, i8* %6, i32 1)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load i8, i8* %6
%20 = zext i8 %19 to i32
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %23
22: ; preds = %18
br label %30
23: ; preds = %18
%24 = load i32, i32* %4
%25 = mul nsw i32 %24, 10
%26 = load i8, i8* %6
%27 = zext i8 %26 to i32
%28 = sub nsw i32 %27, 48
%29 = add nsw i32 %25, %28
store i32 %29, i32* %4
br label %14
30: ; preds = %22, %14
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %34
34: ; preds = %30, %12
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @read_pid(i8* nocapture %0) {
%2 = alloca i8
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = call i64 @read(i32 %4, i8* %2, i32 1)
%8 = icmp slt i64 %7, 1
%9 = load i8, i8* %2
%10 = icmp eq i8 %9, 10
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %24, label %12
12: ; preds = %6, %12
%13 = phi i8 [ %21, %12 ], [ %9, %6 ]
%14 = phi i32 [ %18, %12 ], [ 0, %6 ]
%15 = zext i8 %13 to i32
%16 = mul nsw i32 %14, 10
%17 = add i32 %16, -48
%18 = add i32 %17, %15
%19 = call i64 @read(i32 %4, i8* %2, i32 1)
%20 = icmp slt i64 %19, 1
%21 = load i8, i8* %2
%22 = icmp eq i8 %21, 10
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %12
24: ; preds = %12, %6
%25 = phi i32 [ 0, %6 ], [ %18, %12 ]
%26 = tail call i32 @close(i32 %4)
br label %27
27: ; preds = %1, %24
%28 = phi i32 [ %25, %24 ], [ 0, %1 ]
ret i32 %28
}