#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
void fork_exec(char *cmd) {
  pid_t pid = fork();
  switch (pid) {
  case 0:
    execlp("/bin/sh", "sh", "-c", cmd, NULL);
    fprintf(stderr, "exec failed, cleaning up child\n");
    exit(1);
  case -1:
    fprintf(stderr, "can't fork\n");
  }
}


x86 assembly (clang, -O0):
<fork_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11bc <fork_exec+0x7c>
jmp    116a <fork_exec+0x2a>
mov    -0x10(%rbp),%eax
test   %eax,%eax
jne    11d1 <fork_exec+0x91>
jmp    117a <fork_exec+0x3a>
mov    -0x8(%rbp),%rcx
lea    0xe7b(%rip),%rdi
lea    0xe79(%rip),%rsi
lea    0xe75(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <execlp@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1030 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     1156 <fork_exec+0x16>
test   %eax,%eax
je     116c <fork_exec+0x2c>
pop    %rbx
ret
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
lea    0xec5(%rip),%rsi
pop    %rbx
jmp    1030 <fprintf@plt>
lea    0xe8d(%rip),%rdi
lea    0xe8b(%rip),%rsi
lea    0xe87(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1050 <execlp@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"exec failed, cleaning up child\0A\00"
@.str.4 = [12 x i8] c"can't fork\0A\00"
define void @fork_exec(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = load i32, i32* %3
switch i32 %5, label %15 [
i32 0, label %6
i32 -1, label %12
]
6: ; preds = %1
%7 = load i8*, i8** %2
%8 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %7, i32* null)
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %15
15: ; preds = %12, %1
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"exec failed, cleaning up child\0A\00"
@.str.4 = [12 x i8] c"can't fork\0A\00"
define void @fork_exec(i8* %0) {
%2 = tail call i32 (...) @fork()
switch i32 %2, label %11 [
i32 0, label %3
i32 -1, label %8
]
3: ; preds = %1
%4 = tail call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32* null)
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %11
11: ; preds = %8, %1
ret void
}