#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 */
struct synceddir_ctx {
  int dummy;
};
struct synceddir {
  int dummy;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ _sdir_grab(struct synceddir *);
struct synceddir *_sdir_new(struct synceddir_ctx *, char const *);
int /*<<< orphan*/ _sdirctx_lock(struct synceddir_ctx *);
int /*<<< orphan*/ _sdirctx_unlock(struct synceddir_ctx *);
int /*<<< orphan*/ _sdirlist_append(struct synceddir_ctx *, struct synceddir *);
struct synceddir *_sdirlist_get(struct synceddir_ctx *, char const *);
int synced_dir_register(struct synceddir_ctx *ctx, const char *path,
                        struct synceddir **sdirp, bool *is_responsiblep) {
  int ret = EXIT_FAILURE;
  struct synceddir *sdir = NULL;
  bool responsible = false;
  _sdirctx_lock(ctx);
  sdir = _sdirlist_get(ctx, path);
  if (sdir == NULL) {
    responsible = true;
    sdir = _sdir_new(ctx, path);
    if (sdir == NULL)
      goto end;
    _sdirlist_append(ctx, sdir);
  }
  _sdir_grab(sdir);
  if (is_responsiblep)
    *is_responsiblep = responsible;
  *sdirp = sdir;
  ret = EXIT_SUCCESS;
end:
  _sdirctx_unlock(ctx);
  return ret;
}


x86 assembly (clang, -O0):
<synced_dir_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <_sdirctx_lock@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <_sdirlist_get@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ed <synced_dir_register+0x8d>
movl   $0x1,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <_sdir_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e0 <synced_dir_register+0x80>
jmp    1221 <synced_dir_register+0xc1>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <_sdirlist_append@plt>
mov    -0x30(%rbp),%rdi
call   1060 <_sdir_grab@plt>
cmpq   $0x0,-0x20(%rbp)
je     120a <synced_dir_register+0xaa>
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <_sdirctx_unlock@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<synced_dir_register>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%r12
mov    0x2e62(%rip),%rax
mov    (%rax),%r13d
call   1050 <_sdirctx_lock@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1040 <_sdirlist_get@plt>
test   %rax,%rax
je     119d <synced_dir_register+0x3d>
mov    %rax,%rbx
xor    %ebp,%ebp
jmp    11c0 <synced_dir_register+0x60>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1030 <_sdir_new@plt>
test   %rax,%rax
je     11dd <synced_dir_register+0x7d>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
call   1070 <_sdirlist_append@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <_sdir_grab@plt>
test   %r15,%r15
je     11d0 <synced_dir_register+0x70>
mov    %ebp,(%r15)
mov    %rbx,(%r14)
mov    0x2dfe(%rip),%rax
mov    (%rax),%r13d
mov    %r12,%rdi
call   1080 <_sdirctx_unlock@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.synceddir_ctx = type { i32 }
%struct.synceddir = type { i32 }
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @synced_dir_register(%struct.synceddir_ctx* %0, i8* %1, %struct.synceddir** %2, i32* %3) {
%5 = alloca %struct.synceddir_ctx*
%6 = alloca i8*
%7 = alloca %struct.synceddir**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.synceddir*
%11 = alloca i32
store %struct.synceddir_ctx* %0, %struct.synceddir_ctx** %5
store i8* %1, i8** %6
store %struct.synceddir** %2, %struct.synceddir*** %7
store i32* %3, i32** %8
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %9
store %struct.synceddir* null, %struct.synceddir** %10
store i32 0, i32* %11
%13 = load %struct.synceddir_ctx*, %struct.synceddir_ctx** %5
%14 = call i32 @_sdirctx_lock(%struct.synceddir_ctx* %13)
%15 = load %struct.synceddir_ctx*, %struct.synceddir_ctx** %5
%16 = load i8*, i8** %6
%17 = call %struct.synceddir* @_sdirlist_get(%struct.synceddir_ctx* %15, i8* %16)
store %struct.synceddir* %17, %struct.synceddir** %10
%18 = load %struct.synceddir*, %struct.synceddir** %10
%19 = icmp eq %struct.synceddir* %18, null
br i1 %19, label %20, label %31
20: ; preds = %4
store i32 1, i32* %11
%21 = load %struct.synceddir_ctx*, %struct.synceddir_ctx** %5
%22 = load i8*, i8** %6
%23 = call %struct.synceddir* @_sdir_new(%struct.synceddir_ctx* %21, i8* %22)
store %struct.synceddir* %23, %struct.synceddir** %10
%24 = load %struct.synceddir*, %struct.synceddir** %10
%25 = icmp eq %struct.synceddir* %24, null
br i1 %25, label %26, label %27
26: ; preds = %20
br label %43
27: ; preds = %20
%28 = load %struct.synceddir_ctx*, %struct.synceddir_ctx** %5
%29 = load %struct.synceddir*, %struct.synceddir** %10
%30 = call i32 @_sdirlist_append(%struct.synceddir_ctx* %28, %struct.synceddir* %29)
br label %31
31: ; preds = %27, %4
%32 = load %struct.synceddir*, %struct.synceddir** %10
%33 = call i32 @_sdir_grab(%struct.synceddir* %32)
%34 = load i32*, i32** %8
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* %11
%38 = load i32*, i32** %8
store i32 %37, i32* %38
br label %39
39: ; preds = %36, %31
%40 = load %struct.synceddir*, %struct.synceddir** %10
%41 = load %struct.synceddir**, %struct.synceddir*** %7
store %struct.synceddir* %40, %struct.synceddir** %41
%42 = load i32, i32* @EXIT_SUCCESS
store i32 %42, i32* %9
br label %43
43: ; preds = %39, %26
%44 = load %struct.synceddir_ctx*, %struct.synceddir_ctx** %5
%45 = call i32 @_sdirctx_unlock(%struct.synceddir_ctx* %44)
%46 = load i32, i32* %9
ret i32 %46
}

LLVM IR (-O2):
%struct.synceddir_ctx = type { i32 }
%struct.synceddir = type { i32 }
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @synced_dir_register(%struct.synceddir_ctx* %0, i8* %1, %struct.synceddir** nocapture writeonly %2, i32* writeonly %3) {
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @_sdirctx_lock(%struct.synceddir_ctx* %0)
%7 = tail call %struct.synceddir* @_sdirlist_get(%struct.synceddir_ctx* %0, i8* %1)
%8 = icmp eq %struct.synceddir* %7, null
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = tail call %struct.synceddir* @_sdir_new(%struct.synceddir_ctx* %0, i8* %1)
%11 = icmp eq %struct.synceddir* %10, null
br i1 %11, label %22, label %12
12: ; preds = %9
%13 = tail call i32 @_sdirlist_append(%struct.synceddir_ctx* %0, %struct.synceddir* %10)
br label %14
14: ; preds = %12, %4
%15 = phi %struct.synceddir* [ %10, %12 ], [ %7, %4 ]
%16 = phi i32 [ 1, %12 ], [ 0, %4 ]
%17 = tail call i32 @_sdir_grab(%struct.synceddir* %15)
%18 = icmp eq i32* %3, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 %16, i32* %3
br label %20
20: ; preds = %19, %14
store %struct.synceddir* %15, %struct.synceddir** %2
%21 = load i32, i32* @EXIT_SUCCESS
br label %22
22: ; preds = %9, %20
%23 = phi i32 [ %5, %9 ], [ %21, %20 ]
%24 = tail call i32 @_sdirctx_unlock(%struct.synceddir_ctx* %0)
ret i32 %23
}