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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
pid_t read_pid(const char *pid_file) {
  FILE *fp = fopen(pid_file, "r");
  if (fp == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  pid_t p;
  if (1 != fscanf(fp, "%d", &p)) {
    fprintf(stderr, "%s has no pid value\n", pid_file);
    exit(EXIT_FAILURE);
  }
  return p;
}


x86 assembly (clang, -O0):
<read_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1195 <read_pid+0x45>
lea    0xe80(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <fscanf@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     11df <read_pid+0x8f>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3f(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pid>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xea1(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     118c <read_pid+0x3c>
lea    0xe98(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1030 <fscanf@plt>
cmp    $0x1,%eax
jne    119a <read_pid+0x4a>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
jmp    11b2 <read_pid+0x62>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"%s has no pid value\0A\00"
define i32 @read_pid(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = load i32*, i32** %3
%15 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%16 = icmp ne i32 1, %15
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %2
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %13
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"%s has no pid value\0A\00"
define i32 @read_pid(i8* %0) {
%2 = alloca i32
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = bitcast i32* %2 to i8*
%11 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%12 = icmp eq i32 %11, 1
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %9
%19 = load i32, i32* %2
ret i32 %19
}