#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*/ uint8_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int);
int snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t write(int, int /*<<< orphan*/ *, size_t);
int write_brec_portion(char *output_dir, char *filename, uint8_t *data,
                       size_t length) {
  char pathname[1024];
  if (snprintf(pathname, 1024, "%s/%s", output_dir, filename) >= (1024 - 1)) {
    fprintf(stderr, "Pathname too long\n");
    return 1;
  }
  int handle = open(pathname, O_CREAT | O_WRONLY, 0666);
  if (handle < 0) {
    fprintf(stderr, "Couldn't open %s: %s\n", pathname, strerror(errno));
    return -1;
  }
  if (write(handle, data, length) != length) {
    fprintf(stderr, "Couldn't write %s: %s\n", pathname, strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_brec_portion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xe7a(%rip),%rdx
call   1040 <snprintf@plt>
cmp    $0x3ff,%eax
jl     11b9 <write_brec_portion+0x69>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bd <write_brec_portion+0x16d>
lea    -0x430(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
jge    1244 <write_brec_portion+0xf4>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rdx
mov    %rax,%rcx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <write_brec_portion+0x16d>
mov    -0x434(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x28(%rbp),%rax
je     12b6 <write_brec_portion+0x166>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x454(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rdx
mov    %rax,%rcx
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <write_brec_portion+0x16d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_brec_portion>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r8
mov    %rdi,%rcx
lea    0xe93(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1040 <snprintf@plt>
cmp    $0x3ff,%eax
jl     11a2 <write_brec_portion+0x52>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    122e <write_brec_portion+0xde>
mov    0x2e4f(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11fc <write_brec_portion+0xac>
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rbx,%rcx
je     122e <write_brec_portion+0xde>
mov    0x2df5(%rip),%rax
mov    (%rax),%ebx
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe35(%rip),%rsi
jmp    121a <write_brec_portion+0xca>
mov    0x2dd5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdff(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Pathname too long\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [23 x i8] c"Couldn't write %s: %s\0A\00"
define i32 @write_brec_portion(i8* %0, i8* %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca [1024 x i8]
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i64 %3, i64* %9
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%13 = load i8*, i8** %6
%14 = load i8*, i8** %7
%15 = call i32 @snprintf(i8* %12, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14)
%16 = icmp sge i32 %15, 1023
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %48
20: ; preds = %4
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_WRONLY
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %21, i32 %24, i32 438)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %30, i8* %32)
store i32 -1, i32* %5
br label %48
34: ; preds = %20
%35 = load i32, i32* %11
%36 = load i32*, i32** %8
%37 = load i64, i64* %9
%38 = call i64 @write(i32 %35, i32* %36, i64 %37)
%39 = load i64, i64* %9
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %47
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %43, i8* %45)
store i32 -1, i32* %5
br label %48
47: ; preds = %34
store i32 0, i32* %5
br label %48
48: ; preds = %47, %41, %28, %17
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Pathname too long\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [23 x i8] c"Couldn't write %s: %s\0A\00"
define i32 @write_brec_portion(i8* %0, i8* %1, i32* nocapture %2, i64 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = icmp sgt i32 %7, 1022
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %31
12: ; preds = %4
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %14, %13
%16 = call i32 @open(i8* %6, i32 %15, i32 438)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %21)
br label %31
23: ; preds = %12
%24 = call i64 @write(i32 %16, i32* %2, i64 %3)
%25 = icmp eq i64 %24, %3
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %29)
br label %31
31: ; preds = %18, %26, %23, %9
%32 = phi i32 [ 1, %9 ], [ -1, %18 ], [ -1, %26 ], [ 0, %23 ]
ret i32 %32
}