#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ merge(int *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int array[1000];
  int sorted_array[1000];
  char line[100];
  int length;
  FILE *fp = fopen("numbers.txt", "r");
  if (fp == NULL) {
    printf("Bad file\n");
    return 1;
  }
  length = 1000;
  int i;
  for (i = 1; i <= 1000; i++) {
    fgets(line, 100, fp);
    array[i - 1] = atoi(line);
  }
  merge(array, 1, 1000);
  printf("Input array = ");
  for (i = 1; i <= 1000; i++) {
    printf("%d, ", array[i - 1]);
  }
  printf("\n");
  printf("Sorted array = ");
  for (i = 1; i <= 1000; i++) {
  }
  printf("\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1fe0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1fd0(%rbp)
cmpq   $0x0,-0x1fd0(%rbp)
jne    11b4 <main+0x54>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1306 <main+0x1a6>
movl   $0x3e8,-0x1fc4(%rbp)
movl   $0x1,-0x1fd4(%rbp)
cmpl   $0x3e8,-0x1fd4(%rbp)
jg     1224 <main+0xc4>
lea    -0x1fc0(%rbp),%rdi
mov    -0x1fd0(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
lea    -0x1fc0(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x1fd4(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %ecx,-0xfb0(%rbp,%rax,4)
mov    -0x1fd4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1fd4(%rbp)
jmp    11c8 <main+0x68>
lea    -0xfb0(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1060 <merge@plt>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x1fd4(%rbp)
cmpl   $0x3e8,-0x1fd4(%rbp)
jg     1296 <main+0x136>
mov    -0x1fd4(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0xfb0(%rbp,%rax,4),%esi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1fd4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1fd4(%rbp)
jmp    1252 <main+0xf2>
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x1fd4(%rbp)
cmpl   $0x3e8,-0x1fd4(%rbp)
jg     12e5 <main+0x185>
jmp    12d1 <main+0x171>
mov    -0x1fd4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1fd4(%rbp)
jmp    12bc <main+0x15c>
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1fd0(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1fe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1010,%rsp
lea    0xe6d(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1259 <main+0xd9>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x64,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
mov    %r15,%rdi
call   10a0 <atoi@plt>
mov    %eax,0x70(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x3e8,%rbx
jne    11b0 <main+0x30>
lea    0x70(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1080 <merge@plt>
lea    0xe1a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe1b(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x70(%rsp,%rbx,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    $0x3e8,%rbx
jne    1210 <main+0x90>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xde6(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    126a <main+0xea>
lea    0xdd2(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"numbers.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"Bad file\0A\00"
@.str.3 = [15 x i8] c"Input array = \00"
@.str.4 = [5 x i8] c"%d, \00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [16 x i8] c"Sorted array = \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i32]
%3 = alloca [1000 x i32]
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %61
13: ; preds = %0
store i32 1000, i32* %5
store i32 1, i32* %7
br label %14
14: ; preds = %27, %13
%15 = load i32, i32* %7
%16 = icmp sle i32 %15, 1000
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32 @fgets(i8* %18, i32 100, i32* %19)
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%22 = call i32 @atoi(i8* %21)
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [1000 x i32], [1000 x i32]* %2, i64 0, i64 %25
store i32 %22, i32* %26
br label %27
27: ; preds = %17
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %14
30: ; preds = %14
%31 = getelementptr inbounds [1000 x i32], [1000 x i32]* %2, i64 0, i64 0
%32 = call i32 @merge(i32* %31, i32 1, i32 1000)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %7
br label %34
34: ; preds = %44, %30
%35 = load i32, i32* %7
%36 = icmp sle i32 %35, 1000
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = load i32, i32* %7
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [1000 x i32], [1000 x i32]* %2, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %37
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %34
47: ; preds = %34
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %7
br label %50
50: ; preds = %54, %47
%51 = load i32, i32* %7
%52 = icmp sle i32 %51, 1000
br i1 %52, label %53, label %57
53: ; preds = %50
br label %54
54: ; preds = %53
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %50
57: ; preds = %50
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32*, i32** %6
%60 = call i32 @fclose(i32* %59)
store i32 0, i32* %1
br label %61
61: ; preds = %57, %11
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [12 x i8] c"numbers.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c"Input array = \00"
@.str.4 = [5 x i8] c"%d, \00"
@.str.6 = [16 x i8] c"Sorted array = \00"
@str = [9 x i8] c"Bad file\00"
define i32 @main() {
%1 = alloca [1000 x i32]
%2 = alloca [100 x i8]
%3 = bitcast [1000 x i32]* %1 to i8*
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %34
9: ; preds = %0, %9
%10 = phi i64 [ %15, %9 ], [ 1, %0 ]
%11 = call i32 @fgets(i8* %4, i32 100, i32* %5)
%12 = call i32 @atoi(i8* %4)
%13 = add nsw i64 %10, -1
%14 = getelementptr inbounds [1000 x i32], [1000 x i32]* %1, i64 0, i64 %13
store i32 %12, i32* %14
%15 = add nuw nsw i64 %10, 1
%16 = icmp eq i64 %15, 1001
br i1 %16, label %17, label %9
17: ; preds = %9
%18 = getelementptr inbounds [1000 x i32], [1000 x i32]* %1, i64 0, i64 0
%19 = call i32 @merge(i32* %18, i32 1, i32 1000)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %17, %21
%22 = phi i64 [ 1, %17 ], [ %27, %21 ]
%23 = add nsw i64 %22, -1
%24 = getelementptr inbounds [1000 x i32], [1000 x i32]* %1, i64 0, i64 %23
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = add nuw nsw i64 %22, 1
%28 = icmp eq i64 %27, 1001
br i1 %28, label %29, label %21
29: ; preds = %21
%30 = call i32 @putchar(i32 10)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 @putchar(i32 10)
%33 = call i32 @fclose(i32* %5)
br label %34
34: ; preds = %29, %7
%35 = phi i32 [ 1, %7 ], [ 0, %29 ]
ret i32 %35
}