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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
int main(void) {
  srand((unsigned int)time(NULL));
  int i;
  FILE *p = fopen("D:\\temp\\a.txt", "w");
  for (i = 0; i < 100000; i++) {
    fprintf(p, "%d\n", (int)rand() % 513);
  }
  fclose(p);
  p = fopen("D:\\temp\\a.txt", "r");
  int array[513] = {0};
  while (!feof(p)) {
    char buf[100] = {0};
    fgets(buf, sizeof(buf), p);
    if (buf[0] != 0) {
      int value = atoi(buf);
      array[value]++;
    }
  }
  fclose(p);
  p = fopen("D:\\temp\\b.txt", "w");
  int j;
  for (i = 0; i < 513; i++) {
    for (j = 0; j < array[i]; j++) {
      fprintf(p, "%d\n", i);
    }
  }
  fclose(p);
  printf("end\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8a0,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
lea    0xe27(%rip),%rdi
lea    0xe2e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x186a0,-0x8(%rbp)
jge    1238 <main+0x88>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8a0(%rbp)
mov    $0x0,%al
call   10d0 <rand@plt>
mov    -0x8a0(%rbp),%rdi
mov    $0x201,%ecx
cltd
idiv   %ecx
lea    0xdeb(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11f0 <main+0x40>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xdb8(%rip),%rdi
lea    0xdc5(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x804,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1289 <main+0xd9>
jmp    12f0 <main+0x140>
lea    -0x890(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
lea    -0x890(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
movsbl -0x890(%rbp),%eax
cmp    $0x0,%eax
je     12eb <main+0x13b>
lea    -0x890(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x894(%rbp)
movslq -0x894(%rbp),%rax
mov    -0x820(%rbp,%rax,4),%ecx
add    $0x1,%ecx
mov    %ecx,-0x820(%rbp,%rax,4)
jmp    126b <main+0xbb>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xd16(%rip),%rdi
lea    0xd07(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x201,-0x8(%rbp)
jge    137f <main+0x1cf>
movl   $0x0,-0x898(%rbp)
mov    -0x898(%rbp),%eax
movslq -0x8(%rbp),%rcx
cmp    -0x820(%rbp,%rcx,4),%eax
jge    136c <main+0x1bc>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xcbd(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x898(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x898(%rbp)
jmp    132e <main+0x17e>
jmp    1371 <main+0x1c1>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1317 <main+0x167>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xc95(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x878,%rsp
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
lea    0xe2e(%rip),%rdi
lea    0xe35(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    $0x186a0,%ebp
lea    0xe23(%rip),%r14
nopl   (%rax)
xor    %eax,%eax
call   10d0 <rand@plt>
movslq %eax,%rdx
imul   $0x7fc01ff1,%rdx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x28,%rax
add    %ecx,%eax
mov    %eax,%ecx
shl    $0x9,%ecx
add    %eax,%ecx
sub    %ecx,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <fprintf@plt>
add    $0xffffffff,%ebp
jne    11f0 <main+0x40>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xdca(%rip),%rdi
lea    0xdd7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
lea    0x70(%rsp),%rdi
mov    $0x804,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     12e8 <main+0x138>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xda1(%rip),%rdi
lea    0xd92(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
xor    %ebx,%ebx
lea    0xd83(%rip),%r15
jmp    129d <main+0xed>
nop
add    $0x1,%rbx
cmp    $0x201,%rbx
je     12c4 <main+0x114>
mov    0x70(%rsp,%rbx,4),%ebp
test   %ebp,%ebp
jle    1290 <main+0xe0>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1080 <fprintf@plt>
add    $0xffffffff,%ebp
jne    12b0 <main+0x100>
jmp    1290 <main+0xe0>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xd51(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x878,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%r14
jmp    1300 <main+0x150>
nopl   (%rax)
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    1266 <main+0xb6>
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
cmpb   $0x0,(%rsp)
je     12f0 <main+0x140>
mov    %r14,%rdi
call   10c0 <atoi@plt>
cltq
addl   $0x1,0x70(%rsp,%rax,4)
jmp    12f0 <main+0x140>

LLVM IR (-O0):
@.str = [14 x i8] c"D:\\temp\\a.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [14 x i8] c"D:\\temp\\b.txt\00"
@.str.5 = [5 x i8] c"end\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [513 x i32]
%5 = alloca [100 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i64 @time(i32* null)
%9 = trunc i64 %8 to i32
%10 = call i32 @srand(i32 %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %3
store i32 0, i32* %2
br label %12
12: ; preds = %21, %0
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 100000
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i32*, i32** %3
%17 = call i64 (...) @rand()
%18 = trunc i64 %17 to i32
%19 = srem i32 %18, 513
%20 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %15
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %12
24: ; preds = %12
%25 = load i32*, i32** %3
%26 = call i32 @fclose(i32* %25)
%27 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %27, i32** %3
%28 = bitcast [513 x i32]* %4 to i8*
br label %29
29: ; preds = %51, %24
%30 = load i32*, i32** %3
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %34, label %52
34: ; preds = %29
%35 = bitcast [100 x i8]* %5 to i8*
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%37 = load i32*, i32** %3
%38 = call i32 @fgets(i8* %36, i32 100, i32* %37)
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %34
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%45 = call i32 @atoi(i8* %44)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [513 x i32], [513 x i32]* %4, i64 0, i64 %47
%49 = load i32, i32* %48
%50 = add nsw i32 %49, 1
store i32 %50, i32* %48
br label %51
51: ; preds = %43, %34
br label %29
52: ; preds = %29
%53 = load i32*, i32** %3
%54 = call i32 @fclose(i32* %53)
%55 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %55, i32** %3
store i32 0, i32* %2
br label %56
56: ; preds = %75, %52
%57 = load i32, i32* %2
%58 = icmp slt i32 %57, 513
br i1 %58, label %59, label %78
59: ; preds = %56
store i32 0, i32* %7
br label %60
60: ; preds = %71, %59
%61 = load i32, i32* %7
%62 = load i32, i32* %2
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [513 x i32], [513 x i32]* %4, i64 0, i64 %63
%65 = load i32, i32* %64
%66 = icmp slt i32 %61, %65
br i1 %66, label %67, label %74
67: ; preds = %60
%68 = load i32*, i32** %3
%69 = load i32, i32* %2
%70 = call i32 @fprintf(i32* %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %67
%72 = load i32, i32* %7
%73 = add nsw i32 %72, 1
store i32 %73, i32* %7
br label %60
74: ; preds = %60
br label %75
75: ; preds = %74
%76 = load i32, i32* %2
%77 = add nsw i32 %76, 1
store i32 %77, i32* %2
br label %56
78: ; preds = %56
%79 = load i32*, i32** %3
%80 = call i32 @fclose(i32* %79)
%81 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"D:\\temp\\a.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [14 x i8] c"D:\\temp\\b.txt\00"
@str = [4 x i8] c"end\00"
define i32 @main() {
%1 = alloca [513 x i32]
%2 = alloca [100 x i8]
%3 = tail call i64 @time(i32* null)
%4 = trunc i64 %3 to i32
%5 = tail call i32 @srand(i32 %4)
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %0, %7
%8 = phi i32 [ 0, %0 ], [ %13, %7 ]
%9 = tail call i64 (...) @rand()
%10 = trunc i64 %9 to i32
%11 = srem i32 %10, 513
%12 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = add nuw nsw i32 %8, 1
%14 = icmp eq i32 %13, 100000
br i1 %14, label %15, label %7
15: ; preds = %7
%16 = tail call i32 @fclose(i32* %6)
%17 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = bitcast [513 x i32]* %1 to i8*
%19 = call i32 @feof(i32* %17)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %36
21: ; preds = %15
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
br label %23
23: ; preds = %21, %33
%24 = call i32 @fgets(i8* %22, i32 100, i32* %17)
%25 = load i8, i8* %22
%26 = icmp eq i8 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %23
%28 = call i32 @atoi(i8* %22)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [513 x i32], [513 x i32]* %1, i64 0, i64 %29
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
store i32 %32, i32* %30
br label %33
33: ; preds = %27, %23
%34 = call i32 @feof(i32* %17)
%35 = icmp eq i32 %34, 0
br i1 %35, label %23, label %36
36: ; preds = %33, %15
%37 = call i32 @fclose(i32* %17)
%38 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %36, %51
%40 = phi i64 [ 0, %36 ], [ %52, %51 ]
%41 = getelementptr inbounds [513 x i32], [513 x i32]* %1, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = trunc i64 %40 to i32
br label %46
46: ; preds = %44, %46
%47 = phi i32 [ %49, %46 ], [ 0, %44 ]
%48 = call i32 @fprintf(i32* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%49 = add nuw nsw i32 %47, 1
%50 = icmp eq i32 %49, %42
br i1 %50, label %51, label %46
51: ; preds = %46, %39
%52 = add nuw nsw i64 %40, 1
%53 = icmp eq i64 %52, 513
br i1 %53, label %54, label %39
54: ; preds = %51
%55 = call i32 @fclose(i32* %38)
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
ret i32 0
}