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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
char *strrchr(char *, char);
char *strstr(char *, char *);
int strtol(char *, int /*<<< orphan*/ *, int);
char *_DC_message_name(char *box, char *name) {
  char *dn, *n;
  DIR *d;
  struct dirent *de;
  int min_id;
  if (!box || !name)
    return (NULL);
  if (!*box || !*name)
    return (NULL);
  dn = strdup(box);
  n = malloc(strlen(name) + 10);
  strcpy(n, name);
  strcat(n, ".");
  d = opendir(dn);
  min_id = -1;
  while (d && (de = readdir(d)) != NULL) {
    char *found = strstr(de->d_name, n);
    if (found == de->d_name) {
      char *pos = strrchr(de->d_name, '.');
      if (pos) {
        int id = 0;
        pos++;
        id = strtol(pos, NULL, 10);
        if (id > 0) {
          if (min_id < 0)
            min_id = id;
          else if (id < min_id)
            min_id = id;
        }
      }
    }
  }
  if (d)
    closedir(d);
  if (min_id >= 0) {
    dn = realloc(dn, strlen(box) + strlen(name) + 100);
    sprintf(dn, "%s/%s.%d", box, name, min_id);
    free(n);
    return (dn);
  }
  if (dn)
    free(dn);
  if (n)
    free(n);
  return (NULL);
}


