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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int write_file(FILE *fp, char *data, size_t data_sz) {
  int ret;
  ret = fwrite(data, 1, data_sz, fp);
  if ((ret < 0) || ((size_t)ret != data_sz)) {
    fprintf(stderr, "Can't write data to file\n");
    return -errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1165 <write_file+0x45>
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     118d <write_file+0x6d>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2e55(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1194 <write_file+0x74>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbx
mov    %rdx,%rbx
mov    %rdi,%rcx
mov    %rsi,%rdi
mov    $0x1,%esi
call   1040 <fwrite@plt>
test   %eax,%eax
js     1146 <write_file+0x26>
mov    %eax,%ecx
xor    %eax,%eax
movslq %ecx,%rcx
cmp    %rbx,%rcx
jne    1146 <write_file+0x26>
pop    %rbx
ret
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
lea    0xeaa(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e76(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Can't write data to file\0A\00"
@errno = global i32 0
define i32 @write_file(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = load i64, i64* %7
%11 = load i32*, i32** %5
%12 = call i32 @fwrite(i8* %9, i32 1, i64 %10, i32* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %3
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = load i64, i64* %7
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %25
20: ; preds = %15, %3
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
store i32 %24, i32* %4
br label %26
25: ; preds = %15
store i32 0, i32* %4
br label %26
26: ; preds = %25, %20
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Can't write data to file\0A\00"
@errno = global i32 0
define i32 @write_file(i32* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @fwrite(i8* %1, i32 1, i64 %2, i32* %0)
%5 = icmp sgt i32 %4, -1
%6 = sext i32 %4 to i64
%7 = icmp eq i64 %6, %2
%8 = and i1 %5, %7
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %14
14: ; preds = %3, %9
%15 = phi i32 [ %13, %9 ], [ 0, %3 ]
ret i32 %15
}