#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int stat(char *, struct stat *);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ system(char *);
int sort_regions(char *workdir, int total) {
  struct dirent *e;
  DIR *d;
  char cmd[200];
  chdir(workdir);
  struct stat s;
  int err = stat("sorted", &s);
  if (err == -1) {
    if (errno == ENOENT) {
    }
  } else {
    if (S_ISDIR(s.st_mode)) {
      printf(" - regions already sorted \n");
      return 0;
    }
  }
  d = opendir(".");
  printf("Sorting regions ... \n");
  system("mkdir sorted");
  if (d) {
    while ((e = readdir(d)) != NULL) {
      if (strstr(e->d_name, ".chr")) {
        printf(" - %s\n", e->d_name);
        sprintf(cmd, "sort -g %s > sorted/%s_sorted", e->d_name, e->d_name);
        system(cmd);
      }
    }
    closedir(d);
  }
  return 0;
}


x86 assembly (clang, -O0):
<sort_regions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
lea    0xe78(%rip),%rdi
lea    -0xf8(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0xfc(%rbp)
cmpl   $0xffffffff,-0xfc(%rbp)
jne    1205 <sort_regions+0x65>
mov    0x2df8(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    1200 <sort_regions+0x60>
jmp    1200 <sort_regions+0x60>
jmp    1239 <sort_regions+0x99>
mov    -0xf8(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1234 <sort_regions+0x94>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fe <sort_regions+0x15e>
jmp    1239 <sort_regions+0x99>
lea    0xddc(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdd6(%rip),%rdi
call   1050 <system@plt>
cmpq   $0x0,-0x28(%rbp)
je     12f7 <sort_regions+0x157>
jmp    1273 <sort_regions+0xd3>
mov    -0x28(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12ee <sort_regions+0x14e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xda9(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     12e9 <sort_regions+0x149>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0xf0(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xd75(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0xf0(%rbp),%rdi
call   1050 <system@plt>
jmp    1273 <sort_regions+0xd3>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_regions>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
call   1050 <chdir@plt>
lea    0xe3b(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10a0 <stat@plt>
cmp    $0xffffffff,%eax
je     11fb <sort_regions+0x4b>
mov    0x8(%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %rax,%rax
je     11fb <sort_regions+0x4b>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
jmp    12b3 <sort_regions+0x103>
lea    0xdfe(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
lea    0xde5(%rip),%rdi
call   1060 <system@plt>
test   %rbx,%rbx
je     12b3 <sort_regions+0x103>
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12ab <sort_regions+0xfb>
mov    %rax,%rbp
lea    0xdcd(%rip),%r14
lea    0xdcb(%rip),%r15
lea    0xdcb(%rip),%r12
lea    0x10(%rsp),%r13
jmp    1270 <sort_regions+0xc0>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12ab <sort_regions+0xfb>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
je     1260 <sort_regions+0xb0>
mov    0x0(%rbp),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x0(%rbp),%rcx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rcx,%rdx
call   10c0 <sprintf@plt>
mov    %r13,%rdi
call   1060 <system@plt>
jmp    1260 <sort_regions+0xb0>
mov    %rbx,%rdi
call   1090 <closedir@plt>
xor    %eax,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [7 x i8] c"sorted\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [28 x i8] c" - regions already sorted \0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [22 x i8] c"Sorting regions ... \0A\00"
@.str.4 = [13 x i8] c"mkdir sorted\00"
@.str.5 = [5 x i8] c".chr\00"
@.str.6 = [7 x i8] c" - %s\0A\00"
@.str.7 = [30 x i8] c"sort -g %s > sorted/%s_sorted\00"
define i32 @sort_regions(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca [200 x i8]
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = call i32 @chdir(i8* %11)
%13 = call i32 @stat(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %struct.stat* %9)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
br label %21
21: ; preds = %20, %16
br label %30
22: ; preds = %2
%23 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @S_ISDIR(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %67
29: ; preds = %22
br label %30
30: ; preds = %29, %21
%31 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32*, i32** %7
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %66
36: ; preds = %30
br label %37
37: ; preds = %62, %36
%38 = load i32*, i32** %7
%39 = call %struct.dirent* @readdir(i32* %38)
store %struct.dirent* %39, %struct.dirent** %6
%40 = icmp ne %struct.dirent* %39, null
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = load %struct.dirent*, %struct.dirent** %6
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i64 @strstr(i8* %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %62
47: ; preds = %41
%48 = load %struct.dirent*, %struct.dirent** %6
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* %50)
%52 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%53 = load %struct.dirent*, %struct.dirent** %6
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %55, i8* %58)
%60 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%61 = call i32 @system(i8* %60)
br label %62
62: ; preds = %47, %41
br label %37
63: ; preds = %37
%64 = load i32*, i32** %7
%65 = call i32 @closedir(i32* %64)
br label %66
66: ; preds = %63, %30
store i32 0, i32* %3
br label %67
67: ; preds = %66, %27
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [7 x i8] c"sorted\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [2 x i8] c".\00"
@.str.4 = [13 x i8] c"mkdir sorted\00"
@.str.5 = [5 x i8] c".chr\00"
@.str.6 = [7 x i8] c" - %s\0A\00"
@.str.7 = [30 x i8] c"sort -g %s > sorted/%s_sorted\00"
@str = [21 x i8] c"Sorting regions ... \00"
@str.8 = [27 x i8] c" - regions already sorted \00"
define i32 @sort_regions(i8* %0, i32 %1) {
%3 = alloca [200 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @chdir(i8* %0)
%7 = bitcast %struct.stat* %4 to i8*
%8 = call i32 @stat(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %17, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i64 @S_ISDIR(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
br label %42
17: ; preds = %2, %10
%18 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp eq i32* %18, null
br i1 %21, label %42, label %22
22: ; preds = %17
%23 = tail call %struct.dirent* @readdir(i32* %18)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %40, label %25
25: ; preds = %22, %37
%26 = phi %struct.dirent* [ %38, %37 ], [ %23, %22 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = tail call i64 @strstr(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = load i8*, i8** %27
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %27
%35 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %34, i8* %34)
%36 = call i32 @system(i8* %5)
br label %37
37: ; preds = %31, %25
%38 = tail call %struct.dirent* @readdir(i32* %18)
%39 = icmp eq %struct.dirent* %38, null
br i1 %39, label %40, label %25
40: ; preds = %37, %22
%41 = tail call i32 @closedir(i32* %18)
br label %42
42: ; preds = %17, %40, %15
ret i32 0
}