#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
void bubbleFork(int len, int fd) {
  int val1, val2, i, j, stat;
  pid_t main_pid, pid;
  for (i = 0; i < len - 1; i++) {
    main_pid = fork();
    if (main_pid) {
      waitpid(main_pid, &stat, 0);
      exit(0);
    } else {
      for (j = 0; j < len - 1 - i; j++) {
        pid = fork();
        if (pid) {
          waitpid(pid, &stat, 0);
          exit(0);
        } else {
          lseek(fd, (1 + j) * sizeof(int), SEEK_SET);
          read(fd, &val1, sizeof(int));
          read(fd, &val2, sizeof(int));
          if (val1 > val2) {
            lseek(fd, (1 + j) * sizeof(int), SEEK_SET);
            write(fd, &val2, sizeof(int));
            write(fd, &val1, sizeof(int));
          }
        }
      }
    }
  }
  return;
}


x86 assembly (clang, -O0):
<bubbleFork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12b7 <bubbleFork+0x157>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b2 <bubbleFork+0x52>
mov    -0x28(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jge    129f <bubbleFork+0x13f>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f9 <bubbleFork+0x99>
mov    -0x30(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    1287 <bubbleFork+0x127>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    128c <bubbleFork+0x12c>
jmp    1291 <bubbleFork+0x131>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b9 <bubbleFork+0x59>
jmp    12a4 <bubbleFork+0x144>
jmp    12a9 <bubbleFork+0x149>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1175 <bubbleFork+0x15>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubbleFork>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     126b <bubbleFork+0x10b>
mov    %esi,%ebx
mov    %edi,%r13d
add    $0xffffffff,%r13d
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%r12
mov    %r13d,%r14d
xor    %ebp,%ebp
mov    %r13d,0x4(%rsp)
jmp    11b9 <bubbleFork+0x59>
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%ebp
add    $0x1,%ebp
add    $0xffffffff,%r14d
mov    0x4(%rsp),%r13d
cmp    %r13d,%ebp
je     126b <bubbleFork+0x10b>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    127a <bubbleFork+0x11a>
mov    %ebp,0x8(%rsp)
cmp    %ebp,%r13d
jle    11a0 <bubbleFork+0x40>
xor    %r13d,%r13d
mov    $0x4,%ebp
jmp    11e8 <bubbleFork+0x88>
nopl   0x0(%rax)
add    $0x4,%ebp
cmp    %r13d,%r14d
je     11a0 <bubbleFork+0x40>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    127a <bubbleFork+0x11a>
add    $0x1,%r13d
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    0x14(%rsp),%eax
cmp    0x10(%rsp),%eax
jle    11e0 <bubbleFork+0x80>
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    11e0 <bubbleFork+0x80>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
define void @bubbleFork(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
br label %12
12: ; preds = %77, %2
%13 = load i32, i32* %7
%14 = load i32, i32* %3
%15 = sub nsw i32 %14, 1
%16 = icmp slt i32 %13, %15
br i1 %16, label %17, label %80
17: ; preds = %12
%18 = call i64 (...) @fork()
store i64 %18, i64* %10
%19 = load i64, i64* %10
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i64, i64* %10
%23 = call i32 @waitpid(i64 %22, i32* %9, i32 0)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %17
store i32 0, i32* %8
br label %26
26: ; preds = %72, %25
%27 = load i32, i32* %8
%28 = load i32, i32* %3
%29 = sub nsw i32 %28, 1
%30 = load i32, i32* %7
%31 = sub nsw i32 %29, %30
%32 = icmp slt i32 %27, %31
br i1 %32, label %33, label %75
33: ; preds = %26
%34 = call i64 (...) @fork()
store i64 %34, i64* %11
%35 = load i64, i64* %11
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i64, i64* %11
%39 = call i32 @waitpid(i64 %38, i32* %9, i32 0)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %33
%42 = load i32, i32* %4
%43 = load i32, i32* %8
%44 = add nsw i32 1, %43
%45 = sext i32 %44 to i64
%46 = mul i64 %45, 4
%47 = trunc i64 %46 to i32
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @lseek(i32 %42, i32 %47, i32 %48)
%50 = load i32, i32* %4
%51 = call i32 @read(i32 %50, i32* %5, i32 4)
%52 = load i32, i32* %4
%53 = call i32 @read(i32 %52, i32* %6, i32 4)
%54 = load i32, i32* %5
%55 = load i32, i32* %6
%56 = icmp sgt i32 %54, %55
br i1 %56, label %57, label %70
57: ; preds = %41
%58 = load i32, i32* %4
%59 = load i32, i32* %8
%60 = add nsw i32 1, %59
%61 = sext i32 %60 to i64
%62 = mul i64 %61, 4
%63 = trunc i64 %62 to i32
%64 = load i32, i32* @SEEK_SET
%65 = call i32 @lseek(i32 %58, i32 %63, i32 %64)
%66 = load i32, i32* %4
%67 = call i32 @write(i32 %66, i32* %6, i32 4)
%68 = load i32, i32* %4
%69 = call i32 @write(i32 %68, i32* %5, i32 4)
br label %70
70: ; preds = %57, %41
br label %71
71: ; preds = %70
br label %72
72: ; preds = %71
%73 = load i32, i32* %8
%74 = add nsw i32 %73, 1
store i32 %74, i32* %8
br label %26
75: ; preds = %26
br label %76
76: ; preds = %75
br label %77
77: ; preds = %76
%78 = load i32, i32* %7
%79 = add nsw i32 %78, 1
store i32 %79, i32* %7
br label %12
80: ; preds = %12
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define void @bubbleFork(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = add i32 %0, -1
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %49
11: ; preds = %2, %45
%12 = phi i32 [ %47, %45 ], [ %9, %2 ]
%13 = phi i32 [ %46, %45 ], [ 0, %2 ]
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = icmp sgt i32 %9, %13
br i1 %17, label %21, label %45
18: ; preds = %11
%19 = call i32 @waitpid(i64 %14, i32* %5, i32 0)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %16, %43
%22 = phi i32 [ %29, %43 ], [ 0, %16 ]
%23 = tail call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = call i32 @waitpid(i64 %23, i32* %5, i32 0)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %21
%29 = add nuw nsw i32 %22, 1
%30 = shl i32 %29, 2
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @lseek(i32 %1, i32 %30, i32 %31)
%33 = call i32 @read(i32 %1, i32* %3, i32 4)
%34 = call i32 @read(i32 %1, i32* %4, i32 4)
%35 = load i32, i32* %3
%36 = load i32, i32* %4
%37 = icmp sgt i32 %35, %36
br i1 %37, label %38, label %43
38: ; preds = %28
%39 = load i32, i32* @SEEK_SET
%40 = tail call i32 @lseek(i32 %1, i32 %30, i32 %39)
%41 = call i32 @write(i32 %1, i32* %4, i32 4)
%42 = call i32 @write(i32 %1, i32* %3, i32 4)
br label %43
43: ; preds = %38, %28
%44 = icmp eq i32 %29, %12
br i1 %44, label %45, label %21
45: ; preds = %43, %16
%46 = add nuw nsw i32 %13, 1
%47 = add i32 %12, -1
%48 = icmp eq i32 %46, %9
br i1 %48, label %49, label %11
49: ; preds = %45, %2
ret void
}