#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 */
char *get_prev_cmd();
int /*<<< orphan*/ is_empty_hist();
char **malloc(int);
int /*<<< orphan*/ reset_actual_ptr();
char *strdup(char *);
int /*<<< orphan*/ *strstr(char *, char *);
char **search_cmd_in_hist(char *cmd_in) {
  int nb_cmd;
  char *my_cmd;
  char **tab_cmd = NULL;
  reset_actual_ptr();
  if (!is_empty_hist()) {
    while ((my_cmd = get_prev_cmd())) {
      if (my_cmd[0] != '\0') {
        if (strstr(my_cmd, cmd_in) != NULL)
          nb_cmd++;
      } else
        break;
    }
    tab_cmd = malloc((nb_cmd + 1) * sizeof(char *));
    nb_cmd = 0;
    reset_actual_ptr();
    while ((my_cmd = get_prev_cmd())) {
      if (my_cmd[0] != '\0') {
        if (strstr(my_cmd, cmd_in) != NULL)
          tab_cmd[nb_cmd] = strdup(my_cmd);
        nb_cmd++;
      } else
        break;
    }
    tab_cmd[nb_cmd] = NULL;
  }
  reset_actual_ptr();
  return NULL;
}


x86 assembly (clang, -O0):
<search_cmd_in_hist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <reset_actual_ptr@plt>
mov    $0x0,%al
call   1060 <is_empty_hist@plt>
cmp    $0x0,%eax
jne    1285 <search_cmd_in_hist+0x125>
jmp    1190 <search_cmd_in_hist+0x30>
mov    $0x0,%al
call   1040 <get_prev_cmd@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11e4 <search_cmd_in_hist+0x84>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11da <search_cmd_in_hist+0x7a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     11d5 <search_cmd_in_hist+0x75>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11df <search_cmd_in_hist+0x7f>
jmp    11e4 <search_cmd_in_hist+0x84>
jmp    1190 <search_cmd_in_hist+0x30>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <reset_actual_ptr@plt>
mov    $0x0,%al
call   1040 <get_prev_cmd@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1275 <search_cmd_in_hist+0x115>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     126b <search_cmd_in_hist+0x10b>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     125d <search_cmd_in_hist+0xfd>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1270 <search_cmd_in_hist+0x110>
jmp    1275 <search_cmd_in_hist+0x115>
jmp    1209 <search_cmd_in_hist+0xa9>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    $0x0,%al
call   1030 <reset_actual_ptr@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_cmd_in_hist>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %eax,%eax
call   1030 <reset_actual_ptr@plt>
xor    %eax,%eax
call   1060 <is_empty_hist@plt>
test   %eax,%eax
jne    1221 <search_cmd_in_hist+0xc1>
xor    %eax,%eax
call   1040 <get_prev_cmd@plt>
test   %rax,%rax
je     11b3 <search_cmd_in_hist+0x53>
nopl   (%rax)
cmpb   $0x0,(%rax)
je     11b3 <search_cmd_in_hist+0x53>
mov    %rax,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
xor    %eax,%eax
call   1040 <get_prev_cmd@plt>
test   %rax,%rax
jne    1190 <search_cmd_in_hist+0x30>
lea    0x8(,%rbx,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %r12d,%r12d
xor    %eax,%eax
call   1030 <reset_actual_ptr@plt>
xor    %eax,%eax
call   1040 <get_prev_cmd@plt>
test   %rax,%rax
je     1219 <search_cmd_in_hist+0xb9>
mov    %rax,%rbx
xor    %r12d,%r12d
jmp    11f3 <search_cmd_in_hist+0x93>
add    $0x1,%r12
xor    %eax,%eax
call   1040 <get_prev_cmd@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1216 <search_cmd_in_hist+0xb6>
cmpb   $0x0,(%rbx)
je     1216 <search_cmd_in_hist+0xb6>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11e0 <search_cmd_in_hist+0x80>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,(%r14,%r12,8)
jmp    11e0 <search_cmd_in_hist+0x80>
mov    %r12d,%r12d
movq   $0x0,(%r14,%r12,8)
xor    %eax,%eax
call   1030 <reset_actual_ptr@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8** @search_cmd_in_hist(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
store i8* %0, i8** %2
store i8** null, i8*** %5
%6 = call i32 (...) @reset_actual_ptr()
%7 = call i32 (...) @is_empty_hist()
%8 = icmp ne i32 %7, 0
br i1 %8, label %69, label %9
9: ; preds = %1
br label %10
10: ; preds = %29, %9
%11 = call i8* (...) @get_prev_cmd()
store i8* %11, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = load i8*, i8** %2
%22 = call i32* @strstr(i8* %20, i8* %21)
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %27
27: ; preds = %24, %19
br label %29
28: ; preds = %13
br label %30
29: ; preds = %27
br label %10
30: ; preds = %28, %10
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = mul i64 %33, 8
%35 = trunc i64 %34 to i32
%36 = call i8** @malloc(i32 %35)
store i8** %36, i8*** %5
store i32 0, i32* %3
%37 = call i32 (...) @reset_actual_ptr()
br label %38
38: ; preds = %63, %30
%39 = call i8* (...) @get_prev_cmd()
store i8* %39, i8** %4
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %64
41: ; preds = %38
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %62
47: ; preds = %41
%48 = load i8*, i8** %4
%49 = load i8*, i8** %2
%50 = call i32* @strstr(i8* %48, i8* %49)
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %59
52: ; preds = %47
%53 = load i8*, i8** %4
%54 = call i8* @strdup(i8* %53)
%55 = load i8**, i8*** %5
%56 = load i32, i32* %3
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8*, i8** %55, i64 %57
store i8* %54, i8** %58
br label %59
59: ; preds = %52, %47
%60 = load i32, i32* %3
%61 = add nsw i32 %60, 1
store i32 %61, i32* %3
br label %63
62: ; preds = %41
br label %64
63: ; preds = %59
br label %38
64: ; preds = %62, %38
%65 = load i8**, i8*** %5
%66 = load i32, i32* %3
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8*, i8** %65, i64 %67
store i8* null, i8** %68
br label %69
69: ; preds = %64, %1
%70 = call i32 (...) @reset_actual_ptr()
ret i8** null
}

LLVM IR (-O2):
define noalias i8** @search_cmd_in_hist(i8* nocapture %0) {
%2 = tail call i32 (...) @reset_actual_ptr()
%3 = tail call i32 (...) @is_empty_hist()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %49
5: ; preds = %1
%6 = tail call i8* (...) @get_prev_cmd()
%7 = icmp eq i8* %6, null
br i1 %7, label %20, label %8
8: ; preds = %5, %13
%9 = phi i8* [ %18, %13 ], [ %6, %5 ]
%10 = phi i32 [ %17, %13 ], [ undef, %5 ]
%11 = load i8, i8* %9
%12 = icmp eq i8 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %8
%14 = tail call i32* @strstr(i8* %9, i8* dereferenceable(1) %0)
%15 = icmp ne i32* %14, null
%16 = zext i1 %15 to i32
%17 = add nsw i32 %10, %16
%18 = tail call i8* (...) @get_prev_cmd()
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %8
20: ; preds = %13, %8, %5
%21 = phi i32 [ undef, %5 ], [ %10, %8 ], [ %17, %13 ]
%22 = shl i32 %21, 3
%23 = add i32 %22, 8
%24 = tail call i8** @malloc(i32 %23)
%25 = tail call i32 (...) @reset_actual_ptr()
%26 = tail call i8* (...) @get_prev_cmd()
%27 = icmp eq i8* %26, null
br i1 %27, label %46, label %28
28: ; preds = %20, %39
%29 = phi i64 [ %40, %39 ], [ 0, %20 ]
%30 = phi i8* [ %41, %39 ], [ %26, %20 ]
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %43, label %33
33: ; preds = %28
%34 = tail call i32* @strstr(i8* %30, i8* dereferenceable(1) %0)
%35 = icmp eq i32* %34, null
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = tail call i8* @strdup(i8* %30)
%38 = getelementptr inbounds i8*, i8** %24, i64 %29
store i8* %37, i8** %38
br label %39
39: ; preds = %36, %33
%40 = add nuw i64 %29, 1
%41 = tail call i8* (...) @get_prev_cmd()
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %28
43: ; preds = %28, %39
%44 = phi i64 [ %40, %39 ], [ %29, %28 ]
%45 = and i64 %44, 4294967295
br label %46
46: ; preds = %43, %20
%47 = phi i64 [ 0, %20 ], [ %45, %43 ]
%48 = getelementptr inbounds i8*, i8** %24, i64 %47
store i8* null, i8** %48
br label %49
49: ; preds = %46, %1
%50 = tail call i32 (...) @reset_actual_ptr()
ret i8** null
}