#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 */
struct bbr_table {
  int dummy;
};
typedef int __u64;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int LONG_MAX;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int ferror(int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(struct bbr_table *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
int write_bbr_table(FILE *f, const char *device, struct bbr_table *table,
                    __u64 table_lsn, __u64 nr_sects) {
  int i;
  if (fseek(f, 0, SEEK_SET)) {
    printf("Failed to seek %s to beginning\n", device);
    return ferror(f);
  }
  while (table_lsn > (LONG_MAX >> 9)) {
    if (fseek(f, (LONG_MAX >> 9) << 9, SEEK_CUR)) {
      printf("Failed to seek %s for table\n", device);
      return ferror(f);
    }
    table_lsn -= (LONG_MAX >> 9);
  }
  if (fseek(f, table_lsn << 9, SEEK_CUR)) {
    printf("Failed to seek %s\n", device);
    return ferror(f);
  }
  for (i = 0; i < nr_sects; i++) {
    if (fwrite(table, 512, 1, f) != 1) {
      printf("Failed to write bbr table for %s\n", device);
      return ferror(f);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_bbr_table>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
cmp    $0x0,%rax
je     119a <write_bbr_table+0x5a>
mov    -0x18(%rbp),%rsi
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,-0x4(%rbp)
jmp    12cc <write_bbr_table+0x18c>
jmp    119f <write_bbr_table+0x5f>
mov    -0x24(%rbp),%eax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%ecx
sar    $0x9,%ecx
cmp    %ecx,%eax
jle    121b <write_bbr_table+0xdb>
mov    -0x10(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
sar    $0x9,%esi
shl    $0x9,%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
cmp    $0x0,%rax
je     1202 <write_bbr_table+0xc2>
mov    -0x18(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,-0x4(%rbp)
jmp    12cc <write_bbr_table+0x18c>
mov    0x2dcf(%rip),%rax
mov    (%rax),%ecx
sar    $0x9,%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    119f <write_bbr_table+0x5f>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
shl    $0x9,%esi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
cmp    $0x0,%rax
je     125e <write_bbr_table+0x11e>
mov    -0x18(%rbp),%rsi
lea    0xdf5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,-0x4(%rbp)
jmp    12cc <write_bbr_table+0x18c>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12c5 <write_bbr_table+0x185>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
je     12b2 <write_bbr_table+0x172>
mov    -0x18(%rbp),%rsi
lea    0xdb4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,-0x4(%rbp)
jmp    12cc <write_bbr_table+0x18c>
jmp    12b7 <write_bbr_table+0x177>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1265 <write_bbr_table+0x125>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bbr_table>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
test   %rax,%rax
je     117d <write_bbr_table+0x3d>
lea    0xe88(%rip),%r13
jmp    1245 <write_bbr_table+0x105>
mov    %r15,0x10(%rsp)
mov    %r14,0x8(%rsp)
mov    0x2e4a(%rip),%r14
mov    (%r14),%esi
mov    %esi,%eax
sar    $0x9,%eax
cmp    %ebx,%eax
jge    11d4 <write_bbr_table+0x94>
mov    0x2e47(%rip),%r15
lea    0xe78(%rip),%r13
nopl   0x0(%rax,%rax,1)
and    $0xfffffe00,%esi
mov    (%r15),%edx
mov    %r12,%rdi
call   1050 <fseek@plt>
test   %rax,%rax
jne    1240 <write_bbr_table+0x100>
mov    (%r14),%esi
mov    %esi,%eax
sar    $0x9,%eax
sub    %eax,%ebx
cmp    %eax,%ebx
jg     11b0 <write_bbr_table+0x70>
shl    $0x9,%ebx
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %ebx,%esi
call   1050 <fseek@plt>
test   %rax,%rax
mov    0x8(%rsp),%r14
mov    0x10(%rsp),%rbx
je     1202 <write_bbr_table+0xc2>
lea    0xe3d(%rip),%r13
jmp    1245 <write_bbr_table+0x105>
test   %ebp,%ebp
jle    122f <write_bbr_table+0xef>
lea    0xe43(%rip),%r13
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
jne    1245 <write_bbr_table+0x105>
add    $0xffffffff,%ebp
jne    1210 <write_bbr_table+0xd0>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%r14
mov    %r13,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r12,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ferror@plt>

LLVM IR (-O0):
%struct.bbr_table = type { i32 }
@SEEK_SET = global i32 0
@.str = [32 x i8] c"Failed to seek %s to beginning\0A\00"
@LONG_MAX = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [29 x i8] c"Failed to seek %s for table\0A\00"
@.str.2 = [19 x i8] c"Failed to seek %s\0A\00"
@.str.3 = [34 x i8] c"Failed to write bbr table for %s\0A\00"
define i32 @write_bbr_table(i32* %0, i8* %1, %struct.bbr_table* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca %struct.bbr_table*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %7
store i8* %1, i8** %8
store %struct.bbr_table* %2, %struct.bbr_table** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%13 = load i32*, i32** %7
%14 = load i32, i32* @SEEK_SET
%15 = call i64 @fseek(i32* %13, i32 0, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %5
%18 = load i8*, i8** %8
%19 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32*, i32** %7
%21 = call i32 @ferror(i32* %20)
store i32 %21, i32* %6
br label %78
22: ; preds = %5
br label %23
23: ; preds = %41, %22
%24 = load i32, i32* %10
%25 = load i32, i32* @LONG_MAX
%26 = ashr i32 %25, 9
%27 = icmp sgt i32 %24, %26
br i1 %27, label %28, label %46
28: ; preds = %23
%29 = load i32*, i32** %7
%30 = load i32, i32* @LONG_MAX
%31 = ashr i32 %30, 9
%32 = shl i32 %31, 9
%33 = load i32, i32* @SEEK_CUR
%34 = call i64 @fseek(i32* %29, i32 %32, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i8*, i8** %8
%38 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %7
%40 = call i32 @ferror(i32* %39)
store i32 %40, i32* %6
br label %78
41: ; preds = %28
%42 = load i32, i32* @LONG_MAX
%43 = ashr i32 %42, 9
%44 = load i32, i32* %10
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %10
br label %23
46: ; preds = %23
%47 = load i32*, i32** %7
%48 = load i32, i32* %10
%49 = shl i32 %48, 9
%50 = load i32, i32* @SEEK_CUR
%51 = call i64 @fseek(i32* %47, i32 %49, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %46
%54 = load i8*, i8** %8
%55 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = load i32*, i32** %7
%57 = call i32 @ferror(i32* %56)
store i32 %57, i32* %6
br label %78
58: ; preds = %46
store i32 0, i32* %12
br label %59
59: ; preds = %74, %58
%60 = load i32, i32* %12
%61 = load i32, i32* %11
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %77
63: ; preds = %59
%64 = load %struct.bbr_table*, %struct.bbr_table** %9
%65 = load i32*, i32** %7
%66 = call i32 @fwrite(%struct.bbr_table* %64, i32 512, i32 1, i32* %65)
%67 = icmp ne i32 %66, 1
br i1 %67, label %68, label %73
68: ; preds = %63
%69 = load i8*, i8** %8
%70 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %69)
%71 = load i32*, i32** %7
%72 = call i32 @ferror(i32* %71)
store i32 %72, i32* %6
br label %78
73: ; preds = %63
br label %74
74: ; preds = %73
%75 = load i32, i32* %12
%76 = add nsw i32 %75, 1
store i32 %76, i32* %12
br label %59
77: ; preds = %59
store i32 0, i32* %6
br label %78
78: ; preds = %77, %68, %53, %36, %17
%79 = load i32, i32* %6
ret i32 %79
}

LLVM IR (-O2):
%struct.bbr_table = type { i32 }
@SEEK_SET = global i32 0
@.str = [32 x i8] c"Failed to seek %s to beginning\0A\00"
@LONG_MAX = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [29 x i8] c"Failed to seek %s for table\0A\00"
@.str.2 = [19 x i8] c"Failed to seek %s\0A\00"
@.str.3 = [34 x i8] c"Failed to write bbr table for %s\0A\00"
define i32 @write_bbr_table(i32* nocapture %0, i8* %1, %struct.bbr_table* nocapture %2, i32 %3, i32 %4) {
%6 = load i32, i32* @SEEK_SET
%7 = tail call i64 @fseek(i32* %0, i32 0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %40
9: ; preds = %5
%10 = load i32, i32* @LONG_MAX
%11 = ashr i32 %10, 9
%12 = icmp slt i32 %11, %3
br i1 %12, label %13, label %25
13: ; preds = %9, %20
%14 = phi i32 [ %21, %20 ], [ %10, %9 ]
%15 = phi i32 [ %23, %20 ], [ %3, %9 ]
%16 = and i32 %14, -512
%17 = load i32, i32* @SEEK_CUR
%18 = tail call i64 @fseek(i32* %0, i32 %16, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %40
20: ; preds = %13
%21 = load i32, i32* @LONG_MAX
%22 = ashr i32 %21, 9
%23 = sub nsw i32 %15, %22
%24 = icmp sgt i32 %23, %22
br i1 %24, label %13, label %25
25: ; preds = %20, %9
%26 = phi i32 [ %3, %9 ], [ %23, %20 ]
%27 = shl i32 %26, 9
%28 = load i32, i32* @SEEK_CUR
%29 = tail call i64 @fseek(i32* %0, i32 %27, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %25
%32 = icmp sgt i32 %4, 0
br i1 %32, label %35, label %44
33: ; preds = %35
%34 = icmp eq i32 %39, %4
br i1 %34, label %44, label %35
35: ; preds = %31, %33
%36 = phi i32 [ %39, %33 ], [ 0, %31 ]
%37 = tail call i32 @fwrite(%struct.bbr_table* %2, i32 512, i32 1, i32* %0)
%38 = icmp eq i32 %37, 1
%39 = add nuw nsw i32 %36, 1
br i1 %38, label %33, label %40
40: ; preds = %13, %35, %25, %5
%41 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %25 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), %35 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %13 ]
%42 = tail call i32 @printf(i8* %41, i8* %1)
%43 = tail call i32 @ferror(i32* %0)
br label %44
44: ; preds = %33, %40, %31
%45 = phi i32 [ 0, %31 ], [ %43, %40 ], [ 0, %33 ]
ret i32 %45
}