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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int sscanf(char *, char *, int *);
int getlockpid(char *file) {
  int fd, pid = -1;
  char buf[512];
  int len;
  if ((fd = open(file, O_RDONLY)) == -1)
    return -1;
  if ((len = read(fd, buf, sizeof(buf))) >= 0) {
    buf[len] = 0;
    if (sscanf(buf, "%d", &pid) != 1) {
      logger(1, 0, "Incorrect pid: %s in %s", buf, file);
      pid = 0;
    }
  }
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<getlockpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1190 <getlockpid+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <getlockpid+0xca>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0x224(%rbp)
cmp    $0x0,%eax
jl     120c <getlockpid+0xbc>
movslq -0x224(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
lea    0xe30(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     1207 <getlockpid+0xb7>
lea    -0x220(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe08(%rip),%rdx
call   1050 <logger@plt>
movl   $0x0,-0x18(%rbp)
jmp    120c <getlockpid+0xbc>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlockpid>:
push   %rbp
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%rbx
movl   $0xffffffff,0xc(%rsp)
ff
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e0 <getlockpid+0x90>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11d3 <getlockpid+0x83>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe64(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     11d3 <getlockpid+0x83>
lea    0xe4c(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
mov    %rbx,%r8
call   1050 <logger@plt>
movl   $0x0,0xc(%rsp)
mov    %ebp,%edi
call   1030 <close@plt>
mov    0xc(%rsp),%eax
jmp    11e5 <getlockpid+0x95>
mov    $0xffffffff,%eax
add    $0x218,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [24 x i8] c"Incorrect pid: %s in %s\00"
define i32 @getlockpid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %5
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %34
13: ; preds = %1
%14 = load i32, i32* %4
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 512)
store i32 %16, i32* %7
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %20
store i8 0, i8* %21
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%27 = load i8*, i8** %3
%28 = call i32 @logger(i32 1, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %27)
store i32 0, i32* %5
br label %29
29: ; preds = %25, %18
br label %30
30: ; preds = %29, %13
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %34
34: ; preds = %30, %12
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [24 x i8] c"Incorrect pid: %s in %s\00"
define i32 @getlockpid(i8* %0) {
%2 = alloca i32
%3 = alloca [512 x i8]
%4 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %22, label %9
9: ; preds = %1
%10 = call i32 @read(i32 %7, i8* %5, i32 512)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = zext i32 %10 to i64
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%16 = icmp eq i32 %15, 1
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = call i32 @logger(i32 1, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %0)
store i32 0, i32* %2
br label %19
19: ; preds = %12, %17, %9
%20 = call i32 @close(i32 %7)
%21 = load i32, i32* %2
br label %22
22: ; preds = %1, %19
%23 = phi i32 [ %21, %19 ], [ -1, %1 ]
ret i32 %23
}