#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ Window;
typedef int /*<<< orphan*/ Display;
typedef scalar_t__ Atom;

/* Variables and functions */
int /*<<< orphan*/ DefaultRootWindow(int /*<<< orphan*/ *);
int /*<<< orphan*/ False;
scalar_t__ None;
int /*<<< orphan*/ PropModeReplace;
int Success;
int /*<<< orphan*/ XA_CARDINAL;
int /*<<< orphan*/ XChangeProperty(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                   scalar_t__, int /*<<< orphan*/, int,
                                   int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ XFlush(int /*<<< orphan*/ *);
int /*<<< orphan*/ XFree(scalar_t__ *);
int XGetWindowProperty(int /*<<< orphan*/ *, int /*<<< orphan*/, scalar_t__,
                       long, long, int /*<<< orphan*/, int /*<<< orphan*/,
                       scalar_t__ *, int *, unsigned long *, unsigned long *,
                       unsigned char **);
scalar_t__ XInternAtom(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ kill(scalar_t__, int /*<<< orphan*/);
int registerInstance(Display *display) {
  Window root = DefaultRootWindow(display);
  Atom atom = XInternAtom(display, "ALOCK_INSTANCE_PID", False);
  pid_t pid = 0;
  {
    Atom ret_type;
    int ret_fmt;
    unsigned long ret_nb;
    unsigned long ret_bleft;
    pid_t *ret_data;
    int rv;
    rv = XGetWindowProperty(display, root, atom, 0L, 1L, False, XA_CARDINAL,
                            &ret_type, &ret_fmt, &ret_nb, &ret_bleft,
                            (unsigned char **)&ret_data);
    if (rv == Success && ret_type != None && ret_data) {
      pid = *ret_data;
      XFree(ret_data);
    }
    if (pid && kill(pid, 0) == 0)
      return -1;
  }
  do {
  } while (0);
  pid = getpid();
  XChangeProperty(display, root, atom, XA_CARDINAL, sizeof(pid_t) * 8,
                  PropModeReplace, (unsigned char *)&pid, 1);
  XFlush(display);
  return 0;
}


x86 assembly (clang, -O0):
<registerInstance>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <DefaultRootWindow@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    0xe4c(%rip),%rsi
call   1070 <XInternAtom@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    0x2e09(%rip),%rax
mov    (%rax),%r9d
mov    0x2de7(%rip),%rax
mov    (%rax),%r15d
lea    -0x68(%rbp),%rax
xor    %ecx,%ecx
mov    $0x1,%r8d
lea    -0x48(%rbp),%r14
lea    -0x4c(%rbp),%rbx
lea    -0x58(%rbp),%r11
lea    -0x60(%rbp),%r10
mov    %r15d,(%rsp)
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11,0x18(%rsp)
mov    %r10,0x20(%rsp)
mov    %rax,0x28(%rsp)
call   10a0 <XGetWindowProperty@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x6c(%rbp),%eax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%eax
jne    126a <registerInstance+0xea>
mov    -0x48(%rbp),%rax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
je     126a <registerInstance+0xea>
cmpq   $0x0,-0x68(%rbp)
je     126a <registerInstance+0xea>
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x68(%rbp),%rdi
call   1050 <XFree@plt>
cmpq   $0x0,-0x40(%rbp)
je     1296 <registerInstance+0x116>
mov    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
jne    1296 <registerInstance+0x116>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12f4 <registerInstance+0x174>
jmp    129b <registerInstance+0x11b>
jmp    12a0 <registerInstance+0x120>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    0x2d0b(%rip),%rax
mov    (%rax),%ecx
mov    0x2cfa(%rip),%rax
mov    (%rax),%r9d
lea    -0x40(%rbp),%rax
mov    $0x40,%r8d
mov    %rax,(%rsp)
movl   $0x1,0x8(%rsp)
call   1080 <XChangeProperty@plt>
mov    -0x28(%rbp),%rdi
call   1090 <XFlush@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<registerInstance>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
call   1040 <DefaultRootWindow@plt>
mov    %eax,%r15d
mov    0x2e40(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xe56(%rip),%rsi
mov    %rbx,%rdi
call   1070 <XInternAtom@plt>
mov    %rax,%r13
mov    0x0(%rbp),%r9d
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
lea    0x8(%rsp),%r14
lea    0x18(%rsp),%r10
lea    0x20(%rsp),%r11
lea    0x14(%rsp),%r12
mov    %rsp,%rbp
mov    $0x1,%r8d
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r13,%rdx
xor    %ecx,%ecx
push   %r14
push   %r10
push   %r11
push   %r12
push   %rbp
push   %rax
call   10a0 <XGetWindowProperty@plt>
add    $0x30,%rsp
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%eax
jne    123e <registerInstance+0xbe>
mov    (%rsp),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
je     123e <registerInstance+0xbe>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     123e <registerInstance+0xbe>
mov    (%rdi),%r12
call   1050 <XFree@plt>
test   %r12,%r12
je     123e <registerInstance+0xbe>
mov    %r12,%rdi
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
je     1290 <registerInstance+0x110>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,(%rsp)
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
mov    0x2d67(%rip),%rax
mov    (%rax),%r9d
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r13,%rdx
mov    $0x40,%r8d
push   $0x1
push   %rbp
call   1080 <XChangeProperty@plt>
add    $0x10,%rsp
mov    %rbx,%rdi
call   1090 <XFlush@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1281 <registerInstance+0x101>

LLVM IR (-O0):
@.str = [19 x i8] c"ALOCK_INSTANCE_PID\00"
@False = global i32 0
@XA_CARDINAL = global i32 0
@Success = global i32 0
@None = global i64 0
@PropModeReplace = global i32 0
define i32 @registerInstance(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i32
store i32* %0, i32** %3
%13 = load i32*, i32** %3
%14 = call i32 @DefaultRootWindow(i32* %13)
store i32 %14, i32* %4
%15 = load i32*, i32** %3
%16 = load i32, i32* @False
%17 = call i64 @XInternAtom(i32* %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %16)
store i64 %17, i64* %5
store i64 0, i64* %6
%18 = load i32*, i32** %3
%19 = load i32, i32* %4
%20 = load i64, i64* %5
%21 = load i32, i32* @False
%22 = load i32, i32* @XA_CARDINAL
%23 = bitcast i64** %11 to i8**
%24 = call i32 @XGetWindowProperty(i32* %18, i32 %19, i64 %20, i64 0, i64 1, i32 %21, i32 %22, i64* %7, i32* %8, i64* %9, i64* %10, i8** %23)
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = load i32, i32* @Success
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %40
28: ; preds = %1
%29 = load i64, i64* %7
%30 = load i64, i64* @None
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = load i64*, i64** %11
%34 = icmp ne i64* %33, null
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i64*, i64** %11
%37 = load i64, i64* %36
store i64 %37, i64* %6
%38 = load i64*, i64** %11
%39 = call i32 @XFree(i64* %38)
br label %40
40: ; preds = %35, %32, %28, %1
%41 = load i64, i64* %6
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i64, i64* %6
%45 = call i64 @kill(i64 %44, i32 0)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 -1, i32* %2
br label %61
48: ; preds = %43, %40
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49
%51 = call i64 (...) @getpid()
store i64 %51, i64* %6
%52 = load i32*, i32** %3
%53 = load i32, i32* %4
%54 = load i64, i64* %5
%55 = load i32, i32* @XA_CARDINAL
%56 = load i32, i32* @PropModeReplace
%57 = bitcast i64* %6 to i8*
%58 = call i32 @XChangeProperty(i32* %52, i32 %53, i64 %54, i32 %55, i32 64, i32 %56, i8* %57, i32 1)
%59 = load i32*, i32** %3
%60 = call i32 @XFlush(i32* %59)
store i32 0, i32* %2
br label %61
61: ; preds = %50, %47
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
@.str = [19 x i8] c"ALOCK_INSTANCE_PID\00"
@False = global i32 0
@XA_CARDINAL = global i32 0
@Success = global i32 0
@None = global i64 0
@PropModeReplace = global i32 0
define i32 @registerInstance(i32* %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64*
%8 = tail call i32 @DefaultRootWindow(i32* %0)
%9 = load i32, i32* @False
%10 = tail call i64 @XInternAtom(i32* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = bitcast i64* %2 to i8*
%12 = bitcast i64* %3 to i8*
%13 = bitcast i32* %4 to i8*
%14 = bitcast i64* %5 to i8*
%15 = bitcast i64* %6 to i8*
%16 = bitcast i64** %7 to i8*
%17 = load i32, i32* @False
%18 = load i32, i32* @XA_CARDINAL
%19 = bitcast i64** %7 to i8**
%20 = call i32 @XGetWindowProperty(i32* %0, i32 %8, i64 %10, i64 0, i64 1, i32 %17, i32 %18, i64* %3, i32* %4, i64* %5, i64* %6, i8** %19)
%21 = load i32, i32* @Success
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %38
23: ; preds = %1
%24 = load i64, i64* %3
%25 = load i64, i64* @None
%26 = icmp ne i64 %24, %25
%27 = load i64*, i64** %7
%28 = icmp ne i64* %27, null
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %30, label %38
30: ; preds = %23
%31 = load i64, i64* %27
%32 = call i32 @XFree(i64* %27)
%33 = icmp eq i64 %31, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = call i64 @kill(i64 %31, i32 0)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
br label %44
38: ; preds = %1, %23, %30, %34
%39 = call i64 (...) @getpid()
store i64 %39, i64* %2
%40 = load i32, i32* @XA_CARDINAL
%41 = load i32, i32* @PropModeReplace
%42 = call i32 @XChangeProperty(i32* %0, i32 %8, i64 %10, i32 %40, i32 64, i32 %41, i8* %11, i32 1)
%43 = call i32 @XFlush(i32* %0)
br label %44
44: ; preds = %37, %38
%45 = phi i32 [ 0, %38 ], [ -1, %37 ]
ret i32 %45
}