#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*/ grep_args;

/* Variables and functions */
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ free(char **);
scalar_t__ malloc(int);
char *strdup(char *);
int grep(char *grep_args[]) {
  int i;
  char **new_args;
  new_args = (char **)malloc(sizeof(char *) * (sizeof(grep_args) + 1));
  new_args[0] = strdup("/bin/grep");
  for (i = 0; grep_args[i]; i++)
    new_args[i + 1] = grep_args[i];
  new_args[i + 1] = NULL;
  execv("/bin/grep", new_args);
  free(new_args);
  return 0;
}


x86 assembly (clang, -O0):
<grep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x48,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe9f(%rip),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11b5 <grep+0x75>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1177 <grep+0x37>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rsi
lea    0xe2b(%rip),%rdi
call   1060 <execv@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grep>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x48,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
lea    0xea5(%rip),%rdi
call   1050 <strdup@plt>
mov    %rax,(%r14)
mov    (%rbx),%rax
test   %rax,%rax
je     1185 <grep+0x45>
mov    $0x1,%ecx
mov    %rax,(%r14,%rcx,8)
mov    (%rbx,%rcx,8),%rax
add    $0x1,%rcx
test   %rax,%rax
jne    1170 <grep+0x30>
mov    %ecx,%eax
jmp    118a <grep+0x4a>
mov    $0x1,%eax
movq   $0x0,(%r14,%rax,8)
lea    0xe67(%rip),%rdi
mov    %r14,%rsi
call   1060 <execv@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/bin/grep\00"
define i32 @grep(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i8**
store i8** %0, i8*** %2
%5 = call i64 @malloc(i32 72)
%6 = inttoptr i64 %5 to i8**
store i8** %6, i8*** %4
%7 = call i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = load i8**, i8*** %4
%9 = getelementptr inbounds i8*, i8** %8, i64 0
store i8* %7, i8** %9
store i32 0, i32* %3
br label %10
10: ; preds = %28, %1
%11 = load i8**, i8*** %2
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8*, i8** %11, i64 %13
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %31
17: ; preds = %10
%18 = load i8**, i8*** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8*, i8** %21
%23 = load i8**, i8*** %4
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %23, i64 %26
store i8* %22, i8** %27
br label %28
28: ; preds = %17
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %10
31: ; preds = %10
%32 = load i8**, i8*** %4
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %32, i64 %35
store i8* null, i8** %36
%37 = load i8**, i8*** %4
%38 = call i32 @execv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %37)
%39 = load i8**, i8*** %4
%40 = call i32 @free(i8** %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"/bin/grep\00"
define i32 @grep(i8** nocapture %0) {
%2 = tail call i64 @malloc(i32 72)
%3 = inttoptr i64 %2 to i8**
%4 = tail call dereferenceable_or_null(10) i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %3
%5 = load i8*, i8** %0
%6 = icmp eq i8* %5, null
br i1 %6, label %18, label %7
7: ; preds = %1, %7
%8 = phi i64 [ %10, %7 ], [ 0, %1 ]
%9 = phi i8* [ %13, %7 ], [ %5, %1 ]
%10 = add nuw nsw i64 %8, 1
%11 = getelementptr inbounds i8*, i8** %3, i64 %10
store i8* %9, i8** %11
%12 = getelementptr inbounds i8*, i8** %0, i64 %10
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %7
15: ; preds = %7
%16 = add nuw i64 %8, 2
%17 = and i64 %16, 4294967295
br label %18
18: ; preds = %15, %1
%19 = phi i64 [ 1, %1 ], [ %17, %15 ]
%20 = getelementptr inbounds i8*, i8** %3, i64 %19
store i8* null, i8** %20
%21 = tail call i32 @execv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %3)
%22 = tail call i32 @free(i8** %3)
ret i32 0
}