#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 /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ unlink(char const *);
int unlockfile(const char *path) {
  FILE *f = fopen(path, "r");
  if (!f) {
    syslog(LOG_NOTICE, "no such lock: %s", path);
    return 0;
  }
  int pid = -1;
  fscanf(f, "%d", &pid);
  fclose(f);
  if (pid != getpid()) {
    syslog(LOG_NOTICE, "lock %s stolen by pid %d", path, pid);
    return 0;
  }
  syslog(LOG_INFO, "released lock %s", path);
  unlink(path);
  return 1;
}


x86 assembly (clang, -O0):
<unlockfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <unlockfile+0x52>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <unlockfile+0xe5>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe65(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
je     121a <unlockfile+0xba>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <unlockfile+0xe5>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlockfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe8e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11df <unlockfile+0x7f>
mov    %rax,%rbp
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe9b(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    0xc(%rsp),%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %eax,%ebp
jne    11fe <unlockfile+0x9e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    $0x1,%r14d
jmp    121c <unlockfile+0xbc>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
jmp    121c <unlockfile+0xbc>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%ecx
lea    0xe01(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@LOG_NOTICE = global i32 0
@.str.1 = [17 x i8] c"no such lock: %s\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [25 x i8] c"lock %s stolen by pid %d\00"
@LOG_INFO = global i32 0
@.str.4 = [17 x i8] c"released lock %s\00"
define i32 @unlockfile(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 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i32, i32* @LOG_NOTICE
%12 = load i8*, i8** %3
%13 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 0, i32* %2
br label %33
14: ; preds = %1
store i32 -1, i32* %5
%15 = load i32*, i32** %4
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %5
%20 = call i32 (...) @getpid()
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %14
%23 = load i32, i32* @LOG_NOTICE
%24 = load i8*, i8** %3
%25 = load i32, i32* %5
%26 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %24, i32 %25)
store i32 0, i32* %2
br label %33
27: ; preds = %14
%28 = load i32, i32* @LOG_INFO
%29 = load i8*, i8** %3
%30 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %3
%32 = call i32 @unlink(i8* %31)
store i32 1, i32* %2
br label %33
33: ; preds = %27, %22, %10
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@LOG_NOTICE = global i32 0
@.str.1 = [17 x i8] c"no such lock: %s\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [25 x i8] c"lock %s stolen by pid %d\00"
@LOG_INFO = global i32 0
@.str.4 = [17 x i8] c"released lock %s\00"
define i32 @unlockfile(i8* %0) {
%2 = alloca i32
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @LOG_NOTICE
%7 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%10 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%11 = call i32 @fclose(i32* %3)
%12 = load i32, i32* %2
%13 = call i32 (...) @getpid()
%14 = icmp eq i32 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %8
%16 = load i32, i32* @LOG_NOTICE
%17 = load i32, i32* %2
%18 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %17)
br label %23
19: ; preds = %8
%20 = load i32, i32* @LOG_INFO
%21 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%22 = call i32 @unlink(i8* %0)
br label %23
23: ; preds = %19, %15
%24 = phi i32 [ 0, %15 ], [ 1, %19 ]
br label %25
25: ; preds = %23, %5
%26 = phi i32 [ %24, %23 ], [ 0, %5 ]
ret i32 %26
}