#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*/ assert(int);
int get_oct(char const *, int, int);
int /*<<< orphan*/ kprintf(char *, int, int);
int /*<<< orphan*/ memcmp(char const *, char *, int);
int tar_unpack_check_header(const char header[512]) {
  int i, sum = 0;
  for (i = 0; i < 512; i++) {
    sum += (unsigned char)header[i];
  }
  if (!sum) {
    return 0;
  }
  int x = 0;
  for (i = 148; i < 156; i++) {
    x += (unsigned char)header[i];
  }
  sum += 32 * 8 - x;
  int chksum = get_oct(header, 148, 6);
  if (sum != chksum) {
    kprintf("broken header, chksum = %d, but sum = %d\n", chksum, sum);
    assert(sum == chksum);
  }
  assert(!memcmp(header + 257, "ustar", 5));
  return 1;
}


x86 assembly (clang, -O0):
<tar_unpack_check_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x200,-0x14(%rbp)
jge    1187 <tar_unpack_check_header+0x47>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <tar_unpack_check_header+0x1a>
cmpl   $0x0,-0x18(%rbp)
jne    119d <tar_unpack_check_header+0x5d>
movl   $0x0,-0x4(%rbp)
jmp    1262 <tar_unpack_check_header+0x122>
movl   $0x0,-0x1c(%rbp)
movl   $0x94,-0x14(%rbp)
cmpl   $0x9c,-0x14(%rbp)
jge    11d8 <tar_unpack_check_header+0x98>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ab <tar_unpack_check_header+0x6b>
mov    $0x100,%eax
sub    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x94,%esi
mov    $0x6,%edx
call   1050 <get_oct@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     122d <tar_unpack_check_header+0xed>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdeb(%rip),%rdi
call   1060 <kprintf@plt>
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
add    $0x101,%rdi
lea    0xdeb(%rip),%rsi
mov    $0x5,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tar_unpack_check_header>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
pxor   %xmm0,%xmm0
mov    $0xc,%eax
pxor   %xmm2,%xmm2
pxor   %xmm1,%xmm1
nopw   0x0(%rax,%rax,1)
movd   -0xc(%rbx,%rax,1),%xmm3
movd   -0x8(%rbx,%rax,1),%xmm4
punpcklbw %xmm0,%xmm3
punpcklwd %xmm0,%xmm3
paddd  %xmm2,%xmm3
punpcklbw %xmm0,%xmm4
punpcklwd %xmm0,%xmm4
paddd  %xmm1,%xmm4
movd   -0x4(%rbx,%rax,1),%xmm2
movd   (%rbx,%rax,1),%xmm1
punpcklbw %xmm0,%xmm2
punpcklwd %xmm0,%xmm2
paddd  %xmm3,%xmm2
punpcklbw %xmm0,%xmm1
punpcklwd %xmm0,%xmm1
paddd  %xmm4,%xmm1
add    $0x10,%rax
cmp    $0x20c,%rax
jne    1160 <tar_unpack_check_header+0x20>
paddd  %xmm2,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddd  %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%ebp
test   %ebp,%ebp
je     126f <tar_unpack_check_header+0x12f>
movq   0x94(%rbx),%xmm0
pxor   %xmm1,%xmm1
punpcklbw %xmm1,%xmm0
movdqa %xmm0,%xmm2
punpckhwd %xmm1,%xmm2
punpcklwd %xmm1,%xmm0
paddd  %xmm2,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%eax
sub    %eax,%ebp
add    $0x100,%ebp
mov    %rbx,%rdi
mov    $0x94,%esi
mov    $0x6,%edx
call   1050 <get_oct@plt>
cmp    %eax,%ebp
je     1240 <tar_unpack_check_header+0x100>
lea    0xdd0(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1060 <kprintf@plt>
xor    %edi,%edi
call   1040 <assert@plt>
add    $0x101,%rbx
lea    0xddc(%rip),%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
call   1030 <memcmp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    $0x1,%eax
jmp    1271 <tar_unpack_check_header+0x131>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"broken header, chksum = %d, but sum = %d\0A\00"
@.str.1 = [6 x i8] c"ustar\00"
define i32 @tar_unpack_check_header(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
store i32 0, i32* %4
br label %8
8: ; preds = %20, %1
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 512
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = load i32, i32* %5
%19 = add nsw i32 %18, %17
store i32 %19, i32* %5
br label %20
20: ; preds = %11
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %8
23: ; preds = %8
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %23
store i32 0, i32* %2
br label %70
27: ; preds = %23
store i32 0, i32* %6
store i32 148, i32* %4
br label %28
28: ; preds = %40, %27
%29 = load i32, i32* %4
%30 = icmp slt i32 %29, 156
br i1 %30, label %31, label %43
31: ; preds = %28
%32 = load i8*, i8** %3
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = load i32, i32* %6
%39 = add nsw i32 %38, %37
store i32 %39, i32* %6
br label %40
40: ; preds = %31
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %28
43: ; preds = %28
%44 = load i32, i32* %6
%45 = sub nsw i32 256, %44
%46 = load i32, i32* %5
%47 = add nsw i32 %46, %45
store i32 %47, i32* %5
%48 = load i8*, i8** %3
%49 = call i32 @get_oct(i8* %48, i32 148, i32 6)
store i32 %49, i32* %7
%50 = load i32, i32* %5
%51 = load i32, i32* %7
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %62
53: ; preds = %43
%54 = load i32, i32* %7
%55 = load i32, i32* %5
%56 = call i32 @kprintf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %54, i32 %55)
%57 = load i32, i32* %5
%58 = load i32, i32* %7
%59 = icmp eq i32 %57, %58
%60 = zext i1 %59 to i32
%61 = call i32 @assert(i32 %60)
br label %62
62: ; preds = %53, %43
%63 = load i8*, i8** %3
%64 = getelementptr inbounds i8, i8* %63, i64 257
%65 = call i32 @memcmp(i8* %64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%66 = icmp ne i32 %65, 0
%67 = xor i1 %66, true
%68 = zext i1 %67 to i32
%69 = call i32 @assert(i32 %68)
store i32 1, i32* %2
br label %70
70: ; preds = %62, %26
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
@.str = [42 x i8] c"broken header, chksum = %d, but sum = %d\0A\00"
@.str.1 = [6 x i8] c"ustar\00"
define i32 @tar_unpack_check_header(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 0, %1 ], [ %27, %2 ]
%4 = phi <4 x i32> [ zeroinitializer, %1 ], [ %25, %2 ]
%5 = phi <4 x i32> [ zeroinitializer, %1 ], [ %26, %2 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %3
%7 = bitcast i8* %6 to <4 x i8>*
%8 = load <4 x i8>, <4 x i8>* %7
%9 = getelementptr inbounds i8, i8* %6, i64 4
%10 = bitcast i8* %9 to <4 x i8>*
%11 = load <4 x i8>, <4 x i8>* %10
%12 = zext <4 x i8> %8 to <4 x i32>
%13 = zext <4 x i8> %11 to <4 x i32>
%14 = add <4 x i32> %4, %12
%15 = add <4 x i32> %5, %13
%16 = or i64 %3, 8
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = bitcast i8* %17 to <4 x i8>*
%19 = load <4 x i8>, <4 x i8>* %18
%20 = getelementptr inbounds i8, i8* %17, i64 4
%21 = bitcast i8* %20 to <4 x i8>*
%22 = load <4 x i8>, <4 x i8>* %21
%23 = zext <4 x i8> %19 to <4 x i32>
%24 = zext <4 x i8> %22 to <4 x i32>
%25 = add <4 x i32> %14, %23
%26 = add <4 x i32> %15, %24
%27 = add nuw nsw i64 %3, 16
%28 = icmp eq i64 %27, 512
br i1 %28, label %29, label %2
29: ; preds = %2
%30 = add <4 x i32> %26, %25
%31 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %52, label %33
33: ; preds = %29
%34 = getelementptr inbounds i8, i8* %0, i64 148
%35 = bitcast i8* %34 to <8 x i8>*
%36 = load <8 x i8>, <8 x i8>* %35
%37 = zext <8 x i8> %36 to <8 x i32>
%38 = call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> %37)
%39 = sub nsw i32 256, %38
%40 = add nsw i32 %39, %31
%41 = tail call i32 @get_oct(i8* %0, i32 148, i32 6)
%42 = icmp eq i32 %40, %41
br i1 %42, label %46, label %43
43: ; preds = %33
%44 = tail call i32 @kprintf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %40)
%45 = tail call i32 @assert(i32 0)
br label %46
46: ; preds = %43, %33
%47 = getelementptr inbounds i8, i8* %0, i64 257
%48 = tail call i32 @memcmp(i8* %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%49 = icmp eq i32 %48, 0
%50 = zext i1 %49 to i32
%51 = tail call i32 @assert(i32 %50)
br label %52
52: ; preds = %29, %46
%53 = phi i32 [ 1, %46 ], [ 0, %29 ]
ret i32 %53
}