int unexec(new_name, a_name, s_name, data_start, bss_start, entry_address)
char *new_name, *a_name, *s_name;
unsigned data_start, bss_start, entry_address;
{
  int new, s_out = -1, a_out = -1;
  if (a_name && (a_out = open(a_name, 0)) < 0) {
    perror(a_name);
    return -1;
  }
  if (s_name && (s_out = open(s_name, 0)) < 0) {
    perror(s_name);
    return -1;
  }
  if ((new = creat(new_name, 0666)) < 0) {
    perror(new_name);
    return -1;
  }
  if (make_hdr(new, a_out, s_out, data_start, bss_start, entry_address, a_name,
               s_name, new_name) < 0 ||
      copy_text_and_data(new) < 0 ||
      copy_sym(new, s_out >= 0 ? s_out : a_out, a_name, new_name) < 0) {
    close(new);
    unlink(new_name);
    return -1;
  }
  close(new);
  if (a_out >= 0)
    close(a_out);
  if (s_out >= 0)
    close(s_out);
  mark_x(new_name);
  return 0;
}


x86 assembly (clang, -O0):
<unexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0xffffffff,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f8 <unexec+0x68>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    11f8 <unexec+0x68>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <unexec+0x1cb>
cmpq   $0x0,-0x20(%rbp)
je     1233 <unexec+0xa3>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    1233 <unexec+0xa3>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <unexec+0x1cb>
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
mov    $0x0,%al
call   10a0 <creat@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    1266 <unexec+0xd6>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <unexec+0x1cb>
mov    -0x30(%rbp),%edi
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
mov    -0x18(%rbp),%r11
mov    -0x20(%rbp),%r10
mov    -0x10(%rbp),%rax
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
mov    $0x0,%al
call   1040 <make_hdr@plt>
cmp    $0x0,%eax
jl     12f6 <unexec+0x166>
mov    -0x30(%rbp),%edi
mov    $0x0,%al
call   1060 <copy_text_and_data@plt>
cmp    $0x0,%eax
jl     12f6 <unexec+0x166>
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     12d2 <unexec+0x142>
mov    -0x34(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    12d8 <unexec+0x148>
mov    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x0,%al
call   10b0 <copy_sym@plt>
cmp    $0x0,%eax
jge    1317 <unexec+0x187>
mov    -0x30(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <unexec+0x1cb>
mov    -0x30(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
cmpl   $0x0,-0x38(%rbp)
jl     1335 <unexec+0x1a5>
mov    -0x38(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
cmpl   $0x0,-0x34(%rbp)
jl     1349 <unexec+0x1b9>
mov    -0x34(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1050 <mark_x@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unexec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r14d
mov    %r8d,0x8(%rsp)
mov    %ecx,0x14(%rsp)
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbp
mov    $0xffffffff,%ebx
mov    $0xffffffff,%r12d
test   %rsi,%rsi
je     11da <unexec+0x4a>
mov    %r13,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
js     12cb <unexec+0x13b>
test   %r15,%r15
je     11f5 <unexec+0x65>
mov    %r15,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12d5 <unexec+0x145>
mov    %ebx,0x4(%rsp)
mov    %r14d,%ebx
mov    %rbp,%rdi
mov    $0x1b6,%esi
xor    %eax,%eax
call   10a0 <creat@plt>
test   %eax,%eax
js     12d0 <unexec+0x140>
mov    %eax,%r14d
sub    $0x8,%rsp
mov    %eax,%edi
mov    %r12d,%esi
mov    0xc(%rsp),%edx
mov    0x1c(%rsp),%ecx
mov    0x10(%rsp),%r8d
mov    %ebx,%r9d
xor    %eax,%eax
mov    %rbp,0x10(%rsp)
push   %rbp
push   %r15
push   %r13
call   1040 <make_hdr@plt>
add    $0x20,%rsp
test   %eax,%eax
js     12b3 <unexec+0x123>
mov    %r14d,%edi
xor    %eax,%eax
call   1060 <copy_text_and_data@plt>
test   %eax,%eax
js     12b3 <unexec+0x123>
mov    0x4(%rsp),%eax
test   %eax,%eax
mov    %r12d,%esi
cmovns %eax,%esi
mov    %r14d,%edi
mov    %r13,%rdx
mov    0x8(%rsp),%rcx
xor    %eax,%eax
call   10b0 <copy_sym@plt>
test   %eax,%eax
js     12b3 <unexec+0x123>
mov    %r14d,%edi
xor    %eax,%eax
call   1070 <close@plt>
test   %r12d,%r12d
js     1291 <unexec+0x101>
mov    %r12d,%edi
xor    %eax,%eax
call   1070 <close@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
mov    0x8(%rsp),%rbp
js     12a5 <unexec+0x115>
xor    %eax,%eax
call   1070 <close@plt>
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <mark_x@plt>
jmp    12e4 <unexec+0x154>
mov    %r14d,%edi
xor    %eax,%eax
call   1070 <close@plt>
mov    0x8(%rsp),%rdi
xor    %eax,%eax
call   1030 <unlink@plt>
jmp    12df <unexec+0x14f>
mov    %r13,%rdi
jmp    12d8 <unexec+0x148>
mov    %rbp,%rdi
jmp    12d8 <unexec+0x148>
mov    %r15,%rdi
xor    %eax,%eax
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @unexec(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 -1, i32* %15
store i32 -1, i32* %16
%17 = load i8*, i8** %9
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %26
19: ; preds = %6
%20 = load i8*, i8** %9
%21 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* %20, i32 0)
store i32 %21, i32* %16
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i8*, i8** %9
%25 = call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %24)
store i32 -1, i32* %7
br label %95
26: ; preds = %19, %6
%27 = load i8*, i8** %10
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = load i8*, i8** %10
%31 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* %30, i32 0)
store i32 %31, i32* %15
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i8*, i8** %10
%35 = call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %34)
store i32 -1, i32* %7
br label %95
36: ; preds = %29, %26
%37 = load i8*, i8** %8
%38 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @creat to i32 (i8*, i32, ...)*)(i8* %37, i32 438)
store i32 %38, i32* %14
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %8
%42 = call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %41)
store i32 -1, i32* %7
br label %95
43: ; preds = %36
%44 = load i32, i32* %14
%45 = load i32, i32* %16
%46 = load i32, i32* %15
%47 = load i32, i32* %11
%48 = load i32, i32* %12
%49 = load i32, i32* %13
%50 = load i8*, i8** %9
%51 = load i8*, i8** %10
%52 = load i8*, i8** %8
%53 = call i32 (i32, i32, i32, i32, i32, i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @make_hdr to i32 (i32, i32, i32, i32, i32, i32, i8*, i8*, i8*, ...)*)(i32 %44, i32 %45, i32 %46, i32 %47, i32 %48, i32 %49, i8* %50, i8* %51, i8* %52)
%54 = icmp slt i32 %53, 0
br i1 %54, label %73, label %55
55: ; preds = %43
%56 = load i32, i32* %14
%57 = call i32 (i32, ...) bitcast (i32 (...)* @copy_text_and_data to i32 (i32, ...)*)(i32 %56)
%58 = icmp slt i32 %57, 0
br i1 %58, label %73, label %59
59: ; preds = %55
%60 = load i32, i32* %14
%61 = load i32, i32* %15
%62 = icmp sge i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = load i32, i32* %15
br label %67
65: ; preds = %59
%66 = load i32, i32* %16
br label %67
67: ; preds = %65, %63
%68 = phi i32 [ %64, %63 ], [ %66, %65 ]
%69 = load i8*, i8** %9
%70 = load i8*, i8** %8
%71 = call i32 (i32, i32, i8*, i8*, ...) bitcast (i32 (...)* @copy_sym to i32 (i32, i32, i8*, i8*, ...)*)(i32 %60, i32 %68, i8* %69, i8* %70)
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %78
73: ; preds = %67, %55, %43
%74 = load i32, i32* %14
%75 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %74)
%76 = load i8*, i8** %8
%77 = call i32 (i8*, ...) bitcast (i32 (...)* @unlink to i32 (i8*, ...)*)(i8* %76)
store i32 -1, i32* %7
br label %95
78: ; preds = %67
%79 = load i32, i32* %14
%80 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %79)
%81 = load i32, i32* %16
%82 = icmp sge i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %78
%84 = load i32, i32* %16
%85 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %84)
br label %86
86: ; preds = %83, %78
%87 = load i32, i32* %15
%88 = icmp sge i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = load i32, i32* %15
%91 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %90)
br label %92
92: ; preds = %89, %86
%93 = load i8*, i8** %8
%94 = call i32 (i8*, ...) bitcast (i32 (...)* @mark_x to i32 (i8*, ...)*)(i8* %93)
store i32 0, i32* %7
br label %95
95: ; preds = %92, %73, %40, %33, %23
%96 = load i32, i32* %7
ret i32 %96
}

