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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int read_pid() {
  FILE *pid_file;
  int pid;
  if (!(pid_file = fopen("/tmp/bluelog.pid", "r")))
    return 0;
  if (fscanf(pid_file, "%d", &pid) < 0)
    pid = 0;
  fclose(pid_file);
  return pid;
}


x86 assembly (clang, -O0):
<read_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
lea    0xecb(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1165 <read_pid+0x35>
movl   $0x0,-0x4(%rbp)
jmp    1199 <read_pid+0x69>
mov    -0x10(%rbp),%rdi
lea    0xea3(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x0,%rax
jge    118a <read_pid+0x5a>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@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   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rdi
lea    0xece(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117f <read_pid+0x4f>
mov    %rax,%rbx
lea    0xebc(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
test   %rax,%rax
jns    1171 <read_pid+0x41>
movl   $0x0,0xc(%rsp)
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    1181 <read_pid+0x51>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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