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

/* Variables and functions */
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int spawn(char *nomefile, pid_t *pid) {
  pid_t child = fork();
  if (child < 0) {
    printf("Fork error.\n");
    return -1;
  }
  if (child == 0) {
    execl(nomefile, nomefile, NULL);
    printf("Exec error.\n");
    exit(-1);
  } else {
    *pid = child;
    return 1;
  }
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    117e <spawn+0x3e>
lea    0xe93(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <spawn+0x82>
cmpq   $0x0,-0x20(%rbp)
jne    11b0 <spawn+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execl@plt>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1162 <spawn+0x22>
je     117b <spawn+0x3b>
mov    %rax,(%rbx)
mov    $0x1,%eax
jmp    1173 <spawn+0x33>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <execl@plt>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"Fork error.\0A\00"
@.str.1 = [13 x i8] c"Exec error.\0A\00"
define i32 @spawn(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %24
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load i8*, i8** %4
%18 = call i32 @execl(i8* %16, i8* %17, i32* null)
%19 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %12
%22 = load i64, i64* %6
%23 = load i64*, i64** %5
store i64 %22, i64* %23
store i32 1, i32* %3
br label %24
24: ; preds = %21, %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@str = [12 x i8] c"Exec error.\00"
@str.2 = [12 x i8] c"Fork error.\00"
define i32 @spawn(i8* %0, i64* nocapture writeonly %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.2, i64 0, i64 0))
br label %14
7: ; preds = %2
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = tail call i32 @execl(i8* %0, i8* %0, i32* null)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %7
store i64 %3, i64* %1
br label %14
14: ; preds = %13, %5
%15 = phi i32 [ -1, %5 ], [ 1, %13 ]
ret i32 %15
}