#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*/ eerror(char *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
pid_t readpid(const char *filename) {
  FILE *file;
  pid_t pid;
  if ((file = fopen(filename, "r")) == NULL) {
    eerror("Faile to open file");
    return 0;
  }
  if (fscanf(file, "%u", &pid) < 1) {
    fclose(file);
    error("Faile to read file");
    return -1;
  }
  fclose(file);
  return pid;
}


x86 assembly (clang, -O0):
<readpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1192 <readpid+0x42>
lea    0xe81(%rip),%rdi
call   1050 <eerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11df <readpid+0x8f>
mov    -0x18(%rbp),%rdi
lea    0xe78(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jge    11d0 <readpid+0x80>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe59(%rip),%rdi
call   1060 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <readpid+0x8f>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readpid>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     118f <readpid+0x3f>
mov    %rax,%rbx
lea    0xea7(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %ebp,%ebp
jle    119f <readpid+0x4f>
mov    0x4(%rsp),%eax
jmp    11b0 <readpid+0x60>
lea    0xe6c(%rip),%rdi
call   1050 <eerror@plt>
xor    %eax,%eax
jmp    11b0 <readpid+0x60>
lea    0xe72(%rip),%rdi
call   1060 <error@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Faile to open file\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [19 x i8] c"Faile to read file\00"
define i32 @readpid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
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 = call i32 @eerror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %23
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
%18 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %23
19: ; preds = %11
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
%22 = load i32, i32* %5
store i32 %22, i32* %2
br label %23
23: ; preds = %19, %15, %9
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Faile to open file\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [19 x i8] c"Faile to read file\00"
define i32 @readpid(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %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 = tail call i32 @eerror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %16
8: ; preds = %1
%9 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%10 = icmp slt i32 %9, 1
%11 = call i32 @fclose(i32* %4)
br i1 %10, label %12, label %14
12: ; preds = %8
%13 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %16
14: ; preds = %8
%15 = load i32, i32* %2
br label %16
16: ; preds = %14, %12, %6
%17 = phi i32 [ 0, %6 ], [ -1, %12 ], [ %15, %14 ]
ret i32 %17
}