#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*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void insert_length(FILE *f, long pos, int len) {
  long ptr;
  int i;
  char nbuf[64];
  fflush(f);
  fseek(f, 0L, 1);
  ptr = ftell(f);
  fseek(f, pos, 0);
  sprintf(nbuf, "%d", len);
  for (i = 0; i < 8 && nbuf[i] != '\0'; i++)
    fputc(nbuf[i], f);
  fseek(f, ptr, 0);
}


x86 assembly (clang, -O0):
<insert_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fflush@plt>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x1,%edx
call   1060 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1060 <fseek@plt>
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe55(%rip),%rsi
call   1070 <sprintf@plt>
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x8,-0x24(%rbp)
mov    %al,-0x71(%rbp)
jge    11d8 <insert_length+0x88>
movslq -0x24(%rbp),%rax
movsbl -0x70(%rbp,%rax,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
test   $0x1,%al
jne    11e8 <insert_length+0x98>
jmp    120a <insert_length+0xba>
movslq -0x24(%rbp),%rax
mov    -0x70(%rbp,%rax,1),%al
mov    -0x8(%rbp),%rsi
movsbl %al,%edi
call   1030 <fputc@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b7 <insert_length+0x67>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1060 <fseek@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_length>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1050 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1040 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   1060 <fseek@plt>
lea    0xe6a(%rip),%rsi
mov    %rsp,%rdi
mov    %r15d,%edx
call   1070 <sprintf@plt>
mov    (%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    0x1(%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    0x2(%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    0x3(%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    0x4(%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    0x5(%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    0x6(%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    0x7(%rsp),%al
test   %al,%al
je     123c <insert_length+0xec>
movsbl %al,%edi
mov    %rbx,%rsi
call   1030 <fputc@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fseek@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define void @insert_length(i32* %0, i64 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca [64 x i8]
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%10 = load i32*, i32** %4
%11 = call i32 @fflush(i32* %10)
%12 = load i32*, i32** %4
%13 = call i32 @fseek(i32* %12, i64 0, i32 1)
%14 = load i32*, i32** %4
%15 = call i64 @ftell(i32* %14)
store i64 %15, i64* %7
%16 = load i32*, i32** %4
%17 = load i64, i64* %5
%18 = call i32 @fseek(i32* %16, i64 %17, i32 0)
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%20 = load i32, i32* %6
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 0, i32* %8
br label %22
22: ; preds = %41, %3
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 8
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br label %32
32: ; preds = %25, %22
%33 = phi i1 [ false, %22 ], [ %31, %25 ]
br i1 %33, label %34, label %44
34: ; preds = %32
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = load i32*, i32** %4
%40 = call i32 @fputc(i8 signext %38, i32* %39)
br label %41
41: ; preds = %34
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %22
44: ; preds = %32
%45 = load i32*, i32** %4
%46 = load i64, i64* %7
%47 = call i32 @fseek(i32* %45, i64 %46, i32 0)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define void @insert_length(i32* nocapture %0, i64 %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @fflush(i32* %0)
%7 = tail call i32 @fseek(i32* %0, i64 0, i32 1)
%8 = tail call i64 @ftell(i32* %0)
%9 = tail call i32 @fseek(i32* %0, i64 %1, i32 0)
%10 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %2)
%11 = load i8, i8* %5
%12 = icmp eq i8 %11, 0
br i1 %12, label %50, label %13
13: ; preds = %3
%14 = tail call i32 @fputc(i8 signext %11, i32* %0)
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %50, label %18
18: ; preds = %13
%19 = tail call i32 @fputc(i8 signext %16, i32* %0)
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 2
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %50, label %23
23: ; preds = %18
%24 = tail call i32 @fputc(i8 signext %21, i32* %0)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 3
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %50, label %28
28: ; preds = %23
%29 = tail call i32 @fputc(i8 signext %26, i32* %0)
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 4
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %50, label %33
33: ; preds = %28
%34 = tail call i32 @fputc(i8 signext %31, i32* %0)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 5
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %50, label %38
38: ; preds = %33
%39 = tail call i32 @fputc(i8 signext %36, i32* %0)
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 6
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 0
br i1 %42, label %50, label %43
43: ; preds = %38
%44 = tail call i32 @fputc(i8 signext %41, i32* %0)
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 7
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %43
%49 = tail call i32 @fputc(i8 signext %46, i32* %0)
br label %50
50: ; preds = %48, %43, %38, %33, %28, %23, %18, %13, %3
%51 = tail call i32 @fseek(i32* %0, i64 %8, i32 0)
ret void
}