#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 /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ errno;
size_t fwrite(void const *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_msg(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int write_data(FILE *file, const void *data, size_t size) {
  size_t result;
  if (size == 0)
    return 1;
  result = fwrite(data, 1, size, file);
  if (result == 0) {
    log_msg(LOG_ERR, "write failed: %s", strerror(errno));
    return 0;
  } else if (result < size) {
    log_msg(LOG_ERR, "short write (disk full?)");
    return 0;
  } else {
    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)
cmpq   $0x0,-0x20(%rbp)
jne    115b <write_data+0x2b>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <write_data+0xc1>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b9 <write_data+0x89>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1030 <log_msg@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <write_data+0xc1>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11ea <write_data+0xba>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1030 <log_msg@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <write_data+0xc1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %rbx
push   %rax
test   %rdx,%rdx
je     1175 <write_data+0x45>
mov    %rdx,%rbx
mov    %rdi,%rcx
mov    $0x1,%ebp
mov    %rsi,%rdi
mov    $0x1,%esi
call   1040 <fwrite@plt>
test   %rax,%rax
je     117c <write_data+0x4c>
cmp    %rbx,%rax
jae    11a7 <write_data+0x77>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xea7(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <log_msg@plt>
jmp    11a7 <write_data+0x77>
mov    $0x1,%ebp
jmp    11a7 <write_data+0x77>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe66(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <log_msg@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [17 x i8] c"write failed: %s\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"short write (disk full?)\00"
define i32 @write_data(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 1, i32* %4
br label %32
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = load i64, i64* %7
%15 = load i32*, i32** %5
%16 = call i64 @fwrite(i8* %13, i32 1, i64 %14, i32* %15)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 (i32, i8*, ...) @log_msg(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 0, i32* %4
br label %32
24: ; preds = %12
%25 = load i64, i64* %8
%26 = load i64, i64* %7
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @LOG_ERR
%30 = call i32 (i32, i8*, ...) @log_msg(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %32
31: ; preds = %24
store i32 1, i32* %4
br label %32
32: ; preds = %31, %28, %19, %11
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [17 x i8] c"write failed: %s\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"short write (disk full?)\00"
define i32 @write_data(i32* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %18, label %5
5: ; preds = %3
%6 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = load i32, i32* @LOG_ERR
%10 = load i32, i32* @errno
%11 = tail call i32 @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, ...) @log_msg(i32 %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %18
13: ; preds = %5
%14 = icmp ult i64 %6, %2
br i1 %14, label %15, label %18
15: ; preds = %13
%16 = load i32, i32* @LOG_ERR
%17 = tail call i32 (i32, i8*, ...) @log_msg(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %13, %3, %15, %8
%19 = phi i32 [ 0, %8 ], [ 0, %15 ], [ 1, %3 ], [ 1, %13 ]
ret i32 %19
}