#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_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int GetLock(char *sLockname) {
  int dLockname = 0, i = 0;
  char sAccum[256];
  while ((dLockname <= 0) && (i < 10)) {
    dLockname = open(sLockname, O_WRONLY | O_EXCL | O_CREAT,
                     S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
    if (dLockname <= 0) {
      sleep(1);
    }
    i++;
  }
  if (dLockname < 0) {
    return -1;
  }
  sprintf(sAccum, "%d", getpid());
  write(dLockname, sAccum, strlen(sAccum));
  close(dLockname);
  return 0;
}


x86 assembly (clang, -O0):
<GetLock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x121(%rbp)
jg     11ac <GetLock+0x3c>
cmpl   $0xa,-0x18(%rbp)
setl   %al
mov    %al,-0x121(%rbp)
mov    -0x121(%rbp),%al
test   $0x1,%al
jne    11bf <GetLock+0x4f>
jmp    1235 <GetLock+0xc5>
mov    -0x10(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
mov    0x2dea(%rip),%rax
or     (%rax),%edx
mov    0x2dc9(%rip),%rax
or     (%rax),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
mov    0x2daf(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1227 <GetLock+0xb7>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118d <GetLock+0x1d>
cmpl   $0x0,-0x14(%rbp)
jge    124b <GetLock+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <GetLock+0x14b>
lea    -0x120(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x140(%rbp),%rdi
mov    %eax,%edx
lea    0xd90(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x134(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetLock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
mov    $0xffffffff,%ebx
mov    0x2e30(%rip),%r13
mov    0x2e49(%rip),%r15
mov    0x2e1a(%rip),%r12
xchg   %ax,%ax
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    0x2de5(%rip),%rax
or     (%rax),%edx
or     0x0(%r13),%edx
or     (%r15),%edx
or     (%r12),%edx
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11fc <GetLock+0x8c>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0x1,%ebx
cmp    $0x9,%ebx
jb     11a0 <GetLock+0x30>
test   %ebp,%ebp
js     124a <GetLock+0xda>
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdf3(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r14d
jmp    1235 <GetLock+0xc5>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @GetLock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %36, %1
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 10
br label %13
13: ; preds = %10, %7
%14 = phi i1 [ false, %7 ], [ %12, %10 ]
br i1 %14, label %15, label %39
15: ; preds = %13
%16 = load i8*, i8** %3
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_EXCL
%19 = or i32 %17, %18
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IWGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IROTH
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %16, i32 %21, i32 %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %15
%35 = call i32 @sleep(i32 1)
br label %36
36: ; preds = %34, %15
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %7
39: ; preds = %13
%40 = load i32, i32* %4
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
store i32 -1, i32* %2
br label %54
43: ; preds = %39
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%45 = call i32 (...) @getpid()
%46 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %4
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 %47, i8* %48, i32 %50)
%52 = load i32, i32* %4
%53 = call i32 @close(i32 %52)
store i32 0, i32* %2
br label %54
54: ; preds = %43, %42
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @GetLock(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %1, %22
%5 = phi i32 [ 0, %1 ], [ %24, %22 ]
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_EXCL
%8 = or i32 %7, %6
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %0, i32 %10, i32 %19)
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %28
22: ; preds = %4
%23 = tail call i32 @sleep(i32 1)
%24 = add nuw nsw i32 %5, 1
%25 = icmp ult i32 %5, 9
br i1 %25, label %4, label %26
26: ; preds = %22
%27 = icmp slt i32 %20, 0
br i1 %27, label %34, label %28
28: ; preds = %4, %26
%29 = tail call i32 (...) @getpid()
%30 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = call i32 @strlen(i8* %3)
%32 = call i32 @write(i32 %20, i8* %3, i32 %31)
%33 = call i32 @close(i32 %20)
br label %34
34: ; preds = %26, %28
%35 = phi i32 [ 0, %28 ], [ -1, %26 ]
ret i32 %35
}