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

/* Variables and functions */
int /*<<< orphan*/ LC_ALL;
int /*<<< orphan*/ close(int);
int create_temp_file(char *, char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fputws(int *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
int *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *setlocale(int /*<<< orphan*/, char *);
int /*<<< orphan*/ wmemset(int *, int, int);
int do_test(void) {
  FILE *fp = NULL;
  wchar_t *inputs[(2)] = {NULL};
  int ret = 1;
  if (setlocale(LC_ALL, "en_US.UTF-8") == NULL) {
    printf("Cannot set en_US.UTF-8 locale.\n");
    goto out;
  }
  wchar_t seed = L'ご';
  for (int i = 0; i < (2); i++) {
    inputs[i] = malloc((1400) * sizeof(wchar_t));
    if (inputs[i] == NULL) {
      printf("Failed to allocate memory for inputs: %m\n");
      goto out;
    }
    wmemset(inputs[i], seed, (1400) - 1);
    inputs[i][(1400) - 1] = L'\0';
  }
  char *filename;
  int fd = create_temp_file("tst-fseek-wide-partial.out", &filename);
  if (fd == -1) {
    printf("create_temp_file: %m\n");
    goto out;
  }
  fp = fdopen(fd, "w+");
  if (fp == NULL) {
    printf("fopen: %m\n");
    close(fd);
    goto out;
  }
  for (int i = 0; i < (2); i++) {
    printf("offset: %ld\n", ftell(fp));
    if (fputws(inputs[i], fp) == -1) {
      perror("fputws");
      goto out;
    }
  }
  ret = 0;
out:
  if (fp != NULL)
    fclose(fp);
  for (int i = 0; i < (2); i++)
    free(inputs[i]);
  return ret;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movq   $0x0,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
movl   $0x1,-0x24(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   10e0 <setlocale@plt>
cmp    $0x0,%rax
jne    1229 <do_test+0x59>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    138a <do_test+0x1ba>
movl   $0x3054,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x2,-0x2c(%rbp)
jge    12b1 <do_test+0xe1>
mov    $0x15e0,%edi
call   10c0 <malloc@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
mov    %rcx,-0x20(%rbp,%rax,8)
movslq -0x2c(%rbp),%rax
cmpq   $0x0,-0x20(%rbp,%rax,8)
jne    127a <do_test+0xaa>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    138a <do_test+0x1ba>
movslq -0x2c(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
mov    -0x28(%rbp),%esi
mov    $0x577,%edx
call   1090 <wmemset@plt>
movslq -0x2c(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rax
movl   $0x0,0x15dc(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1237 <do_test+0x67>
lea    0xd9e(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <create_temp_file@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12e1 <do_test+0x111>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    138a <do_test+0x1ba>
mov    -0x3c(%rbp),%edi
lea    0xd9c(%rip),%rsi
call   10d0 <fdopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    131a <do_test+0x14a>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
jmp    138a <do_test+0x1ba>
movl   $0x0,-0x40(%rbp)
cmpl   $0x2,-0x40(%rbp)
jge    1383 <do_test+0x1b3>
mov    -0x8(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,%esi
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movslq -0x40(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
mov    -0x8(%rbp),%rsi
call   10b0 <fputws@plt>
cmp    $0xffffffff,%eax
jne    1370 <do_test+0x1a0>
lea    0xd3c(%rip),%rdi
call   10f0 <perror@plt>
jmp    138a <do_test+0x1ba>
jmp    1375 <do_test+0x1a5>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1321 <do_test+0x151>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     139e <do_test+0x1ce>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x44(%rbp)
cmpl   $0x2,-0x44(%rbp)
jge    13cb <do_test+0x1fb>
movslq -0x44(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
call   1030 <free@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    13a5 <do_test+0x1d5>
mov    -0x24(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   10e0 <setlocale@plt>
test   %rax,%rax
je     1306 <do_test+0x136>
mov    $0x15e0,%edi
call   10c0 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12ee <do_test+0x11e>
mov    %rbx,%rdi
mov    $0x3054,%esi
mov    $0x577,%edx
call   1090 <wmemset@plt>
movl   $0x0,0x15dc(%rbx)
mov    $0x15e0,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     12ee <do_test+0x11e>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x3054,%esi
mov    $0x577,%edx
call   1090 <wmemset@plt>
movl   $0x0,0x15dc(%r14)
lea    0xdd5(%rip),%rdi
mov    %rsp,%rsi
call   1070 <create_temp_file@plt>
cmp    $0xffffffff,%eax
je     1339 <do_test+0x169>
mov    %eax,%ebp
lea    0xdec(%rip),%rsi
mov    %eax,%edi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     1342 <do_test+0x172>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <ftell@plt>
lea    0xdd8(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <fputws@plt>
cmp    $0xffffffff,%eax
je     135e <do_test+0x18e>
mov    %r15,%rdi
call   10a0 <ftell@plt>
lea    0xdaa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   10b0 <fputws@plt>
cmp    $0xffffffff,%eax
je     135e <do_test+0x18e>
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    131c <do_test+0x14c>
lea    0xd17(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    131c <do_test+0x14c>
lea    0xd7c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
xor    %ebx,%ebx
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd11(%rip),%rdi
jmp    12f8 <do_test+0x128>
lea    0xd21(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0x1,%ebp
jmp    131c <do_test+0x14c>
lea    0xd1d(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%ebp
jmp    12e4 <do_test+0x114>

LLVM IR (-O0):
@LC_ALL = global i32 0
@.str = [12 x i8] c"en_US.UTF-8\00"
@.str.1 = [32 x i8] c"Cannot set en_US.UTF-8 locale.\0A\00"
@.str.2 = [42 x i8] c"Failed to allocate memory for inputs: %m\0A\00"
@.str.3 = [27 x i8] c"tst-fseek-wide-partial.out\00"
@.str.4 = [22 x i8] c"create_temp_file: %m\0A\00"
@.str.5 = [3 x i8] c"w+\00"
@.str.6 = [11 x i8] c"fopen: %m\0A\00"
@.str.7 = [13 x i8] c"offset: %ld\0A\00"
@.str.8 = [7 x i8] c"fputws\00"
define i32 @do_test() {
%1 = alloca i32*
%2 = alloca [2 x i32*]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* null, i32** %1
%10 = bitcast [2 x i32*]* %2 to i8*
store i32 1, i32* %3
%11 = load i32, i32* @LC_ALL
%12 = call i32* @setlocale(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %84
16: ; preds = %0
store i32 12372, i32* %4
store i32 0, i32* %5
br label %17
17: ; preds = %44, %16
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 2
br i1 %19, label %20, label %47
20: ; preds = %17
%21 = call i32* @malloc(i32 5600)
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [2 x i32*], [2 x i32*]* %2, i64 0, i64 %23
store i32* %21, i32** %24
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [2 x i32*], [2 x i32*]* %2, i64 0, i64 %26
%28 = load i32*, i32** %27
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %84
32: ; preds = %20
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [2 x i32*], [2 x i32*]* %2, i64 0, i64 %34
%36 = load i32*, i32** %35
%37 = load i32, i32* %4
%38 = call i32 @wmemset(i32* %36, i32 %37, i32 1399)
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [2 x i32*], [2 x i32*]* %2, i64 0, i64 %40
%42 = load i32*, i32** %41
%43 = getelementptr inbounds i32, i32* %42, i64 1399
store i32 0, i32* %43
br label %44
44: ; preds = %32
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %17
47: ; preds = %17
%48 = call i32 @create_temp_file(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8** %6)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %84
53: ; preds = %47
%54 = load i32, i32* %7
%55 = call i32* @fdopen(i32 %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %55, i32** %1
%56 = load i32*, i32** %1
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%60 = load i32, i32* %7
%61 = call i32 @close(i32 %60)
br label %84
62: ; preds = %53
store i32 0, i32* %8
br label %63
63: ; preds = %80, %62
%64 = load i32, i32* %8
%65 = icmp slt i32 %64, 2
br i1 %65, label %66, label %83
66: ; preds = %63
%67 = load i32*, i32** %1
%68 = call i32 @ftell(i32* %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %68)
%70 = load i32, i32* %8
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [2 x i32*], [2 x i32*]* %2, i64 0, i64 %71
%73 = load i32*, i32** %72
%74 = load i32*, i32** %1
%75 = call i32 @fputws(i32* %73, i32* %74)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %66
%78 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %84
79: ; preds = %66
br label %80
80: ; preds = %79
%81 = load i32, i32* %8
%82 = add nsw i32 %81, 1
store i32 %82, i32* %8
br label %63
83: ; preds = %63
store i32 0, i32* %3
br label %84
84: ; preds = %83, %77, %58, %51, %30, %14
%85 = load i32*, i32** %1
%86 = icmp ne i32* %85, null
br i1 %86, label %87, label %90
87: ; preds = %84
%88 = load i32*, i32** %1
%89 = call i32 @fclose(i32* %88)
br label %90
90: ; preds = %87, %84
store i32 0, i32* %9
br label %91
91: ; preds = %100, %90
%92 = load i32, i32* %9
%93 = icmp slt i32 %92, 2
br i1 %93, label %94, label %103
94: ; preds = %91
%95 = load i32, i32* %9
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [2 x i32*], [2 x i32*]* %2, i64 0, i64 %96
%98 = load i32*, i32** %97
%99 = call i32 @free(i32* %98)
br label %100
100: ; preds = %94
%101 = load i32, i32* %9
%102 = add nsw i32 %101, 1
store i32 %102, i32* %9
br label %91
103: ; preds = %91
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
@LC_ALL = global i32 0
@.str = [12 x i8] c"en_US.UTF-8\00"
@.str.2 = [42 x i8] c"Failed to allocate memory for inputs: %m\0A\00"
@.str.3 = [27 x i8] c"tst-fseek-wide-partial.out\00"
@.str.4 = [22 x i8] c"create_temp_file: %m\0A\00"
@.str.5 = [3 x i8] c"w+\00"
@.str.6 = [11 x i8] c"fopen: %m\0A\00"
@.str.7 = [13 x i8] c"offset: %ld\0A\00"
@.str.8 = [7 x i8] c"fputws\00"
@str = [31 x i8] c"Cannot set en_US.UTF-8 locale.\00"
define i32 @do_test() {
%1 = alloca i8*
%2 = load i32, i32* @LC_ALL
%3 = tail call i32* @setlocale(i32 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = tail call i32* @malloc(i32 5600)
%7 = icmp eq i32* %6, null
br i1 %7, label %20, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %45
10: ; preds = %5
%11 = tail call i32 @wmemset(i32* %6, i32 12372, i32 1399)
%12 = getelementptr inbounds i32, i32* %6, i64 1399
store i32 0, i32* %12
%13 = tail call i32* @malloc(i32 5600)
%14 = icmp eq i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %10
%16 = tail call i32 @wmemset(i32* %13, i32 12372, i32 1399)
%17 = getelementptr inbounds i32, i32* %13, i64 1399
store i32 0, i32* %17
%18 = call i32 @create_temp_file(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8** %1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %22, label %24
20: ; preds = %10, %5
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %45
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %45
24: ; preds = %15
%25 = call i32* @fdopen(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %32, label %27
27: ; preds = %24
%28 = call i32 @ftell(i32* %25)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %28)
%30 = call i32 @fputws(i32* %6, i32* %25)
%31 = icmp eq i32 %30, -1
br i1 %31, label %40, label %35
32: ; preds = %24
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%34 = call i32 @close(i32 %18)
br label %45
35: ; preds = %27
%36 = call i32 @ftell(i32* %25)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %36)
%38 = call i32 @fputws(i32* %13, i32* %25)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35, %27
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %42
42: ; preds = %35, %40
%43 = phi i32 [ 1, %40 ], [ 0, %35 ]
%44 = call i32 @fclose(i32* %25)
br label %45
45: ; preds = %32, %22, %20, %8, %42
%46 = phi i32* [ null, %8 ], [ %6, %20 ], [ %6, %22 ], [ %6, %32 ], [ %6, %42 ]
%47 = phi i32* [ null, %8 ], [ null, %20 ], [ %13, %22 ], [ %13, %32 ], [ %13, %42 ]
%48 = phi i32 [ 1, %8 ], [ 1, %20 ], [ 1, %22 ], [ 1, %32 ], [ %43, %42 ]
%49 = call i32 @free(i32* %46)
%50 = call i32 @free(i32* %47)
ret i32 %48
}