#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_IWUSR;
int /*<<< orphan*/ close(int);
int do_allocate(int, long long);
int open(char const *, int, int);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int allocate(const char *filename, long long size, int force) {
  int flags = O_WRONLY | O_CREAT;
  if (!force)
    flags |= O_EXCL;
  int fd = open(filename, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0) {
    perror(0);
    return -1;
  }
  int ret = do_allocate(fd, size);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<allocate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e95(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1181 <allocate+0x41>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    0x2e48(%rip),%rax
or     (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
mov    0x2e16(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d1 <allocate+0x91>
xor    %edi,%edi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <allocate+0xae>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <do_allocate@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e5b(%rip),%rax
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %eax,%eax
test   %edx,%edx
jne    1167 <allocate+0x27>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    0x2e4a(%rip),%rcx
mov    0x2e6b(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e48(%rip),%rcx
or     (%rcx),%edx
or     %eax,%esi
mov    0x2e35(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
test   %eax,%eax
js     11af <allocate+0x6f>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1040 <do_allocate@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
jmp    11bb <allocate+0x7b>
xor    %edi,%edi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @allocate(i8* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
store i32 %13, i32* %8
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %3
%17 = load i32, i32* @O_EXCL
%18 = load i32, i32* %8
%19 = or i32 %18, %17
store i32 %19, i32* %8
br label %20
20: ; preds = %16, %3
%21 = load i8*, i8** %5
%22 = load i32, i32* %8
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IROTH
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %21, i32 %22, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @perror(i32 0)
store i32 -1, i32* %4
br label %42
35: ; preds = %20
%36 = load i32, i32* %9
%37 = load i64, i64* %6
%38 = call i32 @do_allocate(i32 %36, i64 %37)
store i32 %38, i32* %10
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %10
store i32 %41, i32* %4
br label %42
42: ; preds = %35, %33
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @allocate(i8* nocapture %0, i64 %1, i32 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = icmp eq i32 %2, 0
%8 = load i32, i32* @O_EXCL
%9 = select i1 %7, i32 %8, i32 0
%10 = or i32 %6, %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_IROTH
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %0, i32 %10, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = tail call i32 @perror(i32 0)
br label %25
22: ; preds = %3
%23 = tail call i32 @do_allocate(i32 %18, i64 %1)
%24 = tail call i32 @close(i32 %18)
br label %25
25: ; preds = %22, %20
%26 = phi i32 [ -1, %20 ], [ %23, %22 ]
ret i32 %26
}