#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 */

/* Variables and functions */
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char const *,
                          char const *, int /*<<< orphan*/ *);
int fork();
char *getenv(char *);
int /*<<< orphan*/ restore_sigmask();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ wait(int *);
void hook_on_finish(int jobid, int errorlevel, const char *ofname,
                    const char *command) {
  char *onfinish;
  int pid;
  char sjobid[20];
  char serrorlevel[20];
  int status;
  onfinish = getenv("TS_ONFINISH");
  if (onfinish == NULL)
    return;
  pid = fork();
  switch (pid) {
  case 0:
    restore_sigmask();
    sprintf(sjobid, "%i", jobid);
    sprintf(serrorlevel, "%i", errorlevel);
    execlp(onfinish, onfinish, sjobid, serrorlevel, ofname, command, NULL);
  case -1:
    error("fork on finish");
  default:
    wait(&status);
  }
}


x86 assembly (clang, -O0):
<hook_on_finish>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    0xe73(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a6 <hook_on_finish+0x36>
jmp    123d <hook_on_finish+0xcd>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x68(%rbp)
sub    $0xffffffff,%eax
je     1228 <hook_on_finish+0xb8>
jmp    11c4 <hook_on_finish+0x54>
mov    -0x68(%rbp),%eax
test   %eax,%eax
jne    1234 <hook_on_finish+0xc4>
jmp    11d4 <hook_on_finish+0x64>
mov    $0x0,%al
call   1040 <restore_sigmask@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe23(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe10(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
xor    %eax,%eax
movq   $0x0,(%rsp)
call   1070 <execlp@plt>
lea    0xde0(%rip),%rdi
call   1050 <error@plt>
lea    -0x64(%rbp),%rdi
call   1080 <wait@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hook_on_finish>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
mov    %edi,%ebp
lea    0xe70(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1214 <hook_on_finish+0xa4>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11fe <hook_on_finish+0x8e>
test   %eax,%eax
jne    120a <hook_on_finish+0x9a>
xor    %eax,%eax
call   1040 <restore_sigmask@plt>
lea    0xe51(%rip),%r13
lea    0x10(%rsp),%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1060 <sprintf@plt>
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1060 <sprintf@plt>
movq   $0x0,(%rsp)
mov    %rbx,%rdi
mov    %rbx,%rsi
lea    0x10(%rsp),%rdx
mov    %rbp,%rcx
mov    %r15,%r8
mov    %r14,%r9
call   1070 <execlp@plt>
lea    0xe0a(%rip),%rdi
call   1050 <error@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"TS_ONFINISH\00"
@.str.1 = [3 x i8] c"%i\00"
@.str.2 = [15 x i8] c"fork on finish\00"
define void @hook_on_finish(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [20 x i8]
%12 = alloca [20 x i8]
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%14 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %4
br label %40
18: ; preds = %4
%19 = call i32 (...) @fork()
store i32 %19, i32* %10
%20 = load i32, i32* %10
switch i32 %20, label %38 [
i32 0, label %21
i32 -1, label %36
]
21: ; preds = %18
%22 = call i32 (...) @restore_sigmask()
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%24 = load i32, i32* %5
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%27 = load i32, i32* %6
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i8*, i8** %9
%30 = load i8*, i8** %9
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%33 = load i8*, i8** %7
%34 = load i8*, i8** %8
%35 = call i32 @execlp(i8* %29, i8* %30, i8* %31, i8* %32, i8* %33, i8* %34, i32* null)
br label %36
36: ; preds = %18, %21
%37 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %18, %36
%39 = call i32 @wait(i32* %13)
br label %40
40: ; preds = %17, %38
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"TS_ONFINISH\00"
@.str.1 = [3 x i8] c"%i\00"
@.str.2 = [15 x i8] c"fork on finish\00"
define void @hook_on_finish(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [20 x i8]
%6 = alloca [20 x i8]
%7 = alloca i32
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%10 = bitcast i32* %7 to i8*
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %24, label %13
13: ; preds = %4
%14 = tail call i32 (...) @fork()
switch i32 %14, label %22 [
i32 0, label %15
i32 -1, label %20
]
15: ; preds = %13
%16 = tail call i32 (...) @restore_sigmask()
%17 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%18 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%19 = call i32 @execlp(i8* %11, i8* %11, i8* %8, i8* %9, i8* %2, i8* %3, i32* null)
br label %20
20: ; preds = %13, %15
%21 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %13, %20
%23 = call i32 @wait(i32* %7)
br label %24
24: ; preds = %4, %22
ret void
}