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

/* Variables and functions */
int EOWNERDEAD;
int /*<<< orphan*/ EXIT_FAILURE;
int MAP_FILE;
int MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int pthread_mutex_consistent(int /*<<< orphan*/ *);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void worker(const char *filename) {
  pthread_mutex_t *mutex;
  void *addr;
  int ret, fd;
  fd = open(filename, O_RDWR, 0600);
  if (fd == -1) {
    perror("FAIL: open");
    exit(EXIT_FAILURE);
  }
  addr = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE,
              MAP_SHARED | MAP_FILE, fd, 0);
  if (addr == NULL) {
    perror("FAIL: mmap");
    exit(EXIT_FAILURE);
  }
  mutex = (pthread_mutex_t *)addr;
  printf("INFO: pid %u locking\n", getpid());
  do {
    ret = pthread_mutex_lock(mutex);
    if (ret == EOWNERDEAD) {
      int rc;
      rc = pthread_mutex_consistent(mutex);
      if (rc == 0) {
        pthread_mutex_unlock(mutex);
      } else {
        fprintf(stderr, "FAIL: pthread_mutex_consistent "
                        "failed\n");
        exit(EXIT_FAILURE);
      }
    }
  } while (ret != 0);
  printf("INFO: pid %u locked, now killing\n", getpid());
  kill(getpid(), SIGKILL);
}


x86 assembly (clang, -O0):
<worker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11fa <worker+0x4a>
lea    0xe19(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    0x2d9e(%rip),%rax
or     (%rax),%edx
mov    0x2dd5(%rip),%rax
mov    (%rax),%ecx
mov    0x2db4(%rip),%rax
or     (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125c <worker+0xac>
lea    0xdc2(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xda2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <pthread_mutex_lock@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%eax
jne    12e3 <worker+0x133>
mov    -0x10(%rbp),%rdi
call   1060 <pthread_mutex_consistent@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12bb <worker+0x10b>
mov    -0x10(%rbp),%rdi
call   1090 <pthread_mutex_unlock@plt>
jmp    12de <worker+0x12e>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12e3 <worker+0x133>
jmp    12e8 <worker+0x138>
cmpl   $0x0,-0x1c(%rbp)
jne    1279 <worker+0xc9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd51(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2c89(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<worker>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12ac <worker+0xfc>
mov    0x2e21(%rip),%rcx
mov    0x2dca(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dff(%rip),%rsi
mov    0x2de0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     12b5 <worker+0x105>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdf6(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x2d8a(%rip),%r14
jmp    1234 <worker+0x84>
test   %ebp,%ebp
je     1259 <worker+0xa9>
mov    %rbx,%rdi
call   10d0 <pthread_mutex_lock@plt>
mov    %eax,%ebp
cmp    (%r14),%eax
jne    1230 <worker+0x80>
mov    %rbx,%rdi
call   1060 <pthread_mutex_consistent@plt>
test   %eax,%eax
jne    1289 <worker+0xd9>
mov    %rbx,%rdi
call   1090 <pthread_mutex_unlock@plt>
jmp    1230 <worker+0x80>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdec(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2d24(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <kill@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xd4d(%rip),%rdi
jmp    12bc <worker+0x10c>
lea    0xd4f(%rip),%rdi
call   10b0 <perror@plt>
jmp    129e <worker+0xee>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [11 x i8] c"FAIL: open\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FILE = global i32 0
@.str.1 = [11 x i8] c"FAIL: mmap\00"
@.str.2 = [22 x i8] c"INFO: pid %u locking\0A\00"
@EOWNERDEAD = global i32 0
@stderr = global i32 0
@.str.3 = [39 x i8] c"FAIL: pthread_mutex_consistent failed\0A\00"
@.str.4 = [34 x i8] c"INFO: pid %u locked, now killing\0A\00"
@SIGKILL = global i32 0
define void @worker(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9, i32 384)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* @MAP_FILE
%23 = or i32 %21, %22
%24 = load i32, i32* %6
%25 = call i8* @mmap(i32* null, i32 4, i32 %20, i32 %23, i32 %24, i32 0)
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %17
%33 = load i8*, i8** %4
%34 = bitcast i8* %33 to i32*
store i32* %34, i32** %3
%35 = call i32 (...) @getpid()
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %58, %32
%38 = load i32*, i32** %3
%39 = call i32 @pthread_mutex_lock(i32* %38)
store i32 %39, i32* %5
%40 = load i32, i32* %5
%41 = load i32, i32* @EOWNERDEAD
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %57
43: ; preds = %37
%44 = load i32*, i32** %3
%45 = call i32 @pthread_mutex_consistent(i32* %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32*, i32** %3
%50 = call i32 @pthread_mutex_unlock(i32* %49)
br label %56
51: ; preds = %43
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %48
br label %57
57: ; preds = %56, %37
br label %58
58: ; preds = %57
%59 = load i32, i32* %5
%60 = icmp ne i32 %59, 0
br i1 %60, label %37, label %61
61: ; preds = %58
%62 = call i32 (...) @getpid()
%63 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = call i32 (...) @getpid()
%65 = load i32, i32* @SIGKILL
%66 = call i32 @kill(i32 %64, i32 %65)
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [11 x i8] c"FAIL: open\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FILE = global i32 0
@.str.1 = [11 x i8] c"FAIL: mmap\00"
@.str.2 = [22 x i8] c"INFO: pid %u locking\0A\00"
@EOWNERDEAD = global i32 0
@stderr = global i32 0
@.str.3 = [39 x i8] c"FAIL: pthread_mutex_consistent failed\0A\00"
@.str.4 = [34 x i8] c"INFO: pid %u locked, now killing\0A\00"
@SIGKILL = global i32 0
define void @worker(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2, i32 384)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = load i32, i32* @MAP_FILE
%15 = or i32 %14, %13
%16 = tail call i8* @mmap(i32* null, i32 4, i32 %12, i32 %15, i32 %3, i32 0)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %9
%23 = bitcast i8* %16 to i32*
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %40, %22
%27 = tail call i32 @pthread_mutex_lock(i32* %23)
%28 = load i32, i32* @EOWNERDEAD
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %40
30: ; preds = %26
%31 = tail call i32 @pthread_mutex_consistent(i32* %23)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i32 @pthread_mutex_unlock(i32* %23)
br label %40
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %26, %33
%41 = icmp eq i32 %27, 0
br i1 %41, label %42, label %26
42: ; preds = %40
%43 = tail call i32 (...) @getpid()
%44 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = tail call i32 (...) @getpid()
%46 = load i32, i32* @SIGKILL
%47 = tail call i32 @kill(i32 %45, i32 %46)
ret void
}