#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ bubbleFork(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int len, fd, i, val;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
    exit(1);
  }
  fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    fprintf(stderr, "Error while opening %s", argv[1]);
    exit(-1);
  }
  read(fd, &len, sizeof(int));
  fprintf(stdout, "Not sorted: ");
  for (i = 0; i < len; i++) {
    read(fd, &val, sizeof(int));
    fprintf(stdout, "%3d ", val);
  }
  fputc('\n', stdout);
  bubbleFork(len, fd);
  lseek(fd, sizeof(int), SEEK_SET);
  fprintf(stdout, "Sorted: ");
  for (i = 0; i < len; i++) {
    read(fd, &val, sizeof(int));
    fprintf(stdout, "%3d ", val);
  }
  fputc('\n', stdout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x48>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1204 <main+0x94>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1278 <main+0x108>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1233 <main+0xc3>
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
mov    $0xa,%edi
call   1040 <fputc@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <bubbleFork@plt>
mov    -0x18(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1030 <lseek@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    130f <main+0x19f>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xd40(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12ca <main+0x15a>
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
mov    $0xa,%edi
call   1040 <fputc@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x1,%edi
jle    12f7 <main+0x187>
mov    0x8(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     131c <main+0x1ac>
mov    %eax,%r13d
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <read@plt>
mov    0x2e0b(%rip),%r12
mov    (%r12),%edi
lea    0xe65(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x4(%rsp),%ebx
test   %ebx,%ebx
jle    1298 <main+0x128>
mov    %rsp,%r14
lea    0xe55(%rip),%r15
mov    %ebx,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    (%r12),%edi
mov    (%rsp),%edx
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0xffffffff,%ebp
jne    11f0 <main+0x80>
mov    (%r12),%esi
mov    $0xa,%edi
call   1040 <fputc@plt>
mov    %ebx,%edi
mov    %r13d,%esi
call   1070 <bubbleFork@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    $0x4,%esi
call   1030 <lseek@plt>
mov    (%r12),%edi
lea    0xdf0(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %ebx,%ebx
jle    12d8 <main+0x168>
mov    %rsp,%r14
lea    0xdd6(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    (%r12),%edi
mov    (%rsp),%edx
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0xffffffff,%ebx
jne    1270 <main+0x100>
jmp    12d8 <main+0x168>
mov    (%r12),%esi
mov    $0xa,%edi
call   1040 <fputc@plt>
mov    %ebx,%edi
mov    %r13d,%esi
call   1070 <bubbleFork@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    $0x4,%esi
call   1030 <lseek@plt>
mov    (%r12),%edi
lea    0xd6e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r12),%esi
mov    $0xa,%edi
call   1040 <fputc@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xcf5(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xce6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Error while opening %s\00"
@stdout = global i32 0
@.str.2 = [13 x i8] c"Not sorted: \00"
@.str.3 = [5 x i8] c"%3d \00"
@SEEK_SET = global i32 0
@.str.4 = [9 x i8] c"Sorted: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %34
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %19
%35 = load i32, i32* %7
%36 = call i32 @read(i32 %35, i32* %6, i32 4)
%37 = load i32, i32* @stdout
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
br label %39
39: ; preds = %49, %34
%40 = load i32, i32* %8
%41 = load i32, i32* %6
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %52
43: ; preds = %39
%44 = load i32, i32* %7
%45 = call i32 @read(i32 %44, i32* %9, i32 4)
%46 = load i32, i32* @stdout
%47 = load i32, i32* %9
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %43
%50 = load i32, i32* %8
%51 = add nsw i32 %50, 1
store i32 %51, i32* %8
br label %39
52: ; preds = %39
%53 = load i32, i32* @stdout
%54 = call i32 @fputc(i8 signext 10, i32 %53)
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = call i32 @bubbleFork(i32 %55, i32 %56)
%58 = load i32, i32* %7
%59 = load i32, i32* @SEEK_SET
%60 = call i32 @lseek(i32 %58, i32 4, i32 %59)
%61 = load i32, i32* @stdout
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %8
br label %63
63: ; preds = %73, %52
%64 = load i32, i32* %8
%65 = load i32, i32* %6
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %76
67: ; preds = %63
%68 = load i32, i32* %7
%69 = call i32 @read(i32 %68, i32* %9, i32 4)
%70 = load i32, i32* @stdout
%71 = load i32, i32* %9
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %67
%74 = load i32, i32* %8
%75 = add nsw i32 %74, 1
store i32 %75, i32* %8
br label %63
76: ; preds = %63
%77 = load i32, i32* @stdout
%78 = call i32 @fputc(i8 signext 10, i32 %77)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Error while opening %s\00"
@stdout = global i32 0
@.str.2 = [13 x i8] c"Not sorted: \00"
@.str.3 = [5 x i8] c"%3d \00"
@SEEK_SET = global i32 0
@.str.4 = [9 x i8] c"Sorted: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %14
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %13
%25 = call i32 @read(i32 %17, i32* %3, i32 4)
%26 = load i32, i32* @stdout
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %3
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %54
30: ; preds = %24, %30
%31 = phi i32 [ %36, %30 ], [ 0, %24 ]
%32 = call i32 @read(i32 %17, i32* %4, i32 4)
%33 = load i32, i32* @stdout
%34 = load i32, i32* %4
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = add nuw nsw i32 %31, 1
%37 = icmp eq i32 %36, %28
br i1 %37, label %38, label %30
38: ; preds = %30
%39 = load i32, i32* @stdout
%40 = tail call i32 @fputc(i8 signext 10, i32 %39)
%41 = tail call i32 @bubbleFork(i32 %28, i32 %17)
%42 = load i32, i32* @SEEK_SET
%43 = tail call i32 @lseek(i32 %17, i32 4, i32 %42)
%44 = load i32, i32* @stdout
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br i1 %29, label %46, label %62
46: ; preds = %38, %46
%47 = phi i32 [ %52, %46 ], [ 0, %38 ]
%48 = call i32 @read(i32 %17, i32* %4, i32 4)
%49 = load i32, i32* @stdout
%50 = load i32, i32* %4
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = add nuw nsw i32 %47, 1
%53 = icmp eq i32 %52, %28
br i1 %53, label %62, label %46
54: ; preds = %24
%55 = load i32, i32* @stdout
%56 = tail call i32 @fputc(i8 signext 10, i32 %55)
%57 = tail call i32 @bubbleFork(i32 %28, i32 %17)
%58 = load i32, i32* @SEEK_SET
%59 = tail call i32 @lseek(i32 %17, i32 4, i32 %58)
%60 = load i32, i32* @stdout
%61 = tail call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %46, %54, %38
%63 = load i32, i32* @stdout
%64 = tail call i32 @fputc(i8 signext 10, i32 %63)
ret i32 0
}