#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 SEEK_CUR;
int fseek(int /*<<< orphan*/ *, long, int);
int fseek_u32(FILE *fp, unsigned offset, int where) {
  const unsigned stepMax = 1U << 30;
  int errorNb = 0;
  if (where != SEEK_CUR)
    return -1;
  while (offset > 0) {
    unsigned s = offset;
    if (s > stepMax)
      s = stepMax;
    errorNb = fseek(fp, (long)s, SEEK_CUR);
    if (errorNb != 0)
      break;
    offset -= s;
  }
  return errorNb;
}


x86 assembly (clang, -O0):
<fseek_u32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x40000000,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2eae(%rip),%rcx
cmp    (%rcx),%eax
je     114e <fseek_u32+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <fseek_u32+0xa6>
jmp    1153 <fseek_u32+0x43>
cmpl   $0x0,-0x14(%rbp)
jbe    11b0 <fseek_u32+0xa0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x40000000,-0x24(%rbp)
jbe    1177 <fseek_u32+0x67>
movl   $0x40000000,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%eax
mov    %eax,%esi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
call   1030 <fseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11a0 <fseek_u32+0x90>
jmp    11b0 <fseek_u32+0xa0>
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1153 <fseek_u32+0x43>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fseek_u32>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2ec9(%rip),%r12
mov    $0xffffffff,%eax
cmp    %edx,(%r12)
jne    115e <fseek_u32+0x4e>
mov    %esi,%ebx
mov    %rdi,%r14
nop
test   %ebx,%ebx
je     115c <fseek_u32+0x4c>
cmp    $0x40000000,%ebx
mov    $0x40000000,%r15d
cmovb  %ebx,%r15d
mov    (%r12),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <fseek@plt>
sub    %r15d,%ebx
test   %eax,%eax
je     1130 <fseek_u32+0x20>
jmp    115e <fseek_u32+0x4e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
define i32 @fseek_u32(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 1073741824, i32* %8
store i32 0, i32* %9
%11 = load i32, i32* %7
%12 = load i32, i32* @SEEK_CUR
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %39
15: ; preds = %3
br label %16
16: ; preds = %33, %15
%17 = load i32, i32* %6
%18 = icmp ugt i32 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %16
%20 = load i32, i32* %6
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp ugt i32 %21, 1073741824
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 1073741824, i32* %10
br label %24
24: ; preds = %23, %19
%25 = load i32*, i32** %5
%26 = load i32, i32* %10
%27 = zext i32 %26 to i64
%28 = load i32, i32* @SEEK_CUR
%29 = call i32 @fseek(i32* %25, i64 %27, i32 %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %24
br label %37
33: ; preds = %24
%34 = load i32, i32* %10
%35 = load i32, i32* %6
%36 = sub i32 %35, %34
store i32 %36, i32* %6
br label %16
37: ; preds = %32, %16
%38 = load i32, i32* %9
store i32 %38, i32* %4
br label %39
39: ; preds = %37, %14
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
define i32 @fseek_u32(i32* nocapture %0, i32 %1, i32 %2) {
%4 = load i32, i32* @SEEK_CUR
%5 = icmp eq i32 %4, %2
br i1 %5, label %6, label %17
6: ; preds = %3, %9
%7 = phi i32 [ %16, %9 ], [ %1, %3 ]
%8 = icmp eq i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %6
%10 = icmp ult i32 %7, 1073741824
%11 = select i1 %10, i32 %7, i32 1073741824
%12 = zext i32 %11 to i64
%13 = load i32, i32* @SEEK_CUR
%14 = tail call i32 @fseek(i32* %0, i64 %12, i32 %13)
%15 = icmp eq i32 %14, 0
%16 = sub i32 %7, %11
br i1 %15, label %6, label %17
17: ; preds = %9, %6, %3
%18 = phi i32 [ -1, %3 ], [ 0, %6 ], [ %14, %9 ]
ret i32 %18
}