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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, char const *);
int /*<<< orphan*/ warnx(char *, char const *);
pid_t readpidfile(const char *file) {
  FILE *fd;
  char line[BUFSIZ];
  pid_t pid;
  if ((fd = fopen(file, "r")) == NULL) {
    warn("%s", file);
    return (pid_t)-1;
  }
  if (fgets(line, sizeof(line) - 1, fd) != NULL) {
    line[sizeof(line) - 1] = '\0';
    pid = (pid_t)strtol(line, NULL, 0);
  } else {
    warnx("unable to read %s", file);
    pid = (pid_t)-1;
  }
  (void)fclose(fd);
  return pid;
}


x86 assembly (clang, -O0):
<readpidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11de <readpidfile+0x7e>
mov    -0x10(%rbp),%rsi
lea    0xe4c(%rip),%rdi
call   1030 <warn@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
movl   $0x1,-0x34(%rbp)
jmp    1253 <readpidfile+0xf3>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1223 <readpidfile+0xc3>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %rax,-0x30(%rbp)
jmp    123b <readpidfile+0xdb>
mov    -0x10(%rbp),%rsi
lea    0xdd4(%rip),%rdi
call   1050 <warnx@plt>
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readpidfile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2e6b(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11cb <readpidfile+0x6b>
mov    %rax,%r15
add    $0xffffffffffffffff,%r12
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11e3 <readpidfile+0x83>
movb   $0x0,(%rbx,%r12,1)
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %rax,%rbx
jmp    11f9 <readpidfile+0x99>
lea    0xe3f(%rip),%rdi
mov    %r14,%rsi
call   1030 <warn@plt>
mov    $0xffffffffffffffff,%rbx
jmp    1201 <readpidfile+0xa1>
lea    0xe18(%rip),%rdi
mov    %r14,%rsi
call   1050 <warnx@plt>
mov    $0xffffffffffffffff,%rbx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [18 x i8] c"unable to read %s\00"
define i64 @readpidfile(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i64 -1, i64* %2
store i32 1, i32* %8
br label %36
19: ; preds = %1
%20 = sub i64 %10, 1
%21 = trunc i64 %20 to i32
%22 = load i32*, i32** %4
%23 = call i32* @fgets(i8* %12, i32 %21, i32* %22)
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = sub i64 %10, 1
%27 = getelementptr inbounds i8, i8* %12, i64 %26
store i8 0, i8* %27
%28 = call i64 @strtol(i8* %12, i32* null, i32 0)
store i64 %28, i64* %7
br label %32
29: ; preds = %19
%30 = load i8*, i8** %3
%31 = call i32 @warnx(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i64 -1, i64* %7
br label %32
32: ; preds = %29, %25
%33 = load i32*, i32** %4
%34 = call i32 @fclose(i32* %33)
%35 = load i64, i64* %7
store i64 %35, i64* %2
store i32 1, i32* %8
br label %36
36: ; preds = %32, %16
%37 = load i8*, i8** %5
%38 = load i64, i64* %2
ret i64 %38
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [18 x i8] c"unable to read %s\00"
define i64 @readpidfile(i8* %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
9: ; preds = %1
%10 = add nsw i64 %3, -1
%11 = trunc i64 %10 to i32
%12 = call i32* @fgets(i8* %4, i32 %11, i32* %5)
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %4, i64 %10
store i8 0, i8* %15
%16 = call i64 @strtol(i8* nocapture %4, i32* null, i32 0)
br label %19
17: ; preds = %9
%18 = call i32 @warnx(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %19
19: ; preds = %17, %14
%20 = phi i64 [ %16, %14 ], [ -1, %17 ]
%21 = call i32 @fclose(i32* %5)
br label %22
22: ; preds = %19, %7
%23 = phi i64 [ -1, %7 ], [ %20, %19 ]
ret i64 %23
}