#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 /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int execute_cmd(int me, char *largv[], char *dir) {
  int rf, res = 0;
  rf = fork();
  if (rf < 0) {
    printf("spanwer(%d): fork failed!!!!\n", me);
    fflush(0);
    return 1;
  }
  if (rf == 0) {
    if (dir != NULL)
      chdir(dir);
    execvp(*largv, largv);
  }
  waitpid(rf, NULL, 0);
  return res;
}


x86 assembly (clang, -O0):
<execute_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b0 <execute_cmd+0x50>
mov    -0x8(%rbp),%esi
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <execute_cmd+0x92>
cmpl   $0x0,-0x1c(%rbp)
jne    11de <execute_cmd+0x7e>
cmpq   $0x0,-0x18(%rbp)
je     11ce <execute_cmd+0x6e>
mov    -0x18(%rbp),%rdi
call   1030 <chdir@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_cmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11a5 <execute_cmd+0x45>
mov    %eax,%ebp
jne    1196 <execute_cmd+0x36>
test   %r15,%r15
je     118b <execute_cmd+0x2b>
mov    %r15,%rdi
call   1030 <chdir@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
jmp    11bf <execute_cmd+0x5f>
lea    0xe54(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"spanwer(%d): fork failed!!!!\0A\00"
define i32 @execute_cmd(i32 %0, i8** %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i32 0, i32* %9
%10 = call i32 (...) @fork()
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* %5
%15 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @fflush(i32 0)
store i32 1, i32* %4
br label %35
17: ; preds = %3
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %7
%25 = call i32 @chdir(i8* %24)
br label %26
26: ; preds = %23, %20
%27 = load i8**, i8*** %6
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %6
%30 = call i32 @execvp(i8* %28, i8** %29)
br label %31
31: ; preds = %26, %17
%32 = load i32, i32* %8
%33 = call i32 @waitpid(i32 %32, i32* null, i32 0)
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %35
35: ; preds = %31, %13
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [30 x i8] c"spanwer(%d): fork failed!!!!\0A\00"
define i32 @execute_cmd(i32 %0, i8** %1, i8* %2) {
%4 = tail call i32 (...) @fork()
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = tail call i32 @fflush(i32 0)
br label %20
9: ; preds = %3
%10 = icmp eq i32 %4, 0
br i1 %10, label %11, label %18
11: ; preds = %9
%12 = icmp eq i8* %2, null
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @chdir(i8* %2)
br label %15
15: ; preds = %13, %11
%16 = load i8*, i8** %1
%17 = tail call i32 @execvp(i8* %16, i8** %1)
br label %18
18: ; preds = %15, %9
%19 = tail call i32 @waitpid(i32 %4, i32* null, i32 0)
br label %20
20: ; preds = %18, %6
%21 = phi i32 [ 1, %6 ], [ 0, %18 ]
ret i32 %21
}