#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;

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ _PATH_SENDMAIL;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err(char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execl(int /*<<< orphan*/, int /*<<< orphan*/, char *, char *,
                         char *);
int fork();
int wait(int *);
int run_sendmail(int ifd) {
  pid_t child, pid;
  int status;
  switch (child = fork()) {
  case -1:
    err("*fork");
    return (1);
  case 0:
    dup2(ifd, 0);
    execl(_PATH_SENDMAIL, _PATH_SENDMAIL, "-odq", "-t", (char *)NULL);
    err("*exec: %s", _PATH_SENDMAIL);
    _exit(1);
  default:
    while ((pid = wait(&status)) != child) {
      if (pid == -1 && errno != EINTR) {
        err("*wait");
        return (1);
      }
    }
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
      err("sendmail failed");
      return (1);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<run_sendmail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11b8 <run_sendmail+0x38>
jmp    11a8 <run_sendmail+0x28>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11d2 <run_sendmail+0x52>
jmp    1227 <run_sendmail+0xa7>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <err@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <run_sendmail+0x14a>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe11(%rip),%rdx
lea    0xe0f(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <err@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
jmp    122c <run_sendmail+0xac>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x10(%rbp)
cmp    -0xc(%rbp),%eax
je     1282 <run_sendmail+0x102>
cmpl   $0xffffffff,-0x10(%rbp)
jne    127d <run_sendmail+0xfd>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
je     127d <run_sendmail+0xfd>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <err@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <run_sendmail+0x14a>
jmp    122c <run_sendmail+0xac>
mov    -0x14(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12a4 <run_sendmail+0x124>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     12be <run_sendmail+0x13e>
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1050 <err@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <run_sendmail+0x14a>
jmp    12c3 <run_sendmail+0x143>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_sendmail>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11d0 <run_sendmail+0x50>
mov    %eax,%ebx
test   %eax,%eax
je     1236 <run_sendmail+0xb6>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
cmp    %ebx,%eax
jne    11ee <run_sendmail+0x6e>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
je     11c7 <run_sendmail+0x47>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     1232 <run_sendmail+0xb2>
lea    0xe50(%rip),%rdi
jmp    11d7 <run_sendmail+0x57>
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1050 <err@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de3(%rip),%r15
mov    0x2dd4(%rip),%rbp
lea    0x4(%rsp),%r14
jmp    121c <run_sendmail+0x9c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1090 <wait@plt>
cmp    %ebx,%eax
je     11ad <run_sendmail+0x2d>
cmp    $0xffffffff,%eax
jne    1210 <run_sendmail+0x90>
mov    (%r15),%eax
cmp    0x0(%rbp),%eax
je     1210 <run_sendmail+0x90>
lea    0xde8(%rip),%rdi
jmp    11d7 <run_sendmail+0x57>
xor    %eax,%eax
jmp    11e3 <run_sendmail+0x63>
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x2da2(%rip),%rbx
mov    (%rbx),%edi
lea    0xdb7(%rip),%rdx
lea    0xdb5(%rip),%rcx
mov    %edi,%esi
xor    %r8d,%r8d
call   1080 <execl@plt>
mov    (%rbx),%esi
lea    0xda5(%rip),%rdi
xor    %eax,%eax
call   1050 <err@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"*fork\00"
@_PATH_SENDMAIL = global i32 0
@.str.1 = [5 x i8] c"-odq\00"
@.str.2 = [3 x i8] c"-t\00"
@.str.3 = [10 x i8] c"*exec: %s\00"
@errno = global i32 0
@EINTR = global i32 0
@.str.4 = [6 x i8] c"*wait\00"
@.str.5 = [16 x i8] c"sendmail failed\00"
define i32 @run_sendmail(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
switch i32 %7, label %19 [
i32 -1, label %8
i32 0, label %10
]
8: ; preds = %1
%9 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %46
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i32 @dup2(i32 %11, i32 0)
%13 = load i32, i32* @_PATH_SENDMAIL
%14 = load i32, i32* @_PATH_SENDMAIL
%15 = call i32 @execl(i32 %13, i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* null)
%16 = load i32, i32* @_PATH_SENDMAIL
%17 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 @_exit(i32 1)
unreachable
19: ; preds = %1
br label %20
20: ; preds = %33, %19
%21 = call i32 @wait(i32* %6)
store i32 %21, i32* %5
%22 = load i32, i32* %4
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %34
24: ; preds = %20
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = load i32, i32* @EINTR
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %2
br label %46
33: ; preds = %27, %24
br label %20
34: ; preds = %20
%35 = load i32, i32* %6
%36 = call i32 @WIFEXITED(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* %6
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38, %34
%43 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %2
br label %46
44: ; preds = %38
br label %45
45: ; preds = %44
store i32 0, i32* %2
br label %46
46: ; preds = %45, %42, %31, %8
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@.str = [6 x i8] c"*fork\00"
@_PATH_SENDMAIL = global i32 0
@.str.1 = [5 x i8] c"-odq\00"
@.str.2 = [3 x i8] c"-t\00"
@.str.3 = [10 x i8] c"*exec: %s\00"
@errno = global i32 0
@EINTR = global i32 0
@.str.4 = [6 x i8] c"*wait\00"
@.str.5 = [16 x i8] c"sendmail failed\00"
define i32 @run_sendmail(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @fork()
switch i32 %4, label %5 [
i32 -1, label %8
i32 0, label %10
]
5: ; preds = %1
%6 = call i32 @wait(i32* %2)
%7 = icmp eq i32 %6, %4
br i1 %7, label %29, label %17
8: ; preds = %1
%9 = tail call i32 (i8*, ...) @err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %39
10: ; preds = %1
%11 = tail call i32 @dup2(i32 %0, i32 0)
%12 = load i32, i32* @_PATH_SENDMAIL
%13 = tail call i32 @execl(i32 %12, i32 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* null)
%14 = load i32, i32* @_PATH_SENDMAIL
%15 = tail call i32 (i8*, ...) @err(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i32 @_exit(i32 1)
unreachable
17: ; preds = %5, %26
%18 = phi i32 [ %27, %26 ], [ %6, %5 ]
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = load i32, i32* @EINTR
%23 = icmp eq i32 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %39
26: ; preds = %20, %17
%27 = call i32 @wait(i32* %2)
%28 = icmp eq i32 %27, %4
br i1 %28, label %29, label %17
29: ; preds = %26, %5
%30 = load i32, i32* %2
%31 = call i32 @WIFEXITED(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i32, i32* %2
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33, %29
%38 = call i32 (i8*, ...) @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %39
39: ; preds = %33, %37, %24, %8
%40 = phi i32 [ 1, %24 ], [ 1, %37 ], [ 1, %8 ], [ 0, %33 ]
ret i32 %40
}