#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ memcmp(char *, unsigned char *, int);
int open(char const *, int /*<<< orphan*/);
int patch_ar(int);
int patch_gzip(int);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char **, int);
void progress(const char *path) {
  int fd;
  unsigned char buf[8];
  if ((fd = open(path, O_RDWR)) == -1) {
    perror("open");
    return;
  }
  if (read(fd, &buf, 8) == -1) {
    perror("read");
  } else if (*buf == 0x1f && *(buf + 1) == 0x8b) {
    if (patch_gzip(fd) == -1)
      perror("patch_gzip");
  } else if (memcmp("!<arch>", buf, 7) == 0) {
    if (patch_ar(fd) == -1)
      perror("patch_ar");
  }
  close(fd);
}


x86 assembly (clang, -O0):
<progress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11ab <progress+0x3b>
lea    0xe5f(%rip),%rdi
call   1090 <perror@plt>
jmp    1267 <progress+0xf7>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11d6 <progress+0x66>
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
jmp    125f <progress+0xef>
movzbl -0x14(%rbp),%eax
cmp    $0x1f,%eax
jne    1214 <progress+0xa4>
movzbl -0x13(%rbp),%eax
cmp    $0x8b,%eax
jne    1214 <progress+0xa4>
mov    -0xc(%rbp),%edi
call   1070 <patch_gzip@plt>
cmp    $0xffffffff,%eax
jne    120f <progress+0x9f>
lea    0xe00(%rip),%rdi
call   1090 <perror@plt>
jmp    125a <progress+0xea>
lea    -0x14(%rbp),%rsi
lea    0xdf6(%rip),%rdi
mov    $0x7,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
jne    1255 <progress+0xe5>
mov    -0xc(%rbp),%edi
call   1040 <patch_ar@plt>
cmp    $0xffffffff,%eax
jne    1250 <progress+0xe0>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
jmp    1255 <progress+0xe5>
jmp    125a <progress+0xea>
jmp    125f <progress+0xef>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<progress>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11f3 <progress+0x83>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1204 <progress+0x94>
cmpb   $0x1f,0x8(%rsp)
jne    11c3 <progress+0x53>
cmpb   $0x8b,0x9(%rsp)
jne    11c3 <progress+0x53>
mov    %ebx,%edi
call   1070 <patch_gzip@plt>
cmp    $0xffffffff,%eax
jne    1210 <progress+0xa0>
lea    0xe49(%rip),%rdi
jmp    120b <progress+0x9b>
lea    0xe4b(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    $0x7,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
jne    1210 <progress+0xa0>
mov    %ebx,%edi
call   1040 <patch_ar@plt>
cmp    $0xffffffff,%eax
jne    1210 <progress+0xa0>
lea    0xe2c(%rip),%rdi
jmp    120b <progress+0x9b>
lea    0xe06(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1090 <perror@plt>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [11 x i8] c"patch_gzip\00"
@.str.3 = [8 x i8] c"!<arch>\00"
@.str.4 = [9 x i8] c"patch_ar\00"
define void @progress(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [8 x i8]
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %52
11: ; preds = %1
%12 = load i32, i32* %3
%13 = bitcast [8 x i8]* %4 to i8**
%14 = call i32 @read(i32 %12, i8** %13, i32 8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %49
18: ; preds = %11
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp eq i32 %21, 31
br i1 %22, label %23, label %36
23: ; preds = %18
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = icmp eq i32 %27, 139
br i1 %28, label %29, label %36
29: ; preds = %23
%30 = load i32, i32* %3
%31 = call i32 @patch_gzip(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
br label %48
36: ; preds = %23, %18
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%38 = call i64 @memcmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %37, i32 7)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load i32, i32* %3
%42 = call i32 @patch_ar(i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
br label %47
47: ; preds = %46, %36
br label %48
48: ; preds = %47, %35
br label %49
49: ; preds = %48, %16
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %9
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [11 x i8] c"patch_gzip\00"
@.str.3 = [8 x i8] c"!<arch>\00"
@.str.4 = [9 x i8] c"patch_ar\00"
define void @progress(i8* nocapture %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %36
9: ; preds = %1
%10 = call i32 @read(i32 %5, i8** %2, i32 8)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %34
14: ; preds = %9
%15 = load i8, i8* %3
%16 = icmp eq i8 %15, 31
%17 = getelementptr inbounds i8, i8* %3, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, -117
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = tail call i32 @patch_gzip(i32 %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %34
26: ; preds = %14
%27 = call i64 @memcmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32 7)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = call i32 @patch_ar(i32 %5)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %24, %21, %29, %32, %26, %12
%35 = call i32 @close(i32 %5)
br label %36
36: ; preds = %34, %7
ret void
}