#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 */

/* Variables and functions */
int /*<<< orphan*/ dskbcr_(int *, int *);
int /*<<< orphan*/ dskbor_(int *, char *, int *, int *);
int /*<<< orphan*/ dskbr_(int *, char *, int *, int *);
int /*<<< orphan*/ dskbwr_(int *, int *);
int /*<<< orphan*/ gethdl(int *, char *);
int strlen(char *);
int rdhead(char *filename, char *head) {
  int readl;
  int j;
  int lhead;
  int lun;
  int lflag;
  int istat;
  lun = 1;
  j = strlen(filename);
  dskbor_(&lun, filename, &j, &istat);
  if (istat != 0)
    return istat;
  lflag = (1 == 1);
  dskbwr_(&lun, &lflag);
  readl = 512;
  dskbr_(&lun, head, &readl, &istat);
  head[512] = 0;
  if (istat != 0)
    goto close;
  gethdl(&lhead, head);
  if (lhead <= 0) {
    istat = -1;
    goto close;
  }
  readl = lhead - 512;
  if (readl > 0) {
    dskbr_(&lun, head + 512, &readl, &istat);
    if (istat != 0)
      goto close;
  }
close:
  if (istat == 0)
    dskbcr_(&lun, &istat);
  else
    dskbcr_(&lun, &j);
  return istat;
}


x86 assembly (clang, -O0):
<rdhead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1040 <dskbor_@plt>
cmpl   $0x0,-0x30(%rbp)
je     11ad <rdhead+0x4d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <rdhead+0x133>
movl   $0x1,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1060 <dskbwr_@plt>
movl   $0x200,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdi
lea    -0x1c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1080 <dskbr_@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,0x200(%rax)
cmpl   $0x0,-0x30(%rbp)
je     11f7 <rdhead+0x97>
jmp    1264 <rdhead+0x104>
mov    -0x18(%rbp),%rsi
lea    -0x24(%rbp),%rdi
call   1070 <gethdl@plt>
cmpl   $0x0,-0x24(%rbp)
jg     121a <rdhead+0xba>
movl   $0xffffffff,-0x30(%rbp)
jmp    1264 <rdhead+0x104>
mov    -0x24(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    125f <rdhead+0xff>
mov    -0x18(%rbp),%rsi
add    $0x200,%rsi
lea    -0x28(%rbp),%rdi
lea    -0x1c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1080 <dskbr_@plt>
cmpl   $0x0,-0x30(%rbp)
je     125a <rdhead+0xfa>
jmp    1264 <rdhead+0x104>
jmp    125f <rdhead+0xff>
jmp    1264 <rdhead+0x104>
cmpl   $0x0,-0x30(%rbp)
jne    1280 <rdhead+0x120>
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <dskbcr_@plt>
jmp    128d <rdhead+0x12d>
lea    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <dskbcr_@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdhead>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
call   1030 <strlen@plt>
mov    %eax,0x14(%rsp)
lea    0x4(%rsp),%rdi
lea    0x14(%rsp),%r14
mov    %rsp,%rcx
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <dskbor_@plt>
mov    (%rsp),%eax
test   %eax,%eax
jne    11fb <rdhead+0x9b>
movl   $0x1,0xc(%rsp)
lea    0x4(%rsp),%rbx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <dskbwr_@plt>
movl   $0x200,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %rsp,%r15
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rcx
call   1080 <dskbr_@plt>
movb   $0x0,0x200(%r12)
cmpl   $0x0,(%rsp)
je     1207 <rdhead+0xa7>
mov    %r14,%r15
lea    0x4(%rsp),%rdi
mov    %r15,%rsi
call   1050 <dskbcr_@plt>
mov    (%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0x10(%rsp),%rdi
mov    %r12,%rsi
call   1070 <gethdl@plt>
mov    0x10(%rsp),%eax
test   %eax,%eax
jle    1251 <rdhead+0xf1>
lea    -0x200(%rax),%ecx
mov    %ecx,0x8(%rsp)
cmp    $0x201,%eax
jb     1249 <rdhead+0xe9>
add    $0x200,%r12
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
mov    %r12,%rsi
call   1080 <dskbr_@plt>
cmpl   $0x0,(%rsp)
jne    11e8 <rdhead+0x88>
jmp    11eb <rdhead+0x8b>
movl   $0xffffffff,(%rsp)
jmp    11e8 <rdhead+0x88>

LLVM IR (-O0):
define i32 @rdhead(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %9
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i8*, i8** %4
%15 = call i32 @dskbor_(i32* %9, i8* %14, i32* %7, i32* %11)
%16 = load i32, i32* %11
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i32, i32* %11
store i32 %19, i32* %3
br label %58
20: ; preds = %2
store i32 1, i32* %10
%21 = call i32 @dskbwr_(i32* %9, i32* %10)
store i32 512, i32* %6
%22 = load i8*, i8** %5
%23 = call i32 @dskbr_(i32* %9, i8* %22, i32* %6, i32* %11)
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 512
store i8 0, i8* %25
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
br label %49
29: ; preds = %20
%30 = load i8*, i8** %5
%31 = call i32 @gethdl(i32* %8, i8* %30)
%32 = load i32, i32* %8
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -1, i32* %11
br label %49
35: ; preds = %29
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 512
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %35
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 512
%43 = call i32 @dskbr_(i32* %9, i8* %42, i32* %6, i32* %11)
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
br label %49
47: ; preds = %40
br label %48
48: ; preds = %47, %35
br label %49
49: ; preds = %48, %46, %34, %28
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @dskbcr_(i32* %9, i32* %11)
br label %56
54: ; preds = %49
%55 = call i32 @dskbcr_(i32* %9, i32* %7)
br label %56
56: ; preds = %54, %52
%57 = load i32, i32* %11
store i32 %57, i32* %3
br label %58
58: ; preds = %56, %18
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
define i32 @rdhead(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = bitcast i32* %6 to i8*
%13 = bitcast i32* %7 to i8*
%14 = bitcast i32* %8 to i8*
store i32 1, i32* %6
%15 = tail call i32 @strlen(i8* %0)
store i32 %15, i32* %4
%16 = call i32 @dskbor_(i32* %6, i8* %0, i32* %4, i32* %8)
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %43
19: ; preds = %2
store i32 1, i32* %7
%20 = call i32 @dskbwr_(i32* %6, i32* %7)
store i32 512, i32* %3
%21 = call i32 @dskbr_(i32* %6, i8* %1, i32* %3, i32* %8)
%22 = getelementptr inbounds i8, i8* %1, i64 512
store i8 0, i8* %22
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %19
%26 = call i32 @gethdl(i32* %5, i8* %1)
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %8
br label %38
30: ; preds = %25
%31 = add nsw i32 %27, -512
store i32 %31, i32* %3
%32 = icmp ugt i32 %27, 512
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @dskbr_(i32* %6, i8* %22, i32* %3, i32* %8)
br label %35
35: ; preds = %33, %30
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %29, %19, %35
br label %39
39: ; preds = %35, %38
%40 = phi i32* [ %4, %38 ], [ %8, %35 ]
%41 = call i32 @dskbcr_(i32* %6, i32* %40)
%42 = load i32, i32* %8
br label %43
43: ; preds = %2, %39
%44 = phi i32 [ %42, %39 ], [ %17, %2 ]
ret i32 %44
}