#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*/ EIO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
scalar_t__ strnlen(char *, int);
void WriteValueToFp(FILE *fp, unsigned int value) {
  int numBytesWritten;
  char buf[33];
  snprintf(buf, 33, "%u", value);
  fseek(fp, 0, 0);
  numBytesWritten = fwrite(buf, 1, strnlen(buf, 33) + 1, fp);
  if (numBytesWritten != ((int)(strnlen(buf, 33) + 1))) {
    printf("ERROR: failed to write value to file pointer\n");
    fclose(fp);
    exit(EIO);
  }
  return;
}


x86 assembly (clang, -O0):
<WriteValueToFp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x21,%esi
lea    0xe6e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <fseek@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x21,%esi
call   1060 <strnlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rdx
add    $0x1,%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x21,%esi
call   1060 <strnlen@plt>
mov    %rax,%rcx
mov    -0x44(%rbp),%eax
add    $0x1,%rcx
cmp    %ecx,%eax
je     121f <WriteValueToFp+0xaf>
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteValueToFp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ecx
mov    %rdi,%r14
lea    0xe7c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x21,%esi
call   1050 <snprintf@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <fseek@plt>
mov    %rbx,%rdi
mov    $0x21,%esi
call   1060 <strnlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x21,%esi
call   1060 <strnlen@plt>
add    $0x1,%eax
cmp    %eax,%ebp
jne    11e0 <WriteValueToFp+0x70>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%u\00"
@.str.1 = [46 x i8] c"ERROR: failed to write value to file pointer\0A\00"
@EIO = global i32 0
define void @WriteValueToFp(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [33 x i8]
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* %4
%9 = call i32 @snprintf(i8* %7, i32 33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32*, i32** %3
%11 = call i32 @fseek(i32* %10, i32 0, i32 0)
%12 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%14 = call i64 @strnlen(i8* %13, i32 33)
%15 = add nsw i64 %14, 1
%16 = load i32*, i32** %3
%17 = call i32 @fwrite(i8* %12, i32 1, i64 %15, i32* %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%20 = call i64 @strnlen(i8* %19, i32 33)
%21 = add nsw i64 %20, 1
%22 = trunc i64 %21 to i32
%23 = icmp ne i32 %18, %22
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32*, i32** %3
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* @EIO
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %2
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%u\00"
@EIO = global i32 0
@str = [45 x i8] c"ERROR: failed to write value to file pointer\00"
define void @WriteValueToFp(i32* nocapture %0, i32 %1) {
%3 = alloca [33 x i8]
%4 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = call i32 @fseek(i32* %0, i32 0, i32 0)
%7 = call i64 @strnlen(i8* %4, i32 33)
%8 = add nsw i64 %7, 1
%9 = call i32 @fwrite(i8* %4, i32 1, i64 %8, i32* %0)
%10 = call i64 @strnlen(i8* %4, i32 33)
%11 = trunc i64 %10 to i32
%12 = add i32 %11, 1
%13 = icmp eq i32 %9, %12
br i1 %13, label %19, label %14
14: ; preds = %2
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%16 = call i32 @fclose(i32* %0)
%17 = load i32, i32* @EIO
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
ret void
}