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) {
    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     11dc <unexec+0x5c>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11dc <unexec+0x5c>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <unexec+0x135>
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
mov    $0x0,%al
call   1090 <creat@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    120f <unexec+0x8f>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <unexec+0x135>
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     126f <unexec+0xef>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    $0x0,%al
call   1050 <copy_text_and_data@plt>
cmp    $0x0,%eax
jl     126f <unexec+0xef>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x0,%al
call   10a0 <copy_sym@plt>
cmp    $0x0,%eax
jge    1285 <unexec+0x105>
mov    -0x28(%rbp),%edi
mov    $0x0,%al
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <unexec+0x135>
mov    -0x28(%rbp),%edi
mov    $0x0,%al
call   1060 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
jl     12a3 <unexec+0x123>
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,%r13d
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%r12
test   %rsi,%rsi
je     11bf <unexec+0x3f>
mov    %rdi,%rbx
mov    %r12,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <open@plt>
mov    %rbx,%rdi
mov    %eax,%ebx
test   %eax,%eax
jns    11c4 <unexec+0x44>
mov    %r12,%rdi
jmp    1269 <unexec+0xe9>
mov    $0xffffffff,%ebx
mov    %r13d,0xc(%rsp)
mov    %rdi,%r13
mov    $0x1b6,%esi
xor    %eax,%eax
call   1090 <creat@plt>
test   %eax,%eax
js     1266 <unexec+0xe6>
mov    %eax,%ebp
mov    %r13,0x10(%rsp)
mov    %r13,(%rsp)
mov    %eax,%edi
mov    %ebx,%r13d
mov    %ebx,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
mov    0xc(%rsp),%r8d
mov    %r12,%r9
xor    %eax,%eax
call   1030 <make_hdr@plt>
test   %eax,%eax
js     125b <unexec+0xdb>
mov    %ebp,%edi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <copy_text_and_data@plt>
test   %eax,%eax
js     125b <unexec+0xdb>
mov    %ebp,%edi
mov    %r13d,%esi
mov    %r12,%rdx
mov    0x10(%rsp),%rcx
xor    %eax,%eax
call   10a0 <copy_sym@plt>
test   %eax,%eax
js     125b <unexec+0xdb>
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %eax,%eax
call   1060 <close@plt>
test   %r13d,%r13d
js     124d <unexec+0xcd>
mov    %r13d,%edi
xor    %eax,%eax
call   1060 <close@plt>
mov    0x10(%rsp),%rdi
xor    %eax,%eax
call   1040 <mark_x@plt>
jmp    1275 <unexec+0xf5>
mov    %ebp,%edi
xor    %eax,%eax
call   1060 <close@plt>
jmp    1270 <unexec+0xf0>
mov    %r13,%rdi
xor    %eax,%eax
call   1080 <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 %66
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 %66
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 %52, 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 %52, 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 %52, label %55
52: ; preds = %45, %40, %30
%53 = load i32, i32* %12
%54 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %53)
store i32 -1, i32* %6
br label %66
55: ; preds = %45
%56 = load i32, i32* %12
%57 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %56)
%58 = load i32, i32* %13
%59 = icmp sge i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = load i32, i32* %13
%62 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %61)
br label %63
63: ; preds = %60, %55
%64 = load i8*, i8** %7
%65 = call i32 (i8*, ...) bitcast (i32 (...)* @mark_x to i32 (i8*, ...)*)(i8* %64)
store i32 0, i32* %6
br label %66
66: ; preds = %63, %52, %27, %20
%67 = load i32, i32* %6
ret i32 %67
}

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 %36
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 %36
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 %27, 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 %27, 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 %27, label %29
27: ; preds = %24, %21, %18
%28 = tail call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %14)
br label %36
29: ; preds = %24
%30 = tail call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %14)
%31 = icmp sgt i32 %13, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %13)
br label %34
34: ; preds = %32, %29
%35 = tail call i32 (i8*, ...) bitcast (i32 (...)* @mark_x to i32 (i8*, ...)*)(i8* %0)
br label %36
36: ; preds = %34, %27, %16, %10
%37 = phi i32 [ -1, %10 ], [ -1, %16 ], [ -1, %27 ], [ 0, %34 ]
ret i32 %37
}