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

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ perror(char *);
int wait(int *);
void sendmail(FILE *infile, char **addr, int start) {
  int status;
  int pid;
  if (start < 2)
    abort();
  addr[--start] = "-oi";
  addr[--start] = "sendmail";
  do {
    pid = fork();
  } while (pid == -1 && errno == EAGAIN);
  if (pid == -1) {
    perror("fork");
    return;
  }
  if (pid != 0) {
    while (pid != wait(&status))
      ;
    return;
  }
  dup2(fileno(infile), 0);
  fclose(infile);
  execv("/usr/lib/sendmail", addr + start);
  execv("/usr/sbin/sendmail", addr + start);
  perror("execv");
  _exit(1);
}


x86 assembly (clang, -O0):
<sendmail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jge    11b4 <sendmail+0x24>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
movslq %ecx,%rcx
lea    0xe35(%rip),%rdx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
movslq %ecx,%rcx
lea    0xe2c(%rip),%rdx
mov    %rdx,(%rax,%rcx,8)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    121d <sendmail+0x8d>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11ea <sendmail+0x5a>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1243 <sendmail+0xb3>
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
jmp    12df <sendmail+0x14f>
cmpl   $0x0,-0x1c(%rbp)
je     1278 <sendmail+0xe8>
jmp    1252 <sendmail+0xc2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x18(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
je     1273 <sendmail+0xe3>
jmp    1252 <sendmail+0xc2>
jmp    12df <sendmail+0x14f>
mov    -0x8(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
lea    0xd60(%rip),%rdi
call   10a0 <execv@plt>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
lea    0xd57(%rip),%rdi
call   10a0 <execv@plt>
lea    0xd5e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendmail>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edx
jle    1241 <sendmail+0xb1>
mov    %rdi,%r14
lea    -0x1(%rdx),%eax
lea    0xe4e(%rip),%rcx
mov    %rcx,(%rsi,%rax,8)
add    $0xfffffffe,%edx
lea    (%rsi,%rdx,8),%r15
lea    0xe4e(%rip),%rax
mov    %rax,(%rsi,%rdx,8)
mov    0x2e11(%rip),%r12
mov    0x2dfa(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11f7 <sendmail+0x67>
mov    (%rbx),%rax
cmp    %rax,(%r12)
je     11e0 <sendmail+0x50>
cmp    $0xffffffff,%ebp
je     1229 <sendmail+0x99>
test   %ebp,%ebp
je     1248 <sendmail+0xb8>
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1090 <wait@plt>
cmp    %eax,%ebp
jne    1210 <sendmail+0x80>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd4(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %r14,%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xda1(%rip),%rdi
mov    %r15,%rsi
call   10a0 <execv@plt>
lea    0xda4(%rip),%rdi
mov    %r15,%rsi
call   10a0 <execv@plt>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"-oi\00"
@.str.1 = [9 x i8] c"sendmail\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [18 x i8] c"/usr/lib/sendmail\00"
@.str.4 = [19 x i8] c"/usr/sbin/sendmail\00"
@.str.5 = [6 x i8] c"execv\00"
define void @sendmail(i32* %0, i8** %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 (...) @abort()
unreachable
13: ; preds = %3
%14 = load i8**, i8*** %5
%15 = load i32, i32* %6
%16 = add nsw i32 %15, -1
store i32 %16, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %14, i64 %17
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %18
%19 = load i8**, i8*** %5
%20 = load i32, i32* %6
%21 = add nsw i32 %20, -1
store i32 %21, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %19, i64 %22
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %23
br label %24
24: ; preds = %33, %13
%25 = call i32 (...) @fork()
store i32 %25, i32* %8
br label %26
26: ; preds = %24
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp eq i64 %30, %31
br label %33
33: ; preds = %29, %26
%34 = phi i1 [ false, %26 ], [ %32, %29 ]
br i1 %34, label %24, label %35
35: ; preds = %33
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %68
40: ; preds = %35
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %40
br label %44
44: ; preds = %48, %43
%45 = load i32, i32* %8
%46 = call i32 @wait(i32* %7)
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
br label %44
49: ; preds = %44
br label %68
50: ; preds = %40
%51 = load i32*, i32** %4
%52 = call i32 @fileno(i32* %51)
%53 = call i32 @dup2(i32 %52, i32 0)
%54 = load i32*, i32** %4
%55 = call i32 @fclose(i32* %54)
%56 = load i8**, i8*** %5
%57 = load i32, i32* %6
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = call i32 @execv(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8** %59)
%61 = load i8**, i8*** %5
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
%65 = call i32 @execv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8** %64)
%66 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%67 = call i32 @_exit(i32 1)
unreachable
68: ; preds = %49, %38
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"-oi\00"
@.str.1 = [9 x i8] c"sendmail\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [18 x i8] c"/usr/lib/sendmail\00"
@.str.4 = [19 x i8] c"/usr/sbin/sendmail\00"
@.str.5 = [6 x i8] c"execv\00"
define void @sendmail(i32* nocapture %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp slt i32 %2, 2
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %3
%10 = add nsw i32 %2, -1
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8*, i8** %1, i64 %11
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %12
%13 = add nsw i32 %2, -2
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8*, i8** %1, i64 %14
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %15
br label %16
16: ; preds = %16, %9
%17 = tail call i32 (...) @fork()
%18 = icmp eq i32 %17, -1
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %19, %20
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %16, label %23
23: ; preds = %16
br i1 %18, label %24, label %26
24: ; preds = %23
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %39
26: ; preds = %23
%27 = icmp eq i32 %17, 0
br i1 %27, label %31, label %28
28: ; preds = %26, %28
%29 = call i32 @wait(i32* %4)
%30 = icmp eq i32 %17, %29
br i1 %30, label %39, label %28
31: ; preds = %26
%32 = tail call i32 @fileno(i32* %0)
%33 = tail call i32 @dup2(i32 %32, i32 0)
%34 = tail call i32 @fclose(i32* %0)
%35 = tail call i32 @execv(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8** %15)
%36 = tail call i32 @execv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8** %15)
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%38 = tail call i32 @_exit(i32 1)
unreachable
39: ; preds = %28, %24
ret void
}