#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int exec_command(char *, char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ log_debug(char *, int);
int /*<<< orphan*/ log_error(char *, char const *, ...);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int run_task(const char *tache, const char *commande, char *logdir,
             int *resultat) {
  int err;
  char *ficconsole;
  FILE *fconsole = NULL;
  pid_t pid;
  ficconsole = malloc(sizeof(char) * (strlen(logdir) + strlen(tache) + 10));
  if (ficconsole == NULL) {
    log_error("Task %s could not allocate memory. Task aborted.", tache);
    return 1;
  }
  sprintf(ficconsole, "%s/%s_console", logdir, tache);
  fconsole = fopen(ficconsole, "w");
  if (fconsole == NULL) {
    log_error("Task %s could not create or modify the file %s. Task aborted",
              tache, ficconsole);
    return 2;
  }
  fclose(fconsole);
  pid = fork();
  if (pid == -1) {
    log_error("Fork failure : command \"%s\" not runned", commande);
    return 1;
  }
  if (pid == 0) {
    int err = exec_command(ficconsole, commande);
    free(ficconsole);
    log_debug("forked process exits with status %d", err);
    exit(err);
  } else {
    int wstatus;
    waitpid(pid, &wstatus, 0);
    if (!WIFEXITED(wstatus)) {
      *resultat = 1;
    } else {
      *resultat = WEXITSTATUS(wstatus);
      log_debug("parent process get status %d", *resultat);
    }
  }
  free(ficconsole);
  return 0;
}


