#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 /*<<< orphan*/ close(int);
int creat(char const *, int);
int /*<<< orphan*/ perror(char const *);
scalar_t__ write(int, int /*<<< orphan*/ const *, size_t);
int write_data(const char *filename, const uint8_t *data, size_t length) {
  int outfd = creat(filename, 0644);
  if (outfd < 0) {
    perror(filename);
    return 0;
  }
  if (write(outfd, data, length) < 0) {
    perror(filename);
    close(outfd);
    return 0;
  }
  close(outfd);
  return 1;
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1a4,%esi
call   1060 <creat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1184 <write_data+0x44>
mov    -0x10(%rbp),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ca <write_data+0x8a>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11bb <write_data+0x7b>
mov    -0x10(%rbp),%rdi
call   1050 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ca <write_data+0x8a>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x1a4,%esi
call   1060 <creat@plt>
test   %eax,%eax
js     1189 <write_data+0x49>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1195 <write_data+0x55>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    117e <write_data+0x3e>
mov    %r14,%rdi
call   1050 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    117e <write_data+0x3e>

LLVM IR (-O0):
define i32 @write_data(i8* %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = call i32 @creat(i8* %9, i32 420)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @perror(i8* %14)
store i32 0, i32* %4
br label %30
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i32*, i32** %6
%19 = load i64, i64* %7
%20 = call i64 @write(i32 %17, i32* %18, i64 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8*, i8** %5
%24 = call i32 @perror(i8* %23)
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
store i32 0, i32* %4
br label %30
27: ; preds = %16
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
store i32 1, i32* %4
br label %30
30: ; preds = %27, %22, %13
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
define i32 @write_data(i8* %0, i32* nocapture %1, i64 %2) {
%4 = tail call i32 @creat(i8* %0, i32 420)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* %0)
br label %16
8: ; preds = %3
%9 = tail call i64 @write(i32 %4, i32* %1, i64 %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* %0)
%13 = tail call i32 @close(i32 %4)
br label %16
14: ; preds = %8
%15 = tail call i32 @close(i32 %4)
br label %16
16: ; preds = %14, %11, %6
%17 = phi i32 [ 0, %6 ], [ 0, %11 ], [ 1, %14 ]
ret i32 %17
}