#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*/ SEEK_END;
int /*<<< orphan*/ errno;
int fputws(char *, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *strerror(int /*<<< orphan*/);
int do_seek_end(FILE *fp) {
  long save;
  if (fputws(L"abc\n", fp) == -1) {
    printf("do_seek_end: fputws: %s\n", strerror(errno));
    return 1;
  }
  save = ftell(fp);
  rewind(fp);
  if (fseek(fp, 0, SEEK_END) == -1) {
    printf("do_seek_end: fseek: %s\n", strerror(errno));
    return 1;
  }
  if (save != ftell(fp)) {
    printf("save = %ld, ftell = %ld\n", save, ftell(fp));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_seek_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe89(%rip),%rdi
call   1060 <fputws@plt>
cmp    $0xffffffff,%eax
jne    11b0 <do_seek_end+0x50>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <do_seek_end+0x108>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0xffffffff,%eax
jne    120e <do_seek_end+0xae>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <do_seek_end+0x108>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     1261 <do_seek_end+0x101>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <do_seek_end+0x108>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_seek_end>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe92(%rip),%rdi
mov    %rbx,%rsi
call   1060 <fputws@plt>
cmp    $0xffffffff,%eax
je     11d3 <do_seek_end+0x73>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <rewind@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0xffffffff,%eax
je     11ea <do_seek_end+0x8a>
mov    %rbx,%rdi
call   1050 <ftell@plt>
cmp    %rax,%r14
je     120e <do_seek_end+0xae>
mov    %rbx,%rdi
call   1050 <ftell@plt>
lea    0xe81(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1209 <do_seek_end+0xa9>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2c(%rip),%rdi
jmp    11ff <do_seek_end+0x9f>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i32] [i32 97, i32 98, i32 99, i32 10, i32 0]
@.str.1 = [25 x i8] c"do_seek_end: fputws: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"do_seek_end: fseek: %s\0A\00"
@.str.3 = [25 x i8] c"save = %ld, ftell = %ld\0A\00"
define i32 @do_seek_end(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i64
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = call i32 @fputws(i8* bitcast ([5 x i32]* @.str to i8*), i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %10)
store i32 1, i32* %2
br label %36
12: ; preds = %1
%13 = load i32*, i32** %3
%14 = call i64 @ftell(i32* %13)
store i64 %14, i64* %4
%15 = load i32*, i32** %3
%16 = call i32 @rewind(i32* %15)
%17 = load i32*, i32** %3
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %23)
store i32 1, i32* %2
br label %36
25: ; preds = %12
%26 = load i64, i64* %4
%27 = load i32*, i32** %3
%28 = call i64 @ftell(i32* %27)
%29 = icmp ne i64 %26, %28
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i64, i64* %4
%32 = load i32*, i32** %3
%33 = call i64 @ftell(i32* %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %31, i64 %33)
store i32 1, i32* %2
br label %36
35: ; preds = %25
store i32 0, i32* %2
br label %36
36: ; preds = %35, %30, %21, %8
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [5 x i32] [i32 97, i32 98, i32 99, i32 10, i32 0]
@.str.1 = [25 x i8] c"do_seek_end: fputws: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"do_seek_end: fseek: %s\0A\00"
@.str.3 = [25 x i8] c"save = %ld, ftell = %ld\0A\00"
define i32 @do_seek_end(i32* %0) {
%2 = tail call i32 @fputws(i8* bitcast ([5 x i32]* @.str to i8*), i32* %0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %24
8: ; preds = %1
%9 = tail call i64 @ftell(i32* %0)
%10 = tail call i32 @rewind(i32* %0)
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %0, i32 0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %24
18: ; preds = %8
%19 = tail call i64 @ftell(i32* %0)
%20 = icmp eq i64 %9, %19
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = tail call i64 @ftell(i32* %0)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %9, i64 %22)
br label %24
24: ; preds = %18, %21, %14, %4
%25 = phi i32 [ 1, %4 ], [ 1, %14 ], [ 1, %21 ], [ 0, %18 ]
ret i32 %25
}