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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
pid_t read_pidfile(const char *path) {
  pid_t pid = -1;
  FILE *fp = NULL;
  if (!path)
    goto out;
  fp = fopen(path, "r");
  if (!fp)
    goto out;
  if (fscanf(fp, "%d", &pid) != 1)
    goto out;
out:
  if (fp) {
    fclose(fp);
    fp = NULL;
  }
  return pid;
}


x86 assembly (clang, -O0):
<read_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    115b <read_pidfile+0x2b>
jmp    11a6 <read_pidfile+0x76>
mov    -0x8(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117f <read_pidfile+0x4f>
jmp    11a6 <read_pidfile+0x76>
mov    -0x18(%rbp),%rdi
lea    0xe78(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11a1 <read_pidfile+0x71>
jmp    11a6 <read_pidfile+0x76>
jmp    11a6 <read_pidfile+0x76>
cmpq   $0x0,-0x18(%rbp)
je     11c2 <read_pidfile+0x92>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pidfile>:
push   %r14
push   %rbx
push   %rax
movl   $0xffffffff,0x4(%rsp)
ff
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     117a <read_pidfile+0x4a>
lea    0xeb3(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117a <read_pidfile+0x4a>
mov    %rax,%r14
lea    0xea1(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    0x4(%rsp),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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