read_header(fd, sepid, text_bytes, data_bytes, bss_bytes, file_name) int fd,
    *sepid;
unsigned *text_bytes, *data_bytes, *bss_bytes;
char *file_name;
{
  long head[12];
  unsigned short hd[4];
  int n, header_len;
  if ((n = read(fd, hd, 8)) != 8)
    pexit("file header too short: ", file_name);
  header_len = hd[2];
  if (header_len != 32 && header_len != 48)
    pexit("bad header length. File: ", file_name);
  *sepid = hd[1] & 0x20;
  if ((n = read(fd, head, header_len - 8)) != header_len - 8)
    pexit("header too short: ", file_name);
  *text_bytes = (unsigned)head[0];
  *data_bytes = (unsigned)head[1];
  *bss_bytes = (unsigned)head[2];
}


x86 assembly (clang, -O0):
<read_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x9c(%rbp)
cmp    $0x8,%eax
je     1179 <read_header+0x59>
mov    -0x30(%rbp),%rsi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1040 <pexit@plt>
movzwl -0x94(%rbp),%eax
mov    %eax,-0xa0(%rbp)
cmpl   $0x20,-0xa0(%rbp)
je     11b2 <read_header+0x92>
cmpl   $0x30,-0xa0(%rbp)
je     11b2 <read_header+0x92>
mov    -0x30(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <pexit@plt>
movzwl -0x96(%rbp),%ecx
and    $0x20,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0xa0(%rbp),%edx
sub    $0x8,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x9c(%rbp)
mov    -0xa0(%rbp),%ecx
sub    $0x8,%ecx
cmp    %ecx,%eax
je     1205 <read_header+0xe5>
mov    -0x30(%rbp),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <pexit@plt>
mov    -0x90(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x88(%rbp),%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x80(%rbp),%rax
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_header>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %r9,%r13
mov    %r8,0x8(%rsp)
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x8,%edx
xor    %eax,%eax
call   1030 <read@plt>
cmp    $0x8,%eax
je     1166 <read_header+0x46>
lea    0xea4(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <pexit@plt>
movzwl 0x4(%rsp),%r14d
mov    %r14d,%eax
and    $0xffffffef,%eax
cmp    $0x20,%ax
je     1189 <read_header+0x69>
lea    0xe99(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <pexit@plt>
movzwl 0x2(%rsp),%eax
and    $0x20,%eax
mov    %eax,(%rbx)
add    $0xfffffff8,%r14d
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <read@plt>
cmp    %r14d,%eax
je     11be <read_header+0x9e>
lea    0xe51(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <pexit@plt>
mov    0x10(%rsp),%eax
mov    %eax,(%r12)
mov    0x18(%rsp),%eax
mov    %eax,(%r15)
mov    0x20(%rsp),%eax
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"file header too short: \00"
@.str.1 = [26 x i8] c"bad header length. File: \00"
@.str.2 = [19 x i8] c"header too short: \00"
define i32 @read_header(i32 %0, i32* %1, i32* %2, i32* %3, i32* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i8*
%14 = alloca [12 x i64]
%15 = alloca [4 x i16]
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %8
store i32* %1, i32** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i32* %4, i32** %12
store i8* %5, i8** %13
%18 = load i32, i32* %8
%19 = getelementptr inbounds [4 x i16], [4 x i16]* %15, i64 0, i64 0
%20 = call i32 (i32, i16*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i16*, i32, ...)*)(i32 %18, i16* %19, i32 8)
store i32 %20, i32* %16
%21 = icmp ne i32 %20, 8
br i1 %21, label %22, label %25
22: ; preds = %6
%23 = load i8*, i8** %13
%24 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @pexit to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %6
%26 = getelementptr inbounds [4 x i16], [4 x i16]* %15, i64 0, i64 2
%27 = load i16, i16* %26
%28 = zext i16 %27 to i32
store i32 %28, i32* %17
%29 = load i32, i32* %17
%30 = icmp ne i32 %29, 32
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = load i32, i32* %17
%33 = icmp ne i32 %32, 48
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i8*, i8** %13
%36 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @pexit to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34, %31, %25
%38 = getelementptr inbounds [4 x i16], [4 x i16]* %15, i64 0, i64 1
%39 = load i16, i16* %38
%40 = zext i16 %39 to i32
%41 = and i32 %40, 32
%42 = load i32*, i32** %9
store i32 %41, i32* %42
%43 = load i32, i32* %8
%44 = getelementptr inbounds [12 x i64], [12 x i64]* %14, i64 0, i64 0
%45 = load i32, i32* %17
%46 = sub nsw i32 %45, 8
%47 = call i32 (i32, i64*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i64*, i32, ...)*)(i32 %43, i64* %44, i32 %46)
store i32 %47, i32* %16
%48 = load i32, i32* %17
%49 = sub nsw i32 %48, 8
%50 = icmp ne i32 %47, %49
br i1 %50, label %51, label %54
51: ; preds = %37
%52 = load i8*, i8** %13
%53 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @pexit to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %51, %37
%55 = getelementptr inbounds [12 x i64], [12 x i64]* %14, i64 0, i64 0
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i32
%58 = load i32*, i32** %10
store i32 %57, i32* %58
%59 = getelementptr inbounds [12 x i64], [12 x i64]* %14, i64 0, i64 1
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i32
%62 = load i32*, i32** %11
store i32 %61, i32* %62
%63 = getelementptr inbounds [12 x i64], [12 x i64]* %14, i64 0, i64 2
%64 = load i64, i64* %63
%65 = trunc i64 %64 to i32
%66 = load i32*, i32** %12
store i32 %65, i32* %66
%67 = load i32, i32* %7
ret i32 %67
}

LLVM IR (-O2):
@.str = [24 x i8] c"file header too short: \00"
@.str.1 = [26 x i8] c"bad header length. File: \00"
@.str.2 = [19 x i8] c"header too short: \00"
define i32 @read_header(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4, i8* %5) {
%7 = alloca [12 x i64]
%8 = alloca [4 x i16]
%9 = bitcast [12 x i64]* %7 to i8*
%10 = bitcast [4 x i16]* %8 to i8*
%11 = getelementptr inbounds [4 x i16], [4 x i16]* %8, i64 0, i64 0
%12 = call i32 (i32, i16*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i16*, i32, ...)*)(i32 %0, i16* %11, i32 8)
%13 = icmp eq i32 %12, 8
br i1 %13, label %16, label %14
14: ; preds = %6
%15 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @pexit to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %16
16: ; preds = %14, %6
%17 = getelementptr inbounds [4 x i16], [4 x i16]* %8, i64 0, i64 2
%18 = load i16, i16* %17
%19 = zext i16 %18 to i32
%20 = and i16 %18, -17
%21 = icmp eq i16 %20, 32
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @pexit to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %24
24: ; preds = %22, %16
%25 = getelementptr inbounds [4 x i16], [4 x i16]* %8, i64 0, i64 1
%26 = load i16, i16* %25
%27 = and i16 %26, 32
%28 = zext i16 %27 to i32
store i32 %28, i32* %1
%29 = getelementptr inbounds [12 x i64], [12 x i64]* %7, i64 0, i64 0
%30 = add nsw i32 %19, -8
%31 = call i32 (i32, i64*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i64*, i32, ...)*)(i32 %0, i64* %29, i32 %30)
%32 = icmp eq i32 %31, %30
br i1 %32, label %35, label %33
33: ; preds = %24
%34 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @pexit to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %35
35: ; preds = %33, %24
%36 = load i64, i64* %29
%37 = trunc i64 %36 to i32
store i32 %37, i32* %2
%38 = getelementptr inbounds [12 x i64], [12 x i64]* %7, i64 0, i64 1
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
store i32 %40, i32* %3
%41 = getelementptr inbounds [12 x i64], [12 x i64]* %7, i64 0, i64 2
%42 = load i64, i64* %41
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
ret i32 undef
}