x86 assembly (clang, -O0):
<run_task>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%eax
add    %ecx,%eax
add    $0xa,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1259 <run_task+0x79>
mov    -0x10(%rbp),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1050 <log_error@plt>
movl   $0x1,-0x4(%rbp)
jmp    138c <run_task+0x1ac>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdc5(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12b2 <run_task+0xd2>
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1050 <log_error@plt>
movl   $0x2,-0x4(%rbp)
jmp    138c <run_task+0x1ac>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    12ed <run_task+0x10d>
mov    -0x18(%rbp),%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1050 <log_error@plt>
movl   $0x1,-0x4(%rbp)
jmp    138c <run_task+0x1ac>
cmpl   $0x0,-0x44(%rbp)
jne    1327 <run_task+0x147>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <exec_command@plt>
mov    %eax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%esi
lea    0xd8b(%rip),%rdi
call   1070 <log_debug@plt>
mov    -0x48(%rbp),%edi
call   10f0 <exit@plt>
mov    -0x44(%rbp),%edi
lea    -0x4c(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    -0x4c(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1355 <run_task+0x175>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
jmp    1377 <run_task+0x197>
mov    -0x4c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    0xd57(%rip),%rdi
call   1070 <log_debug@plt>
jmp    137c <run_task+0x19c>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_task>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rdx,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0xa,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1298 <run_task+0xb8>
mov    %rax,%r12
lea    0xe0e(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    %r13,%rcx
call   10e0 <sprintf@plt>
lea    0xe07(%rip),%rsi
mov    %r12,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12a4 <run_task+0xc4>
mov    %rax,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     12bf <run_task+0xdf>
test   %eax,%eax
je     12f7 <run_task+0x117>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    0x4(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %eax,%eax
je     12d7 <run_task+0xf7>
mov    0x4(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,(%r14)
lea    0xe3a(%rip),%rdi
mov    %eax,%esi
call   1070 <log_debug@plt>
jmp    12de <run_task+0xfe>
lea    0xd61(%rip),%rdi
mov    %r13,%rsi
jmp    12c9 <run_task+0xe9>
lea    0xd96(%rip),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <log_error@plt>
mov    $0x2,%eax
jmp    12e8 <run_task+0x108>
lea    0xdb8(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <log_error@plt>
mov    $0x1,%eax
jmp    12e8 <run_task+0x108>
movl   $0x1,(%r14)
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <exec_command@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1030 <free@plt>
lea    0xd92(%rip),%rdi
mov    %ebx,%esi
call   1070 <log_debug@plt>
mov    %ebx,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"Task %s could not allocate memory. Task aborted.\00"
@.str.1 = [14 x i8] c"%s/%s_console\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [61 x i8] c"Task %s could not create or modify the file %s. Task aborted\00"
@.str.4 = [39 x i8] c"Fork failure : command \22%s\22 not runned\00"
@.str.5 = [36 x i8] c"forked process exits with status %d\00"
@.str.6 = [29 x i8] c"parent process get status %d\00"
define i32 @run_task(i8* %0, i8* %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32* %3, i32** %9
store i32* null, i32** %12
%16 = load i8*, i8** %8
%17 = call i32 @strlen(i8* %16)
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %17, %19
%21 = add nsw i32 %20, 10
%22 = sext i32 %21 to i64
%23 = mul i64 1, %22
%24 = trunc i64 %23 to i32
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %11
%26 = load i8*, i8** %11
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %4
%29 = load i8*, i8** %6
%30 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %29)
store i32 1, i32* %5
br label %85
31: ; preds = %4
%32 = load i8*, i8** %11
%33 = load i8*, i8** %8
%34 = load i8*, i8** %6
%35 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %34)
%36 = load i8*, i8** %11
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %12
%38 = load i32*, i32** %12
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %44
40: ; preds = %31
%41 = load i8*, i8** %6
%42 = load i8*, i8** %11
%43 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i8* %41, i8* %42)
store i32 2, i32* %5
br label %85
44: ; preds = %31
%45 = load i32*, i32** %12
%46 = call i32 @fclose(i32* %45)
%47 = call i32 (...) @fork()
store i32 %47, i32* %13
%48 = load i32, i32* %13
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i8*, i8** %7
%52 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %51)
store i32 1, i32* %5
br label %85
53: ; preds = %44
%54 = load i32, i32* %13
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %66
56: ; preds = %53
%57 = load i8*, i8** %11
%58 = load i8*, i8** %7
%59 = call i32 @exec_command(i8* %57, i8* %58)
store i32 %59, i32* %14
%60 = load i8*, i8** %11
%61 = call i32 @free(i8* %60)
%62 = load i32, i32* %14
%63 = call i32 @log_debug(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %14
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %53
%67 = load i32, i32* %13
%68 = call i32 @waitpid(i32 %67, i32* %15, i32 0)
%69 = load i32, i32* %15
%70 = call i32 @WIFEXITED(i32 %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %74, label %72
72: ; preds = %66
%73 = load i32*, i32** %9
store i32 1, i32* %73
br label %81
74: ; preds = %66
%75 = load i32, i32* %15
%76 = call i32 @WEXITSTATUS(i32 %75)
%77 = load i32*, i32** %9
store i32 %76, i32* %77
%78 = load i32*, i32** %9
%79 = load i32, i32* %78
%80 = call i32 @log_debug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %79)
br label %81
81: ; preds = %74, %72
br label %82
82: ; preds = %81
%83 = load i8*, i8** %11
%84 = call i32 @free(i8* %83)
store i32 0, i32* %5
br label %85
85: ; preds = %82, %50, %40, %28
%86 = load i32, i32* %5
ret i32 %86
}

LLVM IR (-O2):
@.str = [49 x i8] c"Task %s could not allocate memory. Task aborted.\00"
@.str.1 = [14 x i8] c"%s/%s_console\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [61 x i8] c"Task %s could not create or modify the file %s. Task aborted\00"
@.str.4 = [39 x i8] c"Fork failure : command \22%s\22 not runned\00"
@.str.5 = [36 x i8] c"forked process exits with status %d\00"
@.str.6 = [29 x i8] c"parent process get status %d\00"
define i32 @run_task(i8* %0, i8* %1, i8* %2, i32* nocapture writeonly %3) {
%5 = alloca i32
%6 = tail call i32 @strlen(i8* %2)
%7 = tail call i32 @strlen(i8* %0)
%8 = add i32 %6, 10
%9 = add i32 %8, %7
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %43
14: ; preds = %4
%15 = tail call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %0)
%16 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %10)
br label %43
20: ; preds = %14
%21 = tail call i32 @fclose(i32* %16)
%22 = tail call i32 (...) @fork()
switch i32 %22, label %30 [
i32 -1, label %23
i32 0, label %25
]
23: ; preds = %20
%24 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %43
25: ; preds = %20
%26 = tail call i32 @exec_command(i8* %10, i8* %1)
%27 = tail call i32 @free(i8* %10)
%28 = tail call i32 @log_debug(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%29 = tail call i32 @exit(i32 %26)
unreachable
30: ; preds = %20
%31 = bitcast i32* %5 to i8*
%32 = call i32 @waitpid(i32 %22, i32* %5, i32 0)
%33 = load i32, i32* %5
%34 = call i32 @WIFEXITED(i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 1, i32* %3
br label %41
37: ; preds = %30
%38 = load i32, i32* %5
%39 = call i32 @WEXITSTATUS(i32 %38)
store i32 %39, i32* %3
%40 = call i32 @log_debug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %37, %36
%42 = call i32 @free(i8* %10)
br label %43
43: ; preds = %41, %23, %18, %12
%44 = phi i32 [ 1, %12 ], [ 2, %18 ], [ 1, %23 ], [ 0, %41 ]
ret i32 %44
}