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

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int, int);
int sdb_lock(const char *s) {
  int ret;
  if (!s)
    return 0;
  ret = open(s, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL, 0644);
  if (ret == -1)
    return 0;
  close(ret);
  return 1;
}


x86 assembly (clang, -O0):
<sdb_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1143 <sdb_lock+0x23>
movl   $0x0,-0x4(%rbp)
jmp    119d <sdb_lock+0x7d>
mov    -0x10(%rbp),%rdi
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118e <sdb_lock+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    119d <sdb_lock+0x7d>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdb_lock>:
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     1167 <sdb_lock+0x47>
mov    0x2ec9(%rip),%rax
mov    0x2eba(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e7f(%rip),%rax
or     (%rax),%esi
mov    0x2e86(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1167 <sdb_lock+0x47>
mov    %eax,%edi
call   1030 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
define i32 @sdb_lock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i32 0, i32* %2
br label %24
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @O_WRONLY
%14 = or i32 %12, %13
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %9, i32 %16, i32 420)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %8
store i32 0, i32* %2
br label %24
21: ; preds = %8
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
store i32 1, i32* %2
br label %24
24: ; preds = %21, %20, %7
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
define i32 @sdb_lock(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %5, %4
%7 = load i32, i32* @O_WRONLY
%8 = or i32 %6, %7
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10, i32 420)
%12 = icmp eq i32 %11, -1
br i1 %12, label %15, label %13
13: ; preds = %3
%14 = tail call i32 @close(i32 %11)
br label %15
15: ; preds = %3, %1, %13
%16 = phi i32 [ 1, %13 ], [ 0, %1 ], [ 0, %3 ]
ret i32 %16
}