#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*/ close(int);
int /*<<< orphan*/ copy_output(int, char const *);
char *getenv(char *);
int run_sendmail(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write_header(int, char *, char const *, int, int);
void send_mail(int jobid, int errorlevel, const char *ofname,
               const char *command) {
  char to[101];
  char *user;
  char *env_to;
  int write_fd;
  int status;
  env_to = getenv("TS_MAILTO");
  if (env_to == NULL || strlen(env_to) > 100) {
    user = getenv("USER");
    if (user == NULL)
      user = "nobody";
    strcpy(to, user);
  } else
    strcpy(to, env_to);
  write_fd = run_sendmail(to);
  write_header(write_fd, to, command, jobid, errorlevel);
  copy_output(write_fd, ofname);
  close(write_fd);
  wait(&status);
}


x86 assembly (clang, -O0):
<send_mail>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
je     11cf <send_mail+0x4f>
mov    -0x90(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x64,%eax
jle    1213 <send_mail+0x93>
lea    0xe34(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    11fe <send_mail+0x7e>
lea    0xe18(%rip),%rax
mov    %rax,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x88(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1223 <send_mail+0xa3>
lea    -0x80(%rbp),%rdi
mov    -0x90(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x80(%rbp),%rdi
call   1080 <run_sendmail@plt>
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x4(%rbp),%ecx
mov    -0x8(%rbp),%r8d
call   1090 <write_header@plt>
mov    -0x94(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <copy_output@plt>
mov    -0x94(%rbp),%edi
call   1070 <close@plt>
lea    -0x98(%rbp),%rdi
call   10a0 <wait@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_mail>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %esi,%r12d
mov    %edi,%r13d
lea    0xe5f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11bb <send_mail+0x3b>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
cmp    $0x65,%eax
jl     11dc <send_mail+0x5c>
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe3e(%rip),%rsi
cmovne %rax,%rsi
lea    0x10(%rsp),%rdi
jmp    11e4 <send_mail+0x64>
lea    0x10(%rsp),%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <run_sendmail@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r13d,%ecx
mov    %r12d,%r8d
call   1090 <write_header@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <copy_output@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"TS_MAILTO\00"
@.str.1 = [5 x i8] c"USER\00"
@.str.2 = [7 x i8] c"nobody\00"
define void @send_mail(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [101 x i8]
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%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 ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %4
%18 = load i8*, i8** %11
%19 = call i32 @strlen(i8* %18)
%20 = icmp sgt i32 %19, 100
br i1 %20, label %21, label %30
21: ; preds = %17, %4
%22 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %22, i8** %10
%23 = load i8*, i8** %10
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %10
br label %26
26: ; preds = %25, %21
%27 = getelementptr inbounds [101 x i8], [101 x i8]* %9, i64 0, i64 0
%28 = load i8*, i8** %10
%29 = call i32 @strcpy(i8* %27, i8* %28)
br label %34
30: ; preds = %17
%31 = getelementptr inbounds [101 x i8], [101 x i8]* %9, i64 0, i64 0
%32 = load i8*, i8** %11
%33 = call i32 @strcpy(i8* %31, i8* %32)
br label %34
34: ; preds = %30, %26
%35 = getelementptr inbounds [101 x i8], [101 x i8]* %9, i64 0, i64 0
%36 = call i32 @run_sendmail(i8* %35)
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = getelementptr inbounds [101 x i8], [101 x i8]* %9, i64 0, i64 0
%39 = load i8*, i8** %8
%40 = load i32, i32* %5
%41 = load i32, i32* %6
%42 = call i32 @write_header(i32 %37, i8* %38, i8* %39, i32 %40, i32 %41)
%43 = load i32, i32* %12
%44 = load i8*, i8** %7
%45 = call i32 @copy_output(i32 %43, i8* %44)
%46 = load i32, i32* %12
%47 = call i32 @close(i32 %46)
%48 = call i32 @wait(i32* %13)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"TS_MAILTO\00"
@.str.1 = [5 x i8] c"USER\00"
@.str.2 = [7 x i8] c"nobody\00"
define void @send_mail(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [101 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [101 x i8], [101 x i8]* %5, i64 0, i64 0
%8 = bitcast i32* %6 to i8*
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %4
%12 = tail call i32 @strlen(i8* %9)
%13 = icmp sgt i32 %12, 100
br i1 %13, label %14, label %19
14: ; preds = %11, %4
%15 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i8* %15, null
%17 = select i1 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %15
%18 = call i32 @strcpy(i8* %7, i8* %17)
br label %21
19: ; preds = %11
%20 = call i32 @strcpy(i8* %7, i8* %9)
br label %21
21: ; preds = %19, %14
%22 = call i32 @run_sendmail(i8* %7)
%23 = call i32 @write_header(i32 %22, i8* %7, i8* %3, i32 %0, i32 %1)
%24 = call i32 @copy_output(i32 %22, i8* %2)
%25 = call i32 @close(i32 %22)
%26 = call i32 @wait(i32* %6)
ret void
}