#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, ...);
int /*<<< orphan*/ run_test(char *, int, char **);
int test_sort(int argc, char **argv) {
  char *args[] = {"./enzyme", "ddcba", NULL};
  FILE *err;
  int rv, itmp1, itmp2, totalswap;
  run_test("", 2, args);
  err = fopen("smp2.out", "r");
  fscanf(err,
         "Creating threads...\nDone creating %d threads.\nJoining threads...\n",
         &itmp1);
  do {
    if (itmp1 != 4)
      exit(EXIT_FAILURE);
  } while (0);
  int found = 1, lines = 0;
  totalswap = 0;
  while (found < 5) {
    lines++;
    do {
      if (lines > 100)
        exit(EXIT_FAILURE);
    } while (0);
    rv = fscanf(err, "Thread %d exited normally: %d swaps.\n", &itmp1, &itmp2);
    if (rv == 2) {
      totalswap += itmp2;
      found++;
    }
    do {
      if (rv < 0 || feof(err))
        exit(EXIT_FAILURE);
    } while (0);
  }
  rv = fscanf(err,
              "Total: %d swaps\n"
              "Sorted string: abcdd\n",
              &itmp1);
  do {
    if (rv != 1 || itmp1 != totalswap)
      exit(EXIT_FAILURE);
  } while (0);
  do {
    if (!feof(err))
      exit(EXIT_FAILURE);
  } while (0);
  fclose(err);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<test_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c7a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c77(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c74(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rdx
lea    0xec0(%rip),%rdi
mov    $0x2,%esi
call   1060 <run_test@plt>
lea    0xe63(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe53(%rip),%rsi
lea    -0x40(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
cmpl   $0x4,-0x40(%rbp)
je     11ea <test_sort+0x8a>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11ef <test_sort+0x8f>
movl   $0x1,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x5,-0x4c(%rbp)
jge    12a7 <test_sort+0x147>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
cmpl   $0x64,-0x50(%rbp)
jle    122f <test_sort+0xcf>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1234 <test_sort+0xd4>
mov    -0x38(%rbp),%rdi
lea    0xe1d(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x44(%rbp),%rcx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x2,-0x3c(%rbp)
jne    126d <test_sort+0x10d>
mov    -0x44(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1272 <test_sort+0x112>
cmpl   $0x0,-0x3c(%rbp)
jl     128f <test_sort+0x12f>
mov    -0x38(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     129d <test_sort+0x13d>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    12a2 <test_sort+0x142>
jmp    1204 <test_sort+0xa4>
mov    -0x38(%rbp),%rdi
lea    0xdd0(%rip),%rsi
lea    -0x40(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x3c(%rbp)
jne    12d6 <test_sort+0x176>
mov    -0x40(%rbp),%eax
cmp    -0x48(%rbp),%eax
je     12e4 <test_sort+0x184>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    12e9 <test_sort+0x189>
jmp    12ee <test_sort+0x18e>
mov    -0x38(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
jne    130f <test_sort+0x1af>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1314 <test_sort+0x1b4>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2c8b(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2c6f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xece(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    $0x2,%esi
call   1060 <run_test@plt>
lea    0xe6c(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r13
lea    0xe61(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1040 <fscanf@plt>
cmpl   $0x4,0x8(%rsp)
jne    128d <test_sort+0x12d>
mov    $0x1,%ebx
mov    $0x65,%r14d
lea    0x8(%rsp),%r15
lea    0xc(%rsp),%r12
xor    %ebp,%ebp
jmp    1207 <test_sort+0xa7>
nopl   0x0(%rax)
mov    %al,%dl
add    %edx,%ebx
add    %ecx,%ebp
mov    %r13,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    128d <test_sort+0x12d>
cmp    $0x4,%ebx
ja     123f <test_sort+0xdf>
add    $0xffffffff,%r14d
je     128d <test_sort+0x12d>
mov    %r13,%rdi
lea    0xe40(%rip),%rsi
mov    %r15,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1040 <fscanf@plt>
test   %eax,%eax
js     128d <test_sort+0x12d>
xor    %ecx,%ecx
xor    %edx,%edx
cmp    $0x2,%eax
sete   %al
jne    11f0 <test_sort+0x90>
mov    0xc(%rsp),%ecx
jmp    11f0 <test_sort+0x90>
lea    0xe3c(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r13,%rdi
xor    %eax,%eax
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    128d <test_sort+0x12d>
cmp    %ebp,0x8(%rsp)
jne    128d <test_sort+0x12d>
mov    %r13,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     128d <test_sort+0x12d>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"./enzyme\00"
@.str.1 = [6 x i8] c"ddcba\00"
@__const.test_sort.args = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [9 x i8] c"smp2.out\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [66 x i8] c"Creating threads...\0ADone creating %d threads.\0AJoining threads...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [38 x i8] c"Thread %d exited normally: %d swaps.\0A\00"
@.str.7 = [38 x i8] c"Total: %d swaps\0ASorted string: abcdd\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @test_sort(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [3 x i8*]
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%13 = bitcast [3 x i8*]* %5 to i8*
%14 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%15 = call i32 @run_test(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 2, i8** %14)
%16 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %17, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32* %8)
br label %19
19: ; preds = %2
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 4
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %19
br label %26
26: ; preds = %25
store i32 1, i32* %11
store i32 0, i32* %12
store i32 0, i32* %10
br label %27
27: ; preds = %63, %26
%28 = load i32, i32* %11
%29 = icmp slt i32 %28, 5
br i1 %29, label %30, label %64
30: ; preds = %27
%31 = load i32, i32* %12
%32 = add nsw i32 %31, 1
store i32 %32, i32* %12
br label %33
33: ; preds = %30
%34 = load i32, i32* %12
%35 = icmp sgt i32 %34, 100
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %33
br label %40
40: ; preds = %39
%41 = load i32*, i32** %6
%42 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %41, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i32* %8, i32* %9)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, 2
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = load i32, i32* %9
%47 = load i32, i32* %10
%48 = add nsw i32 %47, %46
store i32 %48, i32* %10
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %51
51: ; preds = %45, %40
br label %52
52: ; preds = %51
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %52
%56 = load i32*, i32** %6
%57 = call i64 @feof(i32* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55, %52
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %55
br label %63
63: ; preds = %62
br label %27
64: ; preds = %27
%65 = load i32*, i32** %6
%66 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %65, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0), i32* %8)
store i32 %66, i32* %7
br label %67
67: ; preds = %64
%68 = load i32, i32* %7
%69 = icmp ne i32 %68, 1
br i1 %69, label %74, label %70
70: ; preds = %67
%71 = load i32, i32* %8
%72 = load i32, i32* %10
%73 = icmp ne i32 %71, %72
br i1 %73, label %74, label %77
74: ; preds = %70, %67
%75 = load i32, i32* @EXIT_FAILURE
%76 = call i32 @exit(i32 %75)
unreachable
77: ; preds = %70
br label %78
78: ; preds = %77
br label %79
79: ; preds = %78
%80 = load i32*, i32** %6
%81 = call i64 @feof(i32* %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %86, label %83
83: ; preds = %79
%84 = load i32, i32* @EXIT_FAILURE
%85 = call i32 @exit(i32 %84)
unreachable
86: ; preds = %79
br label %87
87: ; preds = %86
%88 = load i32*, i32** %6
%89 = call i32 @fclose(i32* %88)
%90 = load i32, i32* @EXIT_SUCCESS
ret i32 %90
}

LLVM IR (-O2):
@.str = [9 x i8] c"./enzyme\00"
@.str.1 = [6 x i8] c"ddcba\00"
@__const.test_sort.args = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [9 x i8] c"smp2.out\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [66 x i8] c"Creating threads...\0ADone creating %d threads.\0AJoining threads...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [38 x i8] c"Thread %d exited normally: %d swaps.\0A\00"
@.str.7 = [38 x i8] c"Total: %d swaps\0ASorted string: abcdd\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @test_sort(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [3 x i8*]
%4 = alloca i32
%5 = alloca i32
%6 = bitcast [3 x i8*]* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%10 = call i32 @run_test(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 2, i8** %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%12 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32* %4)
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 4
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2, %33
%19 = phi i32 [ %38, %33 ], [ 0, %2 ]
%20 = phi i32 [ %35, %33 ], [ 1, %2 ]
%21 = phi i32 [ %24, %33 ], [ 0, %2 ]
%22 = icmp ult i32 %20, 5
br i1 %22, label %23, label %44
23: ; preds = %18
%24 = add nuw nsw i32 %21, 1
%25 = icmp eq i32 %21, 100
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %23
%30 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i32* %4, i32* %5)
%31 = icmp eq i32 %30, 2
%32 = icmp slt i32 %30, 0
br i1 %32, label %41, label %33
33: ; preds = %29
%34 = zext i1 %31 to i32
%35 = add nuw nsw i32 %20, %34
%36 = load i32, i32* %5
%37 = select i1 %31, i32 %36, i32 0
%38 = add nsw i32 %37, %19
%39 = call i64 @feof(i32* %11)
%40 = icmp eq i64 %39, 0
br i1 %40, label %18, label %41
41: ; preds = %33, %29
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %18
%45 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0), i32* %4)
%46 = icmp eq i32 %45, 1
%47 = load i32, i32* %4
%48 = icmp eq i32 %47, %19
%49 = select i1 %46, i1 %48, i1 false
br i1 %49, label %53, label %50
50: ; preds = %44
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %44
%54 = call i64 @feof(i32* %11)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %53
%60 = call i32 @fclose(i32* %11)
%61 = load i32, i32* @EXIT_SUCCESS
ret i32 %61
}