#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 */
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ create_dir(char *, int);
int /*<<< orphan*/ *create_file(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ is_end_of_archive(char *);
char *malloc(int);
int parseoct(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ verify_checksum(char *);
void untar(FILE *a, const char *path) {
  char buff[512];
  FILE *f = NULL;
  size_t bytes_read;
  int filesize;
  for (;;) {
    bytes_read = fread(buff, 1, 512, a);
    if (bytes_read < 512) {
      return;
    }
    if (is_end_of_archive(buff)) {
      return;
    }
    if (!verify_checksum(buff)) {
      return;
    }
    filesize = parseoct(buff + 124, 12);
    char *filepath = malloc(sizeof(buff) + sizeof(path) + sizeof("/"));
    if (filepath) {
      strcpy(filepath, path);
      strcat(filepath, "/");
      strcat(filepath, buff);
    }
    switch (buff[156]) {
    case '1':
      break;
    case '2':
      break;
    case '3':
      break;
    case '4':
      break;
    case '5':
      create_dir(filepath, parseoct(buff + 100, 8));
      filesize = 0;
      break;
    case '6':
      break;
    default:
      f = create_file(filepath, parseoct(buff + 100, 8));
      break;
    }
    while (filesize > 0) {
      bytes_read = fread(buff, 1, 512, a);
      if (bytes_read < 512) {
        if (filepath != NULL)
          free(filepath);
        return;
      }
      if (filesize < 512)
        bytes_read = filesize;
      if (f != NULL) {
        if (fwrite(buff, 1, bytes_read, f) != bytes_read) {
        }
      }
      filesize -= bytes_read;
    }
    if (f != NULL) {
      fclose(f);
      f = NULL;
    }
    free(filepath);
  }
}


x86 assembly (clang, -O0):
<untar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x218(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1050 <fread@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x200,-0x220(%rbp)
jae    1215 <untar+0x55>
jmp    1495 <untar+0x2d5>
lea    -0x210(%rbp),%rdi
call   1090 <is_end_of_archive@plt>
cmp    $0x0,%rax
je     1230 <untar+0x70>
jmp    1495 <untar+0x2d5>
lea    -0x210(%rbp),%rdi
call   10a0 <verify_checksum@plt>
cmp    $0x0,%eax
jne    124a <untar+0x8a>
jmp    1495 <untar+0x2d5>
lea    -0x210(%rbp),%rdi
add    $0x7c,%rdi
mov    $0xc,%esi
call   10e0 <parseoct@plt>
mov    %eax,-0x224(%rbp)
mov    $0x20a,%edi
call   1080 <malloc@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
je     12ba <untar+0xfa>
mov    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x230(%rbp),%rdi
lea    0xd76(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x230(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   10c0 <strcat@plt>
movsbl -0x174(%rbp),%eax
add    $0xffffffcf,%eax
mov    %eax,%ecx
mov    %rcx,-0x238(%rbp)
sub    $0x5,%eax
ja     1346 <untar+0x186>
mov    -0x238(%rbp),%rax
lea    0xd1c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    137e <untar+0x1be>
jmp    137e <untar+0x1be>
jmp    137e <untar+0x1be>
jmp    137e <untar+0x1be>
mov    -0x230(%rbp),%rax
mov    %rax,-0x240(%rbp)
lea    -0x210(%rbp),%rdi
add    $0x64,%rdi
mov    $0x8,%esi
call   10e0 <parseoct@plt>
mov    -0x240(%rbp),%rdi
mov    %eax,%esi
call   1070 <create_dir@plt>
movl   $0x0,-0x224(%rbp)
jmp    137e <untar+0x1be>
jmp    137e <untar+0x1be>
mov    -0x230(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x210(%rbp),%rdi
add    $0x64,%rdi
mov    $0x8,%esi
call   10e0 <parseoct@plt>
mov    -0x248(%rbp),%rdi
mov    %eax,%esi
call   10b0 <create_file@plt>
mov    %rax,-0x218(%rbp)
jmp    1383 <untar+0x1c3>
cmpl   $0x0,-0x224(%rbp)
jle    145f <untar+0x29f>
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1050 <fread@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x200,-0x220(%rbp)
jae    13e1 <untar+0x221>
cmpq   $0x0,-0x230(%rbp)
je     13dc <untar+0x21c>
mov    -0x230(%rbp),%rdi
call   1030 <free@plt>
jmp    1495 <untar+0x2d5>
cmpl   $0x200,-0x224(%rbp)
jge    13ff <untar+0x23f>
movslq -0x224(%rbp),%rax
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     1443 <untar+0x283>
lea    -0x210(%rbp),%rdi
mov    -0x220(%rbp),%rdx
mov    -0x218(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
cmp    -0x220(%rbp),%rax
je     143e <untar+0x27e>
jmp    143e <untar+0x27e>
jmp    1443 <untar+0x283>
mov    -0x220(%rbp),%rcx
movslq -0x224(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x224(%rbp)
jmp    1383 <untar+0x1c3>
cmpq   $0x0,-0x218(%rbp)
je     1484 <untar+0x2c4>
mov    -0x218(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x218(%rbp)
mov    -0x230(%rbp),%rdi
call   1030 <free@plt>
jmp    11de <untar+0x1e>
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<untar>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x200,%rax
jb     1365 <untar+0x1a5>
lea    0x10(%rsp),%r13
mov    %r13,%rdi
call   1090 <is_end_of_archive@plt>
test   %rax,%rax
jne    1365 <untar+0x1a5>
mov    %r13,%rdi
call   10a0 <verify_checksum@plt>
test   %eax,%eax
je     1365 <untar+0x1a5>
lea    0x8c(%rsp),%rdi
mov    $0xc,%esi
call   10e0 <parseoct@plt>
mov    %eax,%ebp
mov    $0x20a,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     126f <untar+0xaf>
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
lea    0xda1(%rip),%rsi
call   10c0 <strcat@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   10c0 <strcat@plt>
movsbl 0xac(%rsp),%eax
lea    -0x31(%rax),%ecx
cmp    $0x4,%ecx
jae    1290 <untar+0xd0>
xor    %r15d,%r15d
jmp    12d3 <untar+0x113>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x0,%r15d
cmp    $0x36,%eax
je     12d3 <untar+0x113>
lea    0x74(%rsp),%rdi
mov    $0x8,%esi
cmp    $0x35,%eax
jne    12bb <untar+0xfb>
call   10e0 <parseoct@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1070 <create_dir@plt>
jmp    132d <untar+0x16d>
call   10e0 <parseoct@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   10b0 <create_file@plt>
mov    %rax,%r15
jmp    12d3 <untar+0x113>
nop
sub    %r14d,%ebp
test   %ebp,%ebp
jle    1320 <untar+0x160>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x1ff,%rax
jbe    1358 <untar+0x198>
mov    %rax,%r14
cmp    $0x200,%ebp
mov    %ebp,%eax
cmovb  %rax,%r14
test   %r15,%r15
je     12d0 <untar+0x110>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   10d0 <fwrite@plt>
jmp    12d0 <untar+0x110>
nopl   (%rax)
test   %r15,%r15
je     132d <untar+0x16d>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x1ff,%rax
ja     1201 <untar+0x41>
jmp    1365 <untar+0x1a5>
test   %r12,%r12
je     1365 <untar+0x1a5>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define void @untar(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %3
store i8* %1, i8** %4
store i32* null, i32** %6
br label %10
10: ; preds = %111, %2
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%12 = load i32*, i32** %3
%13 = call i64 @fread(i8* %11, i32 1, i32 512, i32* %12)
store i64 %13, i64* %7
%14 = load i64, i64* %7
%15 = icmp ult i64 %14, 512
br i1 %15, label %16, label %17
16: ; preds = %10
br label %114
17: ; preds = %10
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%19 = call i64 @is_end_of_archive(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
br label %114
22: ; preds = %17
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%24 = call i32 @verify_checksum(i8* %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %22
br label %114
27: ; preds = %22
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%29 = getelementptr inbounds i8, i8* %28, i64 124
%30 = call i32 @parseoct(i8* %29, i32 12)
store i32 %30, i32* %8
%31 = call i8* @malloc(i32 522)
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %43
34: ; preds = %27
%35 = load i8*, i8** %9
%36 = load i8*, i8** %4
%37 = call i32 @strcpy(i8* %35, i8* %36)
%38 = load i8*, i8** %9
%39 = call i32 @strcat(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%40 = load i8*, i8** %9
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%42 = call i32 @strcat(i8* %40, i8* %41)
br label %43
43: ; preds = %34, %27
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 156
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
switch i32 %46, label %58 [
i32 49, label %47
i32 50, label %48
i32 51, label %49
i32 52, label %50
i32 53, label %51
i32 54, label %57
]
47: ; preds = %43
br label %64
48: ; preds = %43
br label %64
49: ; preds = %43
br label %64
50: ; preds = %43
br label %64
51: ; preds = %43
%52 = load i8*, i8** %9
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%54 = getelementptr inbounds i8, i8* %53, i64 100
%55 = call i32 @parseoct(i8* %54, i32 8)
%56 = call i32 @create_dir(i8* %52, i32 %55)
store i32 0, i32* %8
br label %64
57: ; preds = %43
br label %64
58: ; preds = %43
%59 = load i8*, i8** %9
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%61 = getelementptr inbounds i8, i8* %60, i64 100
%62 = call i32 @parseoct(i8* %61, i32 8)
%63 = call i32* @create_file(i8* %59, i32 %62)
store i32* %63, i32** %6
br label %64
64: ; preds = %58, %57, %51, %50, %49, %48, %47
br label %65
65: ; preds = %99, %64
%66 = load i32, i32* %8
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %105
68: ; preds = %65
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%70 = load i32*, i32** %3
%71 = call i64 @fread(i8* %69, i32 1, i32 512, i32* %70)
store i64 %71, i64* %7
%72 = load i64, i64* %7
%73 = icmp ult i64 %72, 512
br i1 %73, label %74, label %81
74: ; preds = %68
%75 = load i8*, i8** %9
%76 = icmp ne i8* %75, null
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i8*, i8** %9
%79 = call i32 @free(i8* %78)
br label %80
80: ; preds = %77, %74
br label %114
81: ; preds = %68
%82 = load i32, i32* %8
%83 = icmp slt i32 %82, 512
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = load i32, i32* %8
%86 = sext i32 %85 to i64
store i64 %86, i64* %7
br label %87
87: ; preds = %84, %81
%88 = load i32*, i32** %6
%89 = icmp ne i32* %88, null
br i1 %89, label %90, label %99
90: ; preds = %87
%91 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%92 = load i64, i64* %7
%93 = load i32*, i32** %6
%94 = call i64 @fwrite(i8* %91, i32 1, i64 %92, i32* %93)
%95 = load i64, i64* %7
%96 = icmp ne i64 %94, %95
br i1 %96, label %97, label %98
97: ; preds = %90
br label %98
98: ; preds = %97, %90
br label %99
99: ; preds = %98, %87
%100 = load i64, i64* %7
%101 = load i32, i32* %8
%102 = sext i32 %101 to i64
%103 = sub i64 %102, %100
%104 = trunc i64 %103 to i32
store i32 %104, i32* %8
br label %65
105: ; preds = %65
%106 = load i32*, i32** %6
%107 = icmp ne i32* %106, null
br i1 %107, label %108, label %111
108: ; preds = %105
%109 = load i32*, i32** %6
%110 = call i32 @fclose(i32* %109)
store i32* null, i32** %6
br label %111
111: ; preds = %108, %105
%112 = load i8*, i8** %9
%113 = call i32 @free(i8* %112)
br label %10
114: ; preds = %80, %26, %21, %16
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define void @untar(i32* nocapture %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = call i64 @fread(i8* %4, i32 1, i32 512, i32* %0)
%6 = icmp ult i64 %5, 512
br i1 %6, label %64, label %7
7: ; preds = %2
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 124
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 156
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 100
br label %11
11: ; preds = %7, %60
%12 = call i64 @is_end_of_archive(i8* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %64
14: ; preds = %11
%15 = call i32 @verify_checksum(i8* %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %64, label %17
17: ; preds = %14
%18 = call i32 @parseoct(i8* %8, i32 12)
%19 = call dereferenceable_or_null(522) i8* @malloc(i32 522)
%20 = icmp eq i8* %19, null
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = call i32 @strcpy(i8* %19, i8* %1)
%23 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @strcat(i8* %19, i8* %4)
br label %25
25: ; preds = %21, %17
%26 = load i8, i8* %9
%27 = sext i8 %26 to i32
switch i32 %27, label %31 [
i32 49, label %34
i32 50, label %34
i32 51, label %34
i32 52, label %34
i32 53, label %28
i32 54, label %34
]
28: ; preds = %25
%29 = call i32 @parseoct(i8* %10, i32 8)
%30 = call i32 @create_dir(i8* %19, i32 %29)
br label %60
31: ; preds = %25
%32 = call i32 @parseoct(i8* %10, i32 8)
%33 = call i32* @create_file(i8* %19, i32 %32)
br label %34
34: ; preds = %31, %25, %25, %25, %25, %25
%35 = phi i32* [ %33, %31 ], [ null, %25 ], [ null, %25 ], [ null, %25 ], [ null, %25 ], [ null, %25 ]
%36 = icmp sgt i32 %18, 0
br i1 %36, label %37, label %56
37: ; preds = %34
%38 = icmp eq i32* %35, null
br label %39
39: ; preds = %37, %52
%40 = phi i32 [ %18, %37 ], [ %54, %52 ]
%41 = call i64 @fread(i8* %4, i32 1, i32 512, i32* %0)
%42 = icmp ult i64 %41, 512
br i1 %42, label %43, label %46
43: ; preds = %39
br i1 %20, label %64, label %44
44: ; preds = %43
%45 = call i32 @free(i8* %19)
br label %64
46: ; preds = %39
%47 = icmp ult i32 %40, 512
%48 = zext i32 %40 to i64
%49 = select i1 %47, i64 %48, i64 %41
br i1 %38, label %52, label %50
50: ; preds = %46
%51 = call i64 @fwrite(i8* %4, i32 1, i64 %49, i32* %35)
br label %52
52: ; preds = %50, %46
%53 = trunc i64 %49 to i32
%54 = sub i32 %40, %53
%55 = icmp sgt i32 %54, 0
br i1 %55, label %39, label %56
56: ; preds = %52, %34
%57 = icmp eq i32* %35, null
br i1 %57, label %60, label %58
58: ; preds = %56
%59 = call i32 @fclose(i32* %35)
br label %60
60: ; preds = %28, %56, %58
%61 = call i32 @free(i8* %19)
%62 = call i64 @fread(i8* %4, i32 1, i32 512, i32* %0)
%63 = icmp ult i64 %62, 512
br i1 %63, label %64, label %11
64: ; preds = %60, %11, %14, %2, %44, %43
ret void
}