#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*/ fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fflush(int /*<<< orphan*/ *);
scalar_t__ fgetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int);
scalar_t__ fsetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t fwrite(void const *, int, size_t, int /*<<< orphan*/ *);
size_t optk_fwrite_at(FILE *stream, long offset, int whence, const void *block,
                      size_t blocksize) {
  fpos_t pos;
  size_t result;
  if (fgetpos(stream, &pos) != 0 || fflush(stream) != 0)
    return 0;
  if (fseek(stream, offset, whence) == 0)
    result = fwrite(block, 1, blocksize, stream);
  else
    result = 0;
  if (fflush(stream) != 0)
    result = 0;
  if (fsetpos(stream, &pos) != 0)
    result = 0;
  return result;
}


x86 assembly (clang, -O0):
<optk_fwrite_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1030 <fgetpos@plt>
cmp    $0x0,%rax
jne    1195 <optk_fwrite_at+0x45>
mov    -0x10(%rbp),%rdi
call   1040 <fflush@plt>
cmp    $0x0,%rax
je     11a2 <optk_fwrite_at+0x52>
movq   $0x0,-0x8(%rbp)
jmp    1225 <optk_fwrite_at+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <fseek@plt>
cmp    $0x0,%rax
jne    11db <optk_fwrite_at+0x8b>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x40(%rbp)
jmp    11e3 <optk_fwrite_at+0x93>
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fflush@plt>
cmp    $0x0,%rax
je     11fe <optk_fwrite_at+0xae>
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1060 <fsetpos@plt>
cmp    $0x0,%rax
je     121d <optk_fwrite_at+0xcd>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optk_fwrite_at>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
call   1030 <fgetpos@plt>
test   %rax,%rax
jne    1187 <optk_fwrite_at+0x37>
mov    %rbx,%rdi
call   1040 <fflush@plt>
test   %rax,%rax
je     1196 <optk_fwrite_at+0x46>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1050 <fseek@plt>
test   %rax,%rax
je     11ae <optk_fwrite_at+0x5e>
xor    %r14d,%r14d
jmp    11c4 <optk_fwrite_at+0x74>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fflush@plt>
mov    %rax,%rbp
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <fsetpos@plt>
mov    %rax,%rcx
xor    %eax,%eax
or     %rbp,%rcx
cmove  %r14,%rax
jmp    1189 <optk_fwrite_at+0x39>

LLVM IR (-O0):
define i64 @optk_fwrite_at(i32* %0, i64 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
store i32* %0, i32** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%14 = load i32*, i32** %7
%15 = call i64 @fgetpos(i32* %14, i32* %12)
%16 = icmp ne i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %5
%18 = load i32*, i32** %7
%19 = call i64 @fflush(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17, %5
store i64 0, i64* %6
br label %46
22: ; preds = %17
%23 = load i32*, i32** %7
%24 = load i64, i64* %8
%25 = load i32, i32* %9
%26 = call i64 @fseek(i32* %23, i64 %24, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i8*, i8** %10
%30 = load i64, i64* %11
%31 = load i32*, i32** %7
%32 = call i64 @fwrite(i8* %29, i32 1, i64 %30, i32* %31)
store i64 %32, i64* %13
br label %34
33: ; preds = %22
store i64 0, i64* %13
br label %34
34: ; preds = %33, %28
%35 = load i32*, i32** %7
%36 = call i64 @fflush(i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
store i64 0, i64* %13
br label %39
39: ; preds = %38, %34
%40 = load i32*, i32** %7
%41 = call i64 @fsetpos(i32* %40, i32* %12)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39
store i64 0, i64* %13
br label %44
44: ; preds = %43, %39
%45 = load i64, i64* %13
store i64 %45, i64* %6
br label %46
46: ; preds = %44, %21
%47 = load i64, i64* %6
ret i64 %47
}

LLVM IR (-O2):
define i64 @optk_fwrite_at(i32* nocapture %0, i64 %1, i32 %2, i8* nocapture %3, i64 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = call i64 @fgetpos(i32* %0, i32* %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %5
%11 = tail call i64 @fflush(i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = tail call i64 @fseek(i32* %0, i64 %1, i32 %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i64 @fwrite(i8* %3, i32 1, i64 %4, i32* %0)
br label %18
18: ; preds = %13, %16
%19 = phi i64 [ %17, %16 ], [ 0, %13 ]
%20 = tail call i64 @fflush(i32* %0)
%21 = call i64 @fsetpos(i32* %0, i32* %6)
%22 = or i64 %21, %20
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i64 %19, i64 0
br label %25
25: ; preds = %5, %10, %18
%26 = phi i64 [ %24, %18 ], [ 0, %10 ], [ 0, %5 ]
ret i64 %26
}