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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ rs_debug(char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int getpidfromdir() {
  int fd, fpid;
  char path[1024];
  if ((fd = open("/var/run/rtmpserver.pid", O_RDONLY)) >= 0) {
    if (read(fd, path, 1024) <= 0) {
      rs_debug("read run/pid failed\n");
      close(fd);
      return -1;
    } else {
      sscanf(path, "%d", &fpid);
      close(fd);
      return fpid;
    }
  } else
    return -1;
}


x86 assembly (clang, -O0):
<getpidfromdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     11e4 <getpidfromdir+0x94>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jg     11ba <getpidfromdir+0x6a>
lea    0xe77(%rip),%rdi
call   1060 <rs_debug@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <getpidfromdir+0x9b>
lea    -0x410(%rbp),%rdi
lea    0xe65(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <getpidfromdir+0x9b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpidfromdir>:
push   %rbp
push   %rbx
sub    $0x418,%rsp
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1070 <open@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11c5 <getpidfromdir+0x75>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11b2 <getpidfromdir+0x62>
lea    0xe97(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1050 <sscanf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0xc(%rsp),%ebp
jmp    11c5 <getpidfromdir+0x75>
lea    0xe5f(%rip),%rdi
call   1060 <rs_debug@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/var/run/rtmpserver.pid\00"
@O_RDONLY = global i32 0
@.str.1 = [21 x i8] c"read run/pid failed\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @getpidfromdir() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %0
%9 = load i32, i32* %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i64 @read(i32 %9, i8* %10, i32 1024)
%12 = icmp sle i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = call i32 @rs_debug(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %2
%16 = call i32 @close(i32 %15)
store i32 -1, i32* %1
br label %24
17: ; preds = %8
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %3
store i32 %22, i32* %1
br label %24
23: ; preds = %0
store i32 -1, i32* %1
br label %24
24: ; preds = %23, %17, %13
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [24 x i8] c"/var/run/rtmpserver.pid\00"
@O_RDONLY = global i32 0
@.str.1 = [21 x i8] c"read run/pid failed\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @getpidfromdir() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = call i64 @read(i32 %6, i8* %4, i32 1024)
%10 = icmp slt i64 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @rs_debug(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @close(i32 %6)
br label %18
14: ; preds = %8
%15 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%16 = call i32 @close(i32 %6)
%17 = load i32, i32* %1
br label %18
18: ; preds = %0, %14, %11
%19 = phi i32 [ -1, %11 ], [ %17, %14 ], [ -1, %0 ]
ret i32 %19
}