#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*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main() {
  signal(SIGCHLD, SIG_IGN);
  extern char **environ;
  printf("Starting artha...\n");
  pid_t artha_fork;
  if ((artha_fork = fork()) < 0) {
    fprintf(stderr, "Failed to fork\n");
    exit(1);
  }
  if (artha_fork > 0) {
    printf("Successfull fork...\n");
    exit(0);
  }
  char *cmd_line[] = {"/usr/bin/artha", NULL};
  execve("/usr/bin/artha", cmd_line, environ);
  fprintf(stderr, "Failed to execute\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
lea    0xe73(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11c6 <main+0x66>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11e4 <main+0x84>
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2c05(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c02(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    0x2dd3(%rip),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rdi
call   1040 <execve@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11d8 <main+0x78>
jne    11f7 <main+0x97>
movaps 0x2c54(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%rdx
lea    0xe5f(%rip),%rdi
mov    %rsp,%rsi
call   1040 <execve@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
ret
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [19 x i8] c"Starting artha...\0A\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"Failed to fork\0A\00"
@.str.2 = [21 x i8] c"Successfull fork...\0A\00"
@.str.3 = [15 x i8] c"/usr/bin/artha\00"
@__const.main.cmd_line = [2 x i8*] [i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i32 0, i32 0), i8* null]
@environ = external global i8**
@.str.4 = [19 x i8] c"Failed to execute\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i8*]
store i32 0, i32* %1
%4 = load i32, i32* @SIGCHLD
%5 = load i32, i32* @SIG_IGN
%6 = call i32 @signal(i32 %4, i32 %5)
%7 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i64, i64* %2
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %14
%21 = bitcast [2 x i8*]* %3 to i8*
%22 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%23 = load i8**, i8*** @environ
%24 = call i32 @execve(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8** %22, i8** %23)
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
ret i32 1
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"Failed to fork\0A\00"
@.str.3 = [15 x i8] c"/usr/bin/artha\00"
@__const.main.cmd_line = [2 x i8*] [i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i32 0, i32 0), i8* null]
@environ = external global i8**
@.str.4 = [19 x i8] c"Failed to execute\0A\00"
@str = [18 x i8] c"Starting artha...\00"
@str.5 = [20 x i8] c"Successfull fork...\00"
define i32 @main() {
%1 = alloca [2 x i8*]
%2 = load i32, i32* @SIGCHLD
%3 = load i32, i32* @SIG_IGN
%4 = tail call i32 @signal(i32 %2, i32 %3)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = icmp eq i64 %6, 0
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %12
%18 = bitcast [2 x i8*]* %1 to i8*
%19 = getelementptr inbounds [2 x i8*], [2 x i8*]* %1, i64 0, i64 0
%20 = load i8**, i8*** @environ
%21 = call i32 @execve(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8** %19, i8** %20)
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
ret i32 1
}