#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 EOF;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ assert(int);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ log_msg(int /*<<< orphan*/, char *, char const *);
int read_pidfile(const char *pidfile) {
  FILE *fp;
  int n;
  int pid = 0;
  if (!pidfile) {
    return (0);
  }
  assert(pidfile[0] == '/');
  if (!(fp = fopen(pidfile, "r"))) {
    return (0);
  }
  if ((n = fscanf(fp, "%d", &pid)) == EOF) {
    log_msg(LOG_WARNING, "Unable to read from pidfile \"%s\"", pidfile);
  } else if (n != 1) {
    log_msg(LOG_WARNING, "Unable to obtain pid from pidfile \"%s\"", pidfile);
  }
  if (fclose(fp) == EOF) {
    log_msg(LOG_WARNING, "Unable to close pidfile \"%s\"", pidfile);
  }
  return (((n == 1) && (pid > 1)) ? pid : 0);
}


x86 assembly (clang, -O0):
<read_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <read_pidfile+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    1287 <read_pidfile+0x137>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11bb <read_pidfile+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    1287 <read_pidfile+0x137>
mov    -0x18(%rbp),%rdi
lea    0xe3c(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%eax
jne    11ff <read_pidfile+0xaf>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe10(%rip),%rsi
call   1060 <log_msg@plt>
jmp    1227 <read_pidfile+0xd7>
cmpl   $0x1,-0x1c(%rbp)
je     1222 <read_pidfile+0xd2>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe09(%rip),%rsi
call   1060 <log_msg@plt>
jmp    1227 <read_pidfile+0xd7>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%eax
jne    1258 <read_pidfile+0x108>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfa(%rip),%rsi
call   1060 <log_msg@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    1277 <read_pidfile+0x127>
cmpl   $0x1,-0x20(%rbp)
jle    1277 <read_pidfile+0x127>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1281 <read_pidfile+0x131>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1281 <read_pidfile+0x131>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pidfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
xor    %ebp,%ebp
test   %rdi,%rdi
je     1218 <read_pidfile+0xc8>
mov    %rdi,%rbx
xor    %edi,%edi
cmpb   $0x2f,(%rbx)
sete   %dil
call   1050 <assert@plt>
lea    0xe7e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1218 <read_pidfile+0xc8>
mov    %rax,%r15
lea    0xe65(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%r14d
mov    0x2e34(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    11c2 <read_pidfile+0x72>
lea    0xe45(%rip),%rsi
jmp    11cf <read_pidfile+0x7f>
cmp    $0x1,%r14d
je     11e0 <read_pidfile+0x90>
lea    0xe57(%rip),%rsi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rdx
call   1060 <log_msg@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
cmp    0x0(%rbp),%eax
jne    1205 <read_pidfile+0xb5>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    %rbx,%rdx
call   1060 <log_msg@plt>
mov    0x4(%rsp),%ebp
xor    %eax,%eax
cmp    $0x2,%ebp
cmovl  %eax,%ebp
cmp    $0x1,%r14d
cmovne %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i32 0
@LOG_WARNING = global i32 0
@.str.2 = [33 x i8] c"Unable to read from pidfile \22%s\22\00"
@.str.3 = [39 x i8] c"Unable to obtain pid from pidfile \22%s\22\00"
@.str.4 = [29 x i8] c"Unable to close pidfile \22%s\22\00"
define i32 @read_pidfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 0, i32* %2
br label %59
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 47
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8*, i8** %3
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %4
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %10
store i32 0, i32* %2
br label %59
22: ; preds = %10
%23 = load i32*, i32** %4
%24 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
store i32 %24, i32* %5
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* @LOG_WARNING
%29 = load i8*, i8** %3
%30 = call i32 @log_msg(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %39
31: ; preds = %22
%32 = load i32, i32* %5
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* @LOG_WARNING
%36 = load i8*, i8** %3
%37 = call i32 @log_msg(i32 %35, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %34, %31
br label %39
39: ; preds = %38, %27
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* @EOF
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* @LOG_WARNING
%46 = load i8*, i8** %3
%47 = call i32 @log_msg(i32 %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %44, %39
%49 = load i32, i32* %5
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* %6
%53 = icmp sgt i32 %52, 1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i32, i32* %6
br label %57
56: ; preds = %51, %48
br label %57
57: ; preds = %56, %54
%58 = phi i32 [ %55, %54 ], [ 0, %56 ]
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %21, %9
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i32 0
@LOG_WARNING = global i32 0
@.str.2 = [33 x i8] c"Unable to read from pidfile \22%s\22\00"
@.str.3 = [39 x i8] c"Unable to obtain pid from pidfile \22%s\22\00"
@.str.4 = [29 x i8] c"Unable to close pidfile \22%s\22\00"
define i32 @read_pidfile(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = icmp eq i8* %0, null
br i1 %4, label %35, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 47
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %35, label %12
12: ; preds = %5
%13 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = icmp eq i32 %13, 1
br i1 %17, label %22, label %18
18: ; preds = %16, %12
%19 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), %12 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), %16 ]
%20 = load i32, i32* @LOG_WARNING
%21 = call i32 @log_msg(i32 %20, i8* %19, i8* %0)
br label %22
22: ; preds = %18, %16
%23 = call i32 @fclose(i32* %10)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @LOG_WARNING
%28 = call i32 @log_msg(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %26, %22
%30 = icmp eq i32 %13, 1
%31 = load i32, i32* %2
%32 = icmp sgt i32 %31, 1
%33 = select i1 %30, i1 %32, i1 false
%34 = select i1 %33, i32 %31, i32 0
br label %35
35: ; preds = %5, %1, %29
%36 = phi i32 [ %34, %29 ], [ 0, %1 ], [ 0, %5 ]
ret i32 %36
}