#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 hotplug2_event_t {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
char *replace_key_by_value(char *, struct hotplug2_event_t *);
char *strdup(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int exec_noshell(struct hotplug2_event_t *event, char *application,
                 char **argv) {
  pid_t p;
  int i, status;
  p = fork();
  switch (p) {
  case -1:
    return -1;
  case 0:
    application = replace_key_by_value(strdup(application), event);
    for (i = 0; argv[i] != NULL; i++) {
      argv[i] = replace_key_by_value(argv[i], event);
    }
    execvp(application, argv);
    exit(127);
    break;
  default:
    if (waitpid(p, &status, 0) == -1)
      return -1;
    return WEXITSTATUS(status);
    break;
  }
}


x86 assembly (clang, -O0):
<exec_noshell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0xffffffff,%eax
je     11b2 <exec_noshell+0x42>
jmp    11a2 <exec_noshell+0x32>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     11be <exec_noshell+0x4e>
jmp    123a <exec_noshell+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <exec_noshell+0xf8>
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <replace_key_by_value@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1223 <exec_noshell+0xb3>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <replace_key_by_value@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11de <exec_noshell+0x6e>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <execvp@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    125d <exec_noshell+0xed>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <exec_noshell+0xf8>
mov    -0x2c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_noshell>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11b4 <exec_noshell+0x44>
test   %eax,%eax
je     11c0 <exec_noshell+0x50>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11af <exec_noshell+0x3f>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
jmp    11b4 <exec_noshell+0x44>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   1030 <replace_key_by_value@plt>
mov    %rax,%r15
mov    (%r14),%rdi
test   %rdi,%rdi
je     1208 <exec_noshell+0x98>
lea    0x8(%r14),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rsi
call   1030 <replace_key_by_value@plt>
mov    %rax,-0x8(%rbx)
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    11f0 <exec_noshell+0x80>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.hotplug2_event_t = type { i32 }
define i32 @exec_noshell(%struct.hotplug2_event_t* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca %struct.hotplug2_event_t*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.hotplug2_event_t* %0, %struct.hotplug2_event_t** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
%11 = call i32 (...) @fork()
store i32 %11, i32* %8
%12 = load i32, i32* %8
switch i32 %12, label %46 [
i32 -1, label %13
i32 0, label %14
]
13: ; preds = %3
store i32 -1, i32* %4
br label %54
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = call i8* @strdup(i8* %15)
%17 = load %struct.hotplug2_event_t*, %struct.hotplug2_event_t** %5
%18 = call i8* @replace_key_by_value(i8* %16, %struct.hotplug2_event_t* %17)
store i8* %18, i8** %6
store i32 0, i32* %9
br label %19
19: ; preds = %38, %14
%20 = load i8**, i8*** %7
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %41
26: ; preds = %19
%27 = load i8**, i8*** %7
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = load %struct.hotplug2_event_t*, %struct.hotplug2_event_t** %5
%33 = call i8* @replace_key_by_value(i8* %31, %struct.hotplug2_event_t* %32)
%34 = load i8**, i8*** %7
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
store i8* %33, i8** %37
br label %38
38: ; preds = %26
%39 = load i32, i32* %9
%40 = add nsw i32 %39, 1
store i32 %40, i32* %9
br label %19
41: ; preds = %19
%42 = load i8*, i8** %6
%43 = load i8**, i8*** %7
%44 = call i32 @execvp(i8* %42, i8** %43)
%45 = call i32 @exit(i32 127)
unreachable
46: ; preds = %3
%47 = load i32, i32* %8
%48 = call i32 @waitpid(i32 %47, i32* %10, i32 0)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 -1, i32* %4
br label %54
51: ; preds = %46
%52 = load i32, i32* %10
%53 = call i32 @WEXITSTATUS(i32 %52)
store i32 %53, i32* %4
br label %54
54: ; preds = %51, %50, %13
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.hotplug2_event_t = type { i32 }
define i32 @exec_noshell(%struct.hotplug2_event_t* %0, i8* nocapture %1, i8** %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 (...) @fork()
switch i32 %6, label %24 [
i32 -1, label %30
i32 0, label %7
]
7: ; preds = %3
%8 = tail call i8* @strdup(i8* %1)
%9 = tail call i8* @replace_key_by_value(i8* %8, %struct.hotplug2_event_t* %0)
%10 = load i8*, i8** %2
%11 = icmp eq i8* %10, null
br i1 %11, label %21, label %12
12: ; preds = %7, %12
%13 = phi i64 [ %17, %12 ], [ 0, %7 ]
%14 = phi i8* [ %19, %12 ], [ %10, %7 ]
%15 = phi i8** [ %18, %12 ], [ %2, %7 ]
%16 = tail call i8* @replace_key_by_value(i8* %14, %struct.hotplug2_event_t* %0)
store i8* %16, i8** %15
%17 = add nuw i64 %13, 1
%18 = getelementptr inbounds i8*, i8** %2, i64 %17
%19 = load i8*, i8** %18
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %12
21: ; preds = %12, %7
%22 = tail call i32 @execvp(i8* %9, i8** %2)
%23 = tail call i32 @exit(i32 127)
unreachable
24: ; preds = %3
%25 = call i32 @waitpid(i32 %6, i32* %4, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %4
%29 = call i32 @WEXITSTATUS(i32 %28)
br label %30
30: ; preds = %24, %3, %27
%31 = phi i32 [ %29, %27 ], [ %6, %3 ], [ -1, %24 ]
ret i32 %31
}