LLVM IR (-O2):
define i32 @unexec(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = icmp eq i8* %1, null
br i1 %7, label %13, label %8
8: ; preds = %6
%9 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* %1, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %1)
br label %51
13: ; preds = %8, %6
%14 = phi i32 [ %9, %8 ], [ -1, %6 ]
%15 = icmp eq i8* %2, null
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* %2, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %2)
br label %51
21: ; preds = %16, %13
%22 = phi i32 [ %17, %16 ], [ -1, %13 ]
%23 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @creat to i32 (i8*, i32, ...)*)(i8* %0, i32 438)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %0)
br label %51
27: ; preds = %21
%28 = tail call i32 (i32, i32, i32, i32, i32, i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @make_hdr to i32 (i32, i32, i32, i32, i32, i32, i8*, i8*, i8*, ...)*)(i32 %23, i32 %14, i32 %22, i32 %3, i32 %4, i32 %5, i8* %1, i8* %2, i8* %0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %27
%31 = tail call i32 (i32, ...) bitcast (i32 (...)* @copy_text_and_data to i32 (i32, ...)*)(i32 %23)
%32 = icmp slt i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = icmp sgt i32 %22, -1
%35 = select i1 %34, i32 %22, i32 %14
%36 = tail call i32 (i32, i32, i8*, i8*, ...) bitcast (i32 (...)* @copy_sym to i32 (i32, i32, i8*, i8*, ...)*)(i32 %23, i32 %35, i8* %1, i8* %0)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33, %30, %27
%39 = tail call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %23)
%40 = tail call i32 (i8*, ...) bitcast (i32 (...)* @unlink to i32 (i8*, ...)*)(i8* %0)
br label %51
41: ; preds = %33
%42 = tail call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %23)
%43 = icmp sgt i32 %14, -1
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = tail call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %14)
br label %46
46: ; preds = %44, %41
br i1 %34, label %47, label %49
47: ; preds = %46
%48 = tail call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %22)
br label %49
49: ; preds = %47, %46
%50 = tail call i32 (i8*, ...) bitcast (i32 (...)* @mark_x to i32 (i8*, ...)*)(i8* %0)
br label %51
51: ; preds = %49, %38, %25, %19, %11
%52 = phi i32 [ -1, %11 ], [ -1, %19 ], [ -1, %25 ], [ -1, %38 ], [ 0, %49 ]
ret i32 %52
}