#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*/ assert(int);
int ferror(int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
size_t strlen(char *);
int /*<<< orphan*/ warn(char *);
int bee_fnprint(FILE *fh, size_t n, char *str) {
  size_t m;
  if (ferror(fh))
    return 0;
  m = strlen(str);
  assert(n <= m);
  if (!n)
    n = m;
  if (!n)
    return 1;
  m = fwrite(str, sizeof(*str), n, fh);
  if (m != n) {
    assert(ferror(fh));
    warn("fwrite");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<bee_fnprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%eax
je     1182 <bee_fnprint+0x32>
movl   $0x0,-0x4(%rbp)
jmp    1225 <bee_fnprint+0xd5>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <bee_fnprint+0x67>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ce <bee_fnprint+0x7e>
movl   $0x1,-0x4(%rbp)
jmp    1225 <bee_fnprint+0xd5>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     121e <bee_fnprint+0xce>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
mov    %eax,%edi
call   1060 <assert@plt>
lea    0xdf3(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    1225 <bee_fnprint+0xd5>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bee_fnprint>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <ferror@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1178 <bee_fnprint+0x28>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
xor    %edi,%edi
cmp    %rbx,%rax
setae  %dil
call   1060 <assert@plt>
test   %rbx,%rbx
cmove  %rbp,%rbx
mov    $0x1,%ebp
test   %rbx,%rbx
je     116b <bee_fnprint+0x1b>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1070 <fwrite@plt>
cmp    %rbx,%rax
je     116b <bee_fnprint+0x1b>
mov    %r14,%rdi
call   1040 <ferror@plt>
mov    %eax,%edi
call   1060 <assert@plt>
lea    0xe30(%rip),%rdi
call   1030 <warn@plt>
xor    %ebp,%ebp
jmp    116b <bee_fnprint+0x1b>

LLVM IR (-O0):
@.str = [7 x i8] c"fwrite\00"
define i32 @bee_fnprint(i32* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i32*, i32** %5
%10 = call i32 @ferror(i32* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 0, i32* %4
br label %43
13: ; preds = %3
%14 = load i8*, i8** %7
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %8
%16 = load i64, i64* %6
%17 = load i64, i64* %8
%18 = icmp ule i64 %16, %17
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i64, i64* %6
%22 = icmp ne i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %13
%24 = load i64, i64* %8
store i64 %24, i64* %6
br label %25
25: ; preds = %23, %13
%26 = load i64, i64* %6
%27 = icmp ne i64 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %25
store i32 1, i32* %4
br label %43
29: ; preds = %25
%30 = load i8*, i8** %7
%31 = load i64, i64* %6
%32 = load i32*, i32** %5
%33 = call i64 @fwrite(i8* %30, i32 1, i64 %31, i32* %32)
store i64 %33, i64* %8
%34 = load i64, i64* %8
%35 = load i64, i64* %6
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %29
%38 = load i32*, i32** %5
%39 = call i32 @ferror(i32* %38)
%40 = call i32 @assert(i32 %39)
%41 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %43
42: ; preds = %29
store i32 1, i32* %4
br label %43
43: ; preds = %42, %37, %28, %12
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@.str = [7 x i8] c"fwrite\00"
define i32 @bee_fnprint(i32* nocapture %0, i64 %1, i8* nocapture %2) {
%4 = tail call i32 @ferror(i32* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %3
%7 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%8 = icmp uge i64 %7, %1
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = icmp eq i64 %1, 0
%12 = select i1 %11, i64 %7, i64 %1
%13 = icmp eq i64 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %6
%15 = tail call i64 @fwrite(i8* %2, i32 1, i64 %12, i32* %0)
%16 = icmp eq i64 %15, %12
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = tail call i32 @ferror(i32* %0)
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %14, %6, %3, %17
%22 = phi i32 [ 0, %17 ], [ 0, %3 ], [ 1, %6 ], [ 1, %14 ]
ret i32 %22
}