#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 flock {
  int /*<<< orphan*/ l_type;
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, int);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int _create_locked_cmd(char *path, int cmd) {
  int ret = 0, fd;
  struct flock flk;
  fd = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (0 > fd) {
    fprintf(stderr, "Error opening file %s: %s (%d)\n", path, strerror(errno),
            errno);
    return -1;
  }
  flk.l_whence = SEEK_SET;
  flk.l_start = 0;
  flk.l_len = 0;
  flk.l_type = F_WRLCK;
  do {
    if (0 == (ret = fcntl(fd, cmd, &flk))) {
      break;
    } else if (EINTR != errno) {
      fprintf(stderr, "Error locking file %s: %s (%d)\n", path, strerror(errno),
              errno);
      close(fd);
      return -errno;
    }
  } while (EINTR == errno);
  if (0 == ret)
    return fd;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<_create_locked_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jle    11ef <_create_locked_cmd+0x9f>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
mov    0x2dec(%rip),%rax
mov    (%rax),%r8d
lea    0xe22(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f5 <_create_locked_cmd+0x1a5>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jne    123a <_create_locked_cmd+0xea>
jmp    12d1 <_create_locked_cmd+0x181>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%eax
je     12af <_create_locked_cmd+0x15f>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
mov    0x2d3b(%rip),%rax
mov    (%rax),%r8d
lea    0xd91(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    0x2d1b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <_create_locked_cmd+0x1a5>
jmp    12b4 <_create_locked_cmd+0x164>
jmp    12b9 <_create_locked_cmd+0x169>
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%eax
je     1217 <_create_locked_cmd+0xc7>
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jne    12e7 <_create_locked_cmd+0x197>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <_create_locked_cmd+0x1a5>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_create_locked_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e8e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1060 <open@plt>
test   %eax,%eax
js     121f <_create_locked_cmd+0xcf>
mov    %eax,%ebp
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r15
mov    0x2dec(%rip),%r13
mov    0x2df5(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r15,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     1254 <_create_locked_cmd+0x104>
mov    (%r12),%edi
cmp    %edi,0x0(%r13)
je     11d0 <_create_locked_cmd+0x80>
mov    0x2dd7(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
mov    (%r12),%r8d
lea    0xe1d(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
sub    (%r12),%ebp
jmp    1254 <_create_locked_cmd+0x104>
mov    0x2da2(%rip),%rax
mov    (%rax),%ebx
mov    0x2d91(%rip),%rbp
mov    0x0(%rbp),%edi
call   1070 <strerror@plt>
mov    0x0(%rbp),%r8d
lea    0xdbe(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Error opening file %s: %s (%d)\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@F_WRLCK = global i32 0
@EINTR = global i32 0
@.str.1 = [32 x i8] c"Error locking file %s: %s (%d)\0A\00"
define i32 @_create_locked_cmd(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.flock
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %9, i32 %12, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp sgt i32 0, %17
br i1 %18, label %19, label %26
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %4
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = load i32, i32* @errno
%25 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %23, i32 %24)
store i32 -1, i32* %3
br label %69
26: ; preds = %2
%27 = load i32, i32* @SEEK_SET
%28 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i64 0, i64* %30
%31 = load i32, i32* @F_WRLCK
%32 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %31, i32* %32
br label %33
33: ; preds = %56, %26
%34 = load i32, i32* %7
%35 = load i32, i32* %5
%36 = call i32 @fcntl(i32 %34, i32 %35, %struct.flock* %8)
store i32 %36, i32* %6
%37 = icmp eq i32 0, %36
br i1 %37, label %38, label %39
38: ; preds = %33
br label %60
39: ; preds = %33
%40 = load i32, i32* @EINTR
%41 = load i32, i32* @errno
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %4
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = load i32, i32* @errno
%49 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* %47, i32 %48)
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @errno
%53 = sub nsw i32 0, %52
store i32 %53, i32* %3
br label %69
54: ; preds = %39
br label %55
55: ; preds = %54
br label %56
56: ; preds = %55
%57 = load i32, i32* @EINTR
%58 = load i32, i32* @errno
%59 = icmp eq i32 %57, %58
br i1 %59, label %33, label %60
60: ; preds = %56, %38
%61 = load i32, i32* %6
%62 = icmp eq i32 0, %61
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = load i32, i32* %7
store i32 %64, i32* %3
br label %69
65: ; preds = %60
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %6
store i32 %68, i32* %3
br label %69
69: ; preds = %65, %63, %43, %19
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Error opening file %s: %s (%d)\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@F_WRLCK = global i32 0
@EINTR = global i32 0
@.str.1 = [32 x i8] c"Error locking file %s: %s (%d)\0A\00"
define i32 @_create_locked_cmd(i8* %0, i32 %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %0, i32 %7, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = load i32, i32* @errno
%18 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %16, i32 %17)
br label %41
19: ; preds = %2
%20 = load i32, i32* @SEEK_SET
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%23 = bitcast i64* %22 to i8*
%24 = load i32, i32* @F_WRLCK
%25 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %24, i32* %25
br label %26
26: ; preds = %29, %19
%27 = call i32 @fcntl(i32 %11, i32 %1, %struct.flock* %3)
%28 = icmp eq i32 %27, 0
br i1 %28, label %41, label %29
29: ; preds = %26
%30 = load i32, i32* @EINTR
%31 = load i32, i32* @errno
%32 = icmp eq i32 %30, %31
br i1 %32, label %26, label %33
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i8* @strerror(i32 %31)
%36 = load i32, i32* @errno
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %35, i32 %36)
%38 = call i32 @close(i32 %11)
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
br label %41
41: ; preds = %26, %33, %13
%42 = phi i32 [ -1, %13 ], [ %40, %33 ], [ %11, %26 ]
ret i32 %42
}