#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int main() {
  FILE *fptr = fopen("ts.bin", "wb");
  int ct = 131072;
  int i = 0;
  unsigned char key;
  while (ct--) {
    key = i;
    i = (i + 1) % 128;
    fwrite(&key, sizeof(unsigned char), 1, fptr);
  }
  fclose(fptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    0xeba(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x20000,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11a8 <main+0x78>
mov    -0x18(%rbp),%eax
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    $0x80,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1050 <fwrite@plt>
jmp    1164 <main+0x34>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xec5(%rip),%rdi
lea    0xec5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
xor    %edi,%edi
mov    $0xfffe0000,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rdi),%eax
lea    0x80(%rdi),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffffff80,%ecx
neg    %ecx
lea    (%rdi,%rcx,1),%ebx
add    $0x1,%ebx
mov    %r14,%rsi
call   1040 <fputc@plt>
mov    %ebx,%edi
inc    %ebp
jne    1160 <main+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"ts.bin\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
store i32 131072, i32* %3
store i32 0, i32* %4
br label %7
7: ; preds = %11, %0
%8 = load i32, i32* %3
%9 = add nsw i32 %8, -1
store i32 %9, i32* %3
%10 = icmp ne i32 %8, 0
br i1 %10, label %11, label %19
11: ; preds = %7
%12 = load i32, i32* %4
%13 = trunc i32 %12 to i8
store i8 %13, i8* %5
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
%16 = srem i32 %15, 128
store i32 %16, i32* %4
%17 = load i32*, i32** %2
%18 = call i32 @fwrite(i8* %5, i32 1, i32 1, i32* %17)
br label %7
19: ; preds = %7
%20 = load i32*, i32** %2
%21 = call i32 @fclose(i32* %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"ts.bin\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %2
2: ; preds = %0, %2
%3 = phi i32 [ 0, %0 ], [ %7, %2 ]
%4 = phi i32 [ 131072, %0 ], [ %5, %2 ]
%5 = add nsw i32 %4, -1
%6 = add nsw i32 %3, 1
%7 = srem i32 %6, 128
%8 = tail call i32 @fputc(i32 %3, i32* %1)
%9 = icmp eq i32 %5, 0
br i1 %9, label %10, label %2
10: ; preds = %2
%11 = tail call i32 @fclose(i32* %1)
ret i32 0
}