#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 */
typedef int /*<<< orphan*/ uint32_t;
struct razor_set {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int close(int);
int open(char const *, int, int);
int razor_set_write_to_fd(struct razor_set *, int, int /*<<< orphan*/);
__attribute__((visibility("default"))) int
razor_set_write(struct razor_set *set, const char *filename,
                uint32_t sections) {
  int fd, status;
  fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  if (fd < 0)
    return -1;
  status = razor_set_write_to_fd(set, fd, sections);
  if (status) {
    close(fd);
    return status;
  }
  return close(fd);
}


x86 assembly (clang, -O0):
<razor_set_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1185 <razor_set_write+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <razor_set_write+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1030 <razor_set_write_to_fd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11b4 <razor_set_write+0x84>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <razor_set_write+0x8f>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<razor_set_write>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ebd(%rip),%rcx
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%r14d
mov    %rdi,%rbx
mov    0x2e9d(%rip),%rcx
or     (%rcx),%eax
mov    %rsi,%rdi
mov    %eax,%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
test   %eax,%eax
js     1187 <razor_set_write+0x57>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r14d,%edx
call   1030 <razor_set_write_to_fd@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %ebx,%ebx
cmovne %ebx,%eax
jmp    118c <razor_set_write+0x5c>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.razor_set = type { i32 }
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @razor_set_write(%struct.razor_set* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.razor_set*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.razor_set* %0, %struct.razor_set** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 438)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %34
20: ; preds = %3
%21 = load %struct.razor_set*, %struct.razor_set** %5
%22 = load i32, i32* %8
%23 = load i32, i32* %7
%24 = call i32 @razor_set_write_to_fd(%struct.razor_set* %21, i32 %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %9
store i32 %30, i32* %4
br label %34
31: ; preds = %20
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
store i32 %33, i32* %4
br label %34
34: ; preds = %31, %27, %19
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.razor_set = type { i32 }
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @razor_set_write(%struct.razor_set* %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %1, i32 %8, i32 438)
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %3
%12 = tail call i32 @razor_set_write_to_fd(%struct.razor_set* %0, i32 %9, i32 %2)
%13 = icmp eq i32 %12, 0
%14 = tail call i32 @close(i32 %9)
%15 = select i1 %13, i32 %14, i32 %12
br label %16
16: ; preds = %11, %3
%17 = phi i32 [ -1, %3 ], [ %15, %11 ]
ret i32 %17
}