x86 assembly (clang, -O0):
<_DC_message_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1206 <_DC_message_name+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1213 <_DC_message_name+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1435 <_DC_message_name+0x255>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     122d <_DC_message_name+0x4d>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    123a <_DC_message_name+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    1435 <_DC_message_name+0x255>
mov    -0x10(%rbp),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0xa,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xd88(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x30(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x55(%rbp)
je     12b8 <_DC_message_name+0xd8>
mov    -0x30(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    12c8 <_DC_message_name+0xe8>
jmp    137f <_DC_message_name+0x19f>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1100 <strstr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rcx
cmp    (%rcx),%rax
jne    137a <_DC_message_name+0x19a>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1375 <_DC_message_name+0x195>
movl   $0x0,-0x54(%rbp)
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jle    1370 <_DC_message_name+0x190>
cmpl   $0x0,-0x3c(%rbp)
jge    1354 <_DC_message_name+0x174>
mov    -0x54(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    136b <_DC_message_name+0x18b>
mov    -0x54(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1366 <_DC_message_name+0x186>
mov    -0x54(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    136b <_DC_message_name+0x18b>
jmp    1370 <_DC_message_name+0x190>
jmp    1375 <_DC_message_name+0x195>
jmp    137a <_DC_message_name+0x19a>
jmp    1291 <_DC_message_name+0xb1>
cmpq   $0x0,-0x30(%rbp)
je     1393 <_DC_message_name+0x1b3>
mov    -0x30(%rbp),%rdi
call   1080 <closedir@plt>
cmpl   $0x0,-0x3c(%rbp)
jl     1405 <_DC_message_name+0x225>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdi
add    %rax,%rsi
add    $0x64,%rsi
call   10c0 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x3c(%rbp),%r8d
lea    0xc18(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1435 <_DC_message_name+0x255>
cmpq   $0x0,-0x20(%rbp)
je     1419 <_DC_message_name+0x239>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
je     142d <_DC_message_name+0x24d>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_DC_message_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdi,%rdi
je     1350 <_DC_message_name+0x170>
mov    %rsi,%r14
test   %rsi,%rsi
je     1350 <_DC_message_name+0x170>
mov    %rdi,%r15
cmpb   $0x0,(%rdi)
je     134e <_DC_message_name+0x16e>
cmpb   $0x0,(%r14)
je     134e <_DC_message_name+0x16e>
mov    %r15,%rdi
call   1100 <strdup@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1070 <strlen@plt>
lea    0xa(%rax),%rdi
call   10c0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
lea    0xdb7(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
mov    %rbp,(%rsp)
mov    %rbp,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     1333 <_DC_message_name+0x153>
mov    %rax,%rbp
mov    %rax,%rdi
call   10b0 <readdir@plt>
mov    $0xffffffff,%r13d
jmp    1288 <_DC_message_name+0xa8>
cmp    %r13d,%eax
cmovb  %eax,%r13d
mov    %rbp,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12dd <_DC_message_name+0xfd>
mov    (%rax),%r12
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    1280 <_DC_message_name+0xa0>
mov    %r12,%rdi
mov    $0x2e,%esi
call   1080 <strrchr@plt>
test   %rax,%rax
je     1280 <_DC_message_name+0xa0>
add    $0x1,%rax
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
test   %eax,%eax
jle    1280 <_DC_message_name+0xa0>
test   %r13d,%r13d
jns    1279 <_DC_message_name+0x99>
mov    %eax,%r13d
jmp    1280 <_DC_message_name+0xa0>
mov    %rbp,%rdi
call   1090 <closedir@plt>
test   %r13d,%r13d
js     1333 <_DC_message_name+0x153>
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1070 <strlen@plt>
lea    (%rax,%rbp,1),%rsi
add    $0x64,%rsi
mov    (%rsp),%rdi
call   10d0 <realloc@plt>
mov    %rax,%rbp
lea    0xcea(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r13d,%r8d
call   10f0 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1350 <_DC_message_name+0x170>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1341 <_DC_message_name+0x161>
call   1030 <free@plt>
test   %rbx,%rbx
je     134e <_DC_message_name+0x16e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [9 x i8] c"%s/%s.%d\00"
define i8* @_DC_message_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %16, %2
store i8* null, i8** %3
br label %132
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = load i8, i8* %25
%27 = icmp ne i8 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %24, %20
store i8* null, i8** %3
br label %132
29: ; preds = %24
%30 = load i8*, i8** %4
%31 = call i8* @strdup(i8* %30)
store i8* %31, i8** %6
%32 = load i8*, i8** %5
%33 = call i64 @strlen(i8* %32)
%34 = add nsw i64 %33, 10
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = load i8*, i8** %5
%38 = call i32 @strcpy(i8* %36, i8* %37)
%39 = load i8*, i8** %7
%40 = call i32 @strcat(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = load i8*, i8** %6
%42 = call i32* @opendir(i8* %41)
store i32* %42, i32** %8
store i32 -1, i32* %10
br label %43
43: ; preds = %92, %29
%44 = load i32*, i32** %8
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i32*, i32** %8
%48 = call %struct.dirent* @readdir(i32* %47)
store %struct.dirent* %48, %struct.dirent** %9
%49 = icmp ne %struct.dirent* %48, null
br label %50
50: ; preds = %46, %43
%51 = phi i1 [ false, %43 ], [ %49, %46 ]
br i1 %51, label %52, label %93
52: ; preds = %50
%53 = load %struct.dirent*, %struct.dirent** %9
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = load i8*, i8** %7
%57 = call i8* @strstr(i8* %55, i8* %56)
store i8* %57, i8** %11
%58 = load i8*, i8** %11
%59 = load %struct.dirent*, %struct.dirent** %9
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = icmp eq i8* %58, %61
br i1 %62, label %63, label %92
63: ; preds = %52
%64 = load %struct.dirent*, %struct.dirent** %9
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i8* @strrchr(i8* %66, i8 signext 46)
store i8* %67, i8** %12
%68 = load i8*, i8** %12
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %91
70: ; preds = %63
store i32 0, i32* %13
%71 = load i8*, i8** %12
%72 = getelementptr inbounds i8, i8* %71, i32 1
store i8* %72, i8** %12
%73 = load i8*, i8** %12
%74 = call i32 @strtol(i8* %73, i32* null, i32 10)
store i32 %74, i32* %13
%75 = load i32, i32* %13
%76 = icmp sgt i32 %75, 0
br i1 %76, label %77, label %90
77: ; preds = %70
%78 = load i32, i32* %10
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %77
%81 = load i32, i32* %13
store i32 %81, i32* %10
br label %89
82: ; preds = %77
%83 = load i32, i32* %13
%84 = load i32, i32* %10
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %88
86: ; preds = %82
%87 = load i32, i32* %13
store i32 %87, i32* %10
br label %88
88: ; preds = %86, %82
br label %89
89: ; preds = %88, %80
br label %90
90: ; preds = %89, %70
br label %91
91: ; preds = %90, %63
br label %92
92: ; preds = %91, %52
br label %43
93: ; preds = %50
%94 = load i32*, i32** %8
%95 = icmp ne i32* %94, null
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = load i32*, i32** %8
%98 = call i32 @closedir(i32* %97)
br label %99
99: ; preds = %96, %93
%100 = load i32, i32* %10
%101 = icmp sge i32 %100, 0
br i1 %101, label %102, label %119
102: ; preds = %99
%103 = load i8*, i8** %6
%104 = load i8*, i8** %4
%105 = call i64 @strlen(i8* %104)
%106 = load i8*, i8** %5
%107 = call i64 @strlen(i8* %106)
%108 = add nsw i64 %105, %107
%109 = add nsw i64 %108, 100
%110 = call i8* @realloc(i8* %103, i64 %109)
store i8* %110, i8** %6
%111 = load i8*, i8** %6
%112 = load i8*, i8** %4
%113 = load i8*, i8** %5
%114 = load i32, i32* %10
%115 = call i32 @sprintf(i8* %111, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %112, i8* %113, i32 %114)
%116 = load i8*, i8** %7
%117 = call i32 @free(i8* %116)
%118 = load i8*, i8** %6
store i8* %118, i8** %3
br label %132
119: ; preds = %99
%120 = load i8*, i8** %6
%121 = icmp ne i8* %120, null
br i1 %121, label %122, label %125
122: ; preds = %119
%123 = load i8*, i8** %6
%124 = call i32 @free(i8* %123)
br label %125
125: ; preds = %122, %119
%126 = load i8*, i8** %7
%127 = icmp ne i8* %126, null
br i1 %127, label %128, label %131
128: ; preds = %125
%129 = load i8*, i8** %7
%130 = call i32 @free(i8* %129)
br label %131
131: ; preds = %128, %125
store i8* null, i8** %3
br label %132
132: ; preds = %131, %102, %28, %19
%133 = load i8*, i8** %3
ret i8* %133
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [9 x i8] c"%s/%s.%d\00"
define noalias i8* @_DC_message_name(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %68
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %68, label %9
9: ; preds = %6
%10 = load i8, i8* %1
%11 = icmp eq i8 %10, 0
br i1 %11, label %68, label %12
12: ; preds = %9
%13 = tail call i8* @strdup(i8* %0)
%14 = tail call i64 @strlen(i8* %1)
%15 = add nsw i64 %14, 10
%16 = tail call i8* @malloc(i64 %15)
%17 = tail call i32 @strcpy(i8* %16, i8* %1)
%18 = tail call i32 @strcat(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32* @opendir(i8* %13)
%20 = icmp eq i32* %19, null
br i1 %20, label %60, label %21
21: ; preds = %12
%22 = tail call %struct.dirent* @readdir(i32* %19)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %48, label %24
24: ; preds = %21, %44
%25 = phi %struct.dirent* [ %46, %44 ], [ %22, %21 ]
%26 = phi i32 [ %45, %44 ], [ -1, %21 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = tail call i64 @strlen(i8* dereferenceable(1) %16)
%30 = tail call i32 @strncmp(i8* %28, i8* %16, i64 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %24
%33 = tail call i8* @strrchr(i8* dereferenceable(1) %28, i8 signext 46)
%34 = icmp eq i8* %33, null
br i1 %34, label %44, label %35
35: ; preds = %32
%36 = getelementptr inbounds i8, i8* %33, i64 1
%37 = tail call i32 @strtol(i8* nocapture %36, i32* null, i32 10)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = icmp slt i32 %26, 0
br i1 %40, label %44, label %41
41: ; preds = %39
%42 = icmp ult i32 %37, %26
%43 = select i1 %42, i32 %37, i32 %26
br label %44
44: ; preds = %41, %32, %39, %35, %24
%45 = phi i32 [ %26, %24 ], [ %26, %32 ], [ %26, %35 ], [ %37, %39 ], [ %43, %41 ]
%46 = tail call %struct.dirent* @readdir(i32* %19)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %48, label %24
48: ; preds = %44, %21
%49 = phi i32 [ -1, %21 ], [ %45, %44 ]
%50 = tail call i32 @closedir(i32* %19)
%51 = icmp sgt i32 %49, -1
br i1 %51, label %52, label %60
52: ; preds = %48
%53 = tail call i64 @strlen(i8* %0)
%54 = tail call i64 @strlen(i8* %1)
%55 = add i64 %53, 100
%56 = add i64 %55, %54
%57 = tail call i8* @realloc(i8* %13, i64 %56)
%58 = tail call i32 @sprintf(i8* dereferenceable(1) %57, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i32 %49)
%59 = tail call i32 @free(i8* %16)
br label %68
60: ; preds = %12, %48
%61 = icmp eq i8* %13, null
br i1 %61, label %64, label %62
62: ; preds = %60
%63 = tail call i32 @free(i8* %13)
br label %64
64: ; preds = %62, %60
%65 = icmp eq i8* %16, null
br i1 %65, label %68, label %66
66: ; preds = %64
%67 = tail call i32 @free(i8* %16)
br label %68
68: ; preds = %64, %66, %6, %9, %2, %52
%69 = phi i8* [ %57, %52 ], [ null, %2 ], [ null, %9 ], [ null, %6 ], [ null, %66 ], [ null, %64 ]
ret i8* %69
}