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


x86 assembly (clang, -O0):
<unexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ec <unexec+0x5c>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11ec <unexec+0x5c>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <unexec+0x14f>
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
mov    $0x0,%al
call   10a0 <creat@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    121f <unexec+0x8f>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <unexec+0x14f>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x18(%rbp),%r9
mov    -0x10(%rbp),%rax
mov    %rax,(%rsp)
mov    $0x0,%al
call   1030 <make_hdr@plt>
cmp    $0x0,%eax
jl     1299 <unexec+0x109>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    $0x0,%al
call   1050 <copy_text_and_data@plt>
cmp    $0x0,%eax
jl     1299 <unexec+0x109>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x0,%al
call   10b0 <copy_sym@plt>
cmp    $0x0,%eax
jl     1299 <unexec+0x109>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1070 <adjust_lnnoptrs@plt>
cmp    $0x0,%eax
jge    12af <unexec+0x11f>
mov    -0x28(%rbp),%edi
mov    $0x0,%al
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <unexec+0x14f>
mov    -0x28(%rbp),%edi
mov    $0x0,%al
call   1060 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
jl     12cd <unexec+0x13d>
mov    -0x2c(%rbp),%edi
mov    $0x0,%al
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1040 <mark_x@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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    %r8d,%r14d
mov    %ecx,%r13d
mov    %edx,%r15d
mov    %rsi,%r12
test   %rsi,%rsi
je     11cf <unexec+0x3f>
mov    %rdi,%rbx
mov    %r12,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %rbx,%rdi
mov    %eax,%ebx
test   %eax,%eax
jns    11d4 <unexec+0x44>
mov    %r12,%rdi
jmp    128d <unexec+0xfd>
mov    $0xffffffff,%ebx
mov    %r13d,0x14(%rsp)
mov    %rdi,%r13
mov    $0x1b6,%esi
xor    %eax,%eax
call   10a0 <creat@plt>
test   %eax,%eax
js     128a <unexec+0xfa>
mov    %eax,%ebp
mov    %r13,0x8(%rsp)
mov    %r13,(%rsp)
mov    %eax,%edi
mov    %ebx,%r13d
mov    %ebx,%esi
mov    %r15d,%edx
mov    0x14(%rsp),%ecx
mov    %r14d,%r8d
mov    %r12,%r9
xor    %eax,%eax
call   1030 <make_hdr@plt>
test   %eax,%eax
js     127f <unexec+0xef>
mov    %ebp,%edi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <copy_text_and_data@plt>
test   %eax,%eax
js     127f <unexec+0xef>
mov    %ebp,%edi
mov    %r13d,%esi
mov    %r12,%rdx
mov    0x8(%rsp),%rcx
xor    %eax,%eax
call   10b0 <copy_sym@plt>
test   %eax,%eax
js     127f <unexec+0xef>
mov    %ebp,%edi
mov    %r13d,%esi
mov    0x8(%rsp),%rdx
xor    %eax,%eax
call   1070 <adjust_lnnoptrs@plt>
test   %eax,%eax
js     127f <unexec+0xef>
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %eax,%eax
call   1060 <close@plt>
test   %r13d,%r13d
js     1271 <unexec+0xe1>
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <close@plt>
mov    0x8(%rsp),%rdi
xor    %eax,%eax
call   1040 <mark_x@plt>
jmp    1299 <unexec+0x109>
mov    %ebp,%edi
xor    %eax,%eax
call   1060 <close@plt>
jmp    1294 <unexec+0x104>
mov    %r13,%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, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 -1, i32* %13
%14 = load i8*, i8** %8
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %23
16: ; preds = %5
%17 = load i8*, i8** %8
%18 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* %17, i32 0)
store i32 %18, i32* %13
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i8*, i8** %8
%22 = call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %21)
store i32 -1, i32* %6
br label %72
23: ; preds = %16, %5
%24 = load i8*, i8** %7
%25 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @creat to i32 (i8*, i32, ...)*)(i8* %24, i32 438)
store i32 %25, i32* %12
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %7
%29 = call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* %28)
store i32 -1, i32* %6
br label %72
30: ; preds = %23
%31 = load i32, i32* %12
%32 = load i32, i32* %13
%33 = load i32, i32* %9
%34 = load i32, i32* %10
%35 = load i32, i32* %11
%36 = load i8*, i8** %8
%37 = load i8*, i8** %7
%38 = call i32 (i32, i32, i32, i32, i32, i8*, i8*, ...) bitcast (i32 (...)* @make_hdr to i32 (i32, i32, i32, i32, i32, i8*, i8*, ...)*)(i32 %31, i32 %32, i32 %33, i32 %34, i32 %35, i8* %36, i8* %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %58, label %40
40: ; preds = %30
%41 = load i32, i32* %12
%42 = load i32, i32* %13
%43 = call i32 (i32, i32, ...) bitcast (i32 (...)* @copy_text_and_data to i32 (i32, i32, ...)*)(i32 %41, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %58, label %45
45: ; preds = %40
%46 = load i32, i32* %12
%47 = load i32, i32* %13
%48 = load i8*, i8** %8
%49 = load i8*, i8** %7
%50 = call i32 (i32, i32, i8*, i8*, ...) bitcast (i32 (...)* @copy_sym to i32 (i32, i32, i8*, i8*, ...)*)(i32 %46, i32 %47, i8* %48, i8* %49)
%51 = icmp slt i32 %50, 0
br i1 %51, label %58, label %52
52: ; preds = %45
%53 = load i32, i32* %12
%54 = load i32, i32* %13
%55 = load i8*, i8** %7
%56 = call i32 (i32, i32, i8*, ...) bitcast (i32 (...)* @adjust_lnnoptrs to i32 (i32, i32, i8*, ...)*)(i32 %53, i32 %54, i8* %55)
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %52, %45, %40, %30
%59 = load i32, i32* %12
%60 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %59)
store i32 -1, i32* %6
br label %72
61: ; preds = %52
%62 = load i32, i32* %12
%63 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %62)
%64 = load i32, i32* %13
%65 = icmp sge i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i32, i32* %13
%68 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %67)
br label %69
69: ; preds = %66, %61
%70 = load i8*, i8** %7
%71 = call i32 (i8*, ...) bitcast (i32 (...)* @mark_x to i32 (i8*, ...)*)(i8* %70)
store i32 0, i32* %6
br label %72
72: ; preds = %69, %58, %27, %20
%73 = load i32, i32* %6
ret i32 %73
}

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