#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 scalar_t__ pid_t;
typedef int int8_t;

/* Variables and functions */
scalar_t__ execl(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
scalar_t__ system(char *);
int8_t portable_spawn(char *os, char *cmd, char *argv) {
  int8_t status = 0;
  if (strcmp("_WIN32", os) == 0) {
  } else if (strcmp("__linux__", os) == 0) {
    pid_t pid = 0;
    pid = fork();
    if (pid == 0) {
      if (execl(cmd, argv, NULL))
        status = 1;
      else
        status = 0;
    }
  } else {
    char *cmdArgs = (char *)malloc(strlen(cmd) + strlen(argv) + 16);
    strcpy(cmdArgs, cmd);
    strcat(cmdArgs, " ");
    strcat(cmdArgs, argv);
    pid_t pid = 0;
    pid = fork();
    if (pid == 0) {
      if (system(cmd) != 0)
        status = 1;
      else
        status = 0;
    }
    free(cmdArgs);
  }
  return status;
}


x86 assembly (clang, -O0):
<portable_spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11ca <portable_spawn+0x3a>
jmp    12e5 <portable_spawn+0x155>
mov    -0x8(%rbp),%rsi
lea    0xe32(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    123a <portable_spawn+0xaa>
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1235 <portable_spawn+0xa5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <execl@plt>
cmp    $0x0,%rax
je     1229 <portable_spawn+0x99>
movl   $0x1,-0x1c(%rbp)
jmp    1230 <portable_spawn+0xa0>
movl   $0x0,-0x1c(%rbp)
jmp    1235 <portable_spawn+0xa5>
jmp    12e0 <portable_spawn+0x150>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x10,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strcat@plt>
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12d7 <portable_spawn+0x147>
mov    -0x10(%rbp),%rdi
call   1060 <system@plt>
cmp    $0x0,%rax
je     12cb <portable_spawn+0x13b>
movl   $0x1,-0x1c(%rbp)
jmp    12d2 <portable_spawn+0x142>
movl   $0x0,-0x1c(%rbp)
jmp    12d7 <portable_spawn+0x147>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    12e5 <portable_spawn+0x155>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portable_spawn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe59(%rip),%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     123d <portable_spawn+0xad>
lea    0xe48(%rip),%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1241 <portable_spawn+0xb1>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x10,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    0xe10(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    1233 <portable_spawn+0xa3>
mov    %r14,%rdi
call   1060 <system@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1265 <portable_spawn+0xd5>
xor    %ebp,%ebp
jmp    1265 <portable_spawn+0xd5>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    1265 <portable_spawn+0xd5>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   10a0 <execl@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"_WIN32\00"
@.str.1 = [10 x i8] c"__linux__\00"
@.str.2 = [2 x i8] c" \00"
define i32 @portable_spawn(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %7
%11 = load i8*, i8** %4
%12 = call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
br label %63
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = call i64 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %15
store i64 0, i64* %8
%20 = call i64 (...) @fork()
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = load i8*, i8** %5
%25 = load i8*, i8** %6
%26 = call i64 @execl(i8* %24, i8* %25, i32* null)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 1, i32* %7
br label %30
29: ; preds = %23
store i32 0, i32* %7
br label %30
30: ; preds = %29, %28
br label %31
31: ; preds = %30, %19
br label %62
32: ; preds = %15
%33 = load i8*, i8** %5
%34 = call i64 @strlen(i8* %33)
%35 = load i8*, i8** %6
%36 = call i64 @strlen(i8* %35)
%37 = add nsw i64 %34, %36
%38 = add nsw i64 %37, 16
%39 = call i64 @malloc(i64 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %9
%41 = load i8*, i8** %9
%42 = load i8*, i8** %5
%43 = call i32 @strcpy(i8* %41, i8* %42)
%44 = load i8*, i8** %9
%45 = call i32 @strcat(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = load i8*, i8** %9
%47 = load i8*, i8** %6
%48 = call i32 @strcat(i8* %46, i8* %47)
store i64 0, i64* %10
%49 = call i64 (...) @fork()
store i64 %49, i64* %10
%50 = load i64, i64* %10
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %32
%53 = load i8*, i8** %5
%54 = call i64 @system(i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52
store i32 1, i32* %7
br label %58
57: ; preds = %52
store i32 0, i32* %7
br label %58
58: ; preds = %57, %56
br label %59
59: ; preds = %58, %32
%60 = load i8*, i8** %9
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %59, %31
br label %63
63: ; preds = %62, %14
%64 = load i32, i32* %7
ret i32 %64
}

LLVM IR (-O2):
@.str = [7 x i8] c"_WIN32\00"
@.str.1 = [10 x i8] c"__linux__\00"
@.str.2 = [2 x i8] c" \00"
define i32 @portable_spawn(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %35, label %6
6: ; preds = %3
%7 = tail call i64 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %6
%10 = tail call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %9
%13 = tail call i64 @execl(i8* %1, i8* %2, i32* null)
%14 = icmp ne i64 %13, 0
%15 = zext i1 %14 to i32
br label %35
16: ; preds = %6
%17 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%18 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%19 = add i64 %17, 16
%20 = add i64 %19, %18
%21 = tail call i64 @malloc(i64 %20)
%22 = inttoptr i64 %21 to i8*
%23 = tail call i32 @strcpy(i8* %22, i8* %1)
%24 = tail call i32 @strcat(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @strcat(i8* %22, i8* %2)
%26 = tail call i64 (...) @fork()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = tail call i64 @system(i8* %1)
%30 = icmp ne i64 %29, 0
%31 = zext i1 %30 to i32
br label %32
32: ; preds = %28, %16
%33 = phi i32 [ 0, %16 ], [ %31, %28 ]
%34 = tail call i32 @free(i8* %22)
br label %35
35: ; preds = %9, %12, %32, %3
%36 = phi i32 [ 0, %3 ], [ %33, %32 ], [ 0, %9 ], [ %15, %12 ]
ret i32 %36
}