#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ juniper_fpga_formatpath(int, int, char *, char *, int);
int open(char *, int /*<<< orphan*/);
int write(int, char *, int);
int juniper_fpga_partition_write_bytes(int devNo, int partNo, char *flagName,
                                       char *storage, int length) {
  char path[100];
  int fd;
  int wrAmt;
  ;
  juniper_fpga_formatpath(devNo, partNo, flagName, path, 100);
  fd = open(path, O_WRONLY);
  if (fd == -1)
    return -1;
  wrAmt = write(fd, storage, length);
  close(fd);
  if (wrAmt != length)
    return -2;
  return 0;
}


x86 assembly (clang, -O0):
<juniper_fpga_partition_write_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    -0x90(%rbp),%rcx
mov    $0x64,%r8d
call   1030 <juniper_fpga_formatpath@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    11ad <juniper_fpga_partition_write_bytes+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <juniper_fpga_partition_write_bytes+0xb2>
mov    -0x94(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x98(%rbp)
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
mov    -0x98(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     11eb <juniper_fpga_partition_write_bytes+0xab>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11f2 <juniper_fpga_partition_write_bytes+0xb2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<juniper_fpga_partition_write_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %r8d,%r15d
mov    %rcx,%r14
mov    %rsp,%rbp
mov    %rbp,%rcx
mov    $0x64,%r8d
call   1030 <juniper_fpga_formatpath@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     119e <juniper_fpga_partition_write_bytes+0x5e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
cmp    %r15d,%ebx
sete   %al
add    %eax,%eax
add    $0xfffffffe,%eax
jmp    11a3 <juniper_fpga_partition_write_bytes+0x63>
mov    $0xffffffff,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
define i32 @juniper_fpga_partition_write_bytes(i32 %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [100 x i8]
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = load i8*, i8** %9
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%19 = call i32 @juniper_fpga_formatpath(i32 %15, i32 %16, i8* %17, i8* %18, i32 100)
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %5
store i32 -1, i32* %6
br label %38
26: ; preds = %5
%27 = load i32, i32* %13
%28 = load i8*, i8** %10
%29 = load i32, i32* %11
%30 = call i32 @write(i32 %27, i8* %28, i32 %29)
store i32 %30, i32* %14
%31 = load i32, i32* %13
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %14
%34 = load i32, i32* %11
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %26
store i32 -2, i32* %6
br label %38
37: ; preds = %26
store i32 0, i32* %6
br label %38
38: ; preds = %37, %36, %25
%39 = load i32, i32* %6
ret i32 %39
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
define i32 @juniper_fpga_partition_write_bytes(i32 %0, i32 %1, i8* %2, i8* nocapture %3, i32 %4) {
%6 = alloca [100 x i8]
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%8 = call i32 @juniper_fpga_formatpath(i32 %0, i32 %1, i8* %2, i8* %7, i32 100)
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %17, label %12
12: ; preds = %5
%13 = call i32 @write(i32 %10, i8* %3, i32 %4)
%14 = call i32 @close(i32 %10)
%15 = icmp eq i32 %13, %4
%16 = select i1 %15, i32 0, i32 -2
br label %17
17: ; preds = %12, %5
%18 = phi i32 [ -1, %5 ], [ %16, %12 ]
ret i32 %18
}