#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 count_character(char, char *);
int /*<<< orphan*/ piece(char *, char, char *, int);
char *pipe2string(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
char *timlib_get_latest_filename_datestamp(char *directory) {
  char sys_string[1024];
  char *filename;
  int underbar_count;
  static char datestamp[128];
  sprintf(sys_string, "ls -1t %s 2>/dev/null | head -1", directory);
  if (!(filename = pipe2string(sys_string)))
    return (char *)0;
  underbar_count = count_character('_', filename);
  if (!underbar_count)
    return (char *)0;
  piece(datestamp, '_', filename, underbar_count);
  if (strlen(datestamp) < 8)
    return (char *)0;
  *(datestamp + 8) = '\0';
  return datestamp;
}


x86 assembly (clang, -O0):
<timlib_get_latest_filename_datestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe8f(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x410(%rbp),%rdi
call   1050 <pipe2string@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jne    11a0 <timlib_get_latest_filename_datestamp+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1223 <timlib_get_latest_filename_datestamp+0xd3>
mov    -0x418(%rbp),%rsi
mov    $0x5f,%edi
call   1040 <count_character@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jne    11d1 <timlib_get_latest_filename_datestamp+0x81>
movq   $0x0,-0x8(%rbp)
jmp    1223 <timlib_get_latest_filename_datestamp+0xd3>
mov    -0x418(%rbp),%rdx
mov    -0x41c(%rbp),%ecx
lea    0x2e7b(%rip),%rdi
mov    $0x5f,%esi
call   1070 <piece@plt>
lea    0x2e6a(%rip),%rdi
call   1030 <strlen@plt>
cmp    $0x8,%eax
jge    1211 <timlib_get_latest_filename_datestamp+0xc1>
movq   $0x0,-0x8(%rbp)
jmp    1223 <timlib_get_latest_filename_datestamp+0xd3>
movb   $0x0,0x2e50(%rip)
lea    0x2e41(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timlib_get_latest_filename_datestamp>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rdx
lea    0xe9c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <pipe2string@plt>
test   %rax,%rax
je     11b6 <timlib_get_latest_filename_datestamp+0x66>
mov    %rax,%rbx
mov    $0x5f,%edi
mov    %rax,%rsi
call   1040 <count_character@plt>
test   %eax,%eax
je     11b6 <timlib_get_latest_filename_datestamp+0x66>
lea    0x2ec9(%rip),%r14
mov    %r14,%rdi
mov    $0x5f,%esi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1070 <piece@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
cmp    $0x8,%eax
jge    11bb <timlib_get_latest_filename_datestamp+0x6b>
xor    %r14d,%r14d
jmp    11c2 <timlib_get_latest_filename_datestamp+0x72>
movb   $0x0,0x2ea6(%rip)
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@timlib_get_latest_filename_datestamp.datestamp = internal global [128 x i8] zeroinitializer
@.str = [32 x i8] c"ls -1t %s 2>/dev/null | head -1\00"
define i8* @timlib_get_latest_filename_datestamp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i8* @pipe2string(i8* %10)
store i8* %11, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i8* null, i8** %2
br label %28
14: ; preds = %1
%15 = load i8*, i8** %5
%16 = call i32 @count_character(i8 signext 95, i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %14
store i8* null, i8** %2
br label %28
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = call i32 @piece(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 0), i8 signext 95, i8* %21, i32 %22)
%24 = call i32 @strlen(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 0))
%25 = icmp slt i32 %24, 8
br i1 %25, label %26, label %27
26: ; preds = %20
store i8* null, i8** %2
br label %28
27: ; preds = %20
store i8 0, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 8)
store i8* getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 0), i8** %2
br label %28
28: ; preds = %27, %26, %19, %13
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@timlib_get_latest_filename_datestamp.datestamp = internal global [128 x i8] zeroinitializer
@.str = [32 x i8] c"ls -1t %s 2>/dev/null | head -1\00"
define i8* @timlib_get_latest_filename_datestamp(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i8* @pipe2string(i8* %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = call i32 @count_character(i8 signext 95, i8* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = call i32 @piece(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 0), i8 signext 95, i8* %5, i32 %8)
%12 = call i32 @strlen(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 0))
%13 = icmp slt i32 %12, 8
br i1 %13, label %15, label %14
14: ; preds = %10
store i8 0, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 8)
br label %15
15: ; preds = %10, %7, %1, %14
%16 = phi i8* [ getelementptr inbounds ([128 x i8], [128 x i8]* @timlib_get_latest_filename_datestamp.datestamp, i64 0, i64 0), %14 ], [ null, %1 ], [ null, %7 ], [ null, %10 ]
ret i8* %16
}