#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__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, scalar_t__ *);
pid_t read_pid(const char *pidfile) {
  FILE *fp;
  pid_t pid;
  if ((fp = fopen(pidfile, "r")) == NULL) {
    errno = ENOENT;
    return 0;
  }
  if (fscanf(fp, "%d", &pid) != 1)
    pid = 0;
  fclose(fp);
  return pid;
}


x86 assembly (clang, -O0):
<read_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1179 <read_pid+0x49>
mov    0x2e6f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e76(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11af <read_pid+0x7f>
mov    -0x18(%rbp),%rdi
lea    0xe7e(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     119e <read_pid+0x6e>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pid>:
push   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117a <read_pid+0x4a>
mov    %rax,%rbx
lea    0xeb2(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     116b <read_pid+0x3b>
movq   $0x0,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rax
jmp    118e <read_pid+0x5e>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    0x2e56(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@ENOENT = global i32 0
@errno = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i64 @read_pid(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i32, i32* @ENOENT
store i32 %10, i32* @errno
store i64 0, i64* %2
br label %20
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64* %5)
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %11
store i64 0, i64* %5
br label %16
16: ; preds = %15, %11
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
%19 = load i64, i64* %5
store i64 %19, i64* %2
br label %20
20: ; preds = %16, %9
%21 = load i64, i64* %2
ret i64 %21
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@ENOENT = global i32 0
@errno = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i64 @read_pid(i8* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = load i32, i32* @ENOENT
store i32 %7, i32* @errno
br label %15
8: ; preds = %1
%9 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64* %2)
%10 = icmp eq i32 %9, 1
br i1 %10, label %12, label %11
11: ; preds = %8
store i64 0, i64* %2
br label %12
12: ; preds = %11, %8
%13 = call i32 @fclose(i32* %4)
%14 = load i64, i64* %2
br label %15
15: ; preds = %12, %6
%16 = phi i64 [ 0, %6 ], [ %14, %12 ]
ret i64 %16
}