int unpack(unsigned char *sha1) {
  void *buffer;
  unsigned long size;
  char type[20];
  buffer = read_sha1_file(sha1, type, &size);
  if (!buffer)
    usage("unable to read sha1 file");
  if (strcmp(type, "tree"))
    usage("expected a 'tree' node");
  while (size) {
    int len = strlen(buffer) + 1;
    unsigned char *sha1 = buffer + len;
    char *path = strchr(buffer, ' ') + 1;
    unsigned int mode;
    if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
      usage("corrupt 'tree' file");
    buffer = sha1 + 20;
    size -= len + 20;
    printf("%o %s (%s)\n", mode, path, sha1_to_hex(sha1));
  }
  return 0;
}


x86 assembly (clang, -O0):
<unpack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1080 <read_sha1_file@plt>
cltq
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11be <unpack+0x3e>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   10a0 <usage@plt>
lea    -0x30(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     11e5 <unpack+0x65>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   10a0 <usage@plt>
jmp    11ea <unpack+0x6a>
cmpq   $0x0,-0x18(%rbp)
je     12c2 <unpack+0x142>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x20,%esi
call   1040 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x14,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jb     125f <unpack+0xdf>
mov    -0x10(%rbp),%rdi
lea    0xdea(%rip),%rsi
lea    -0x4c(%rbp),%rdx
mov    $0x0,%al
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     126d <unpack+0xed>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   10a0 <usage@plt>
mov    -0x40(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x10(%rbp)
mov    -0x34(%rbp),%eax
add    $0x14,%eax
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x0,%al
call   1060 <sha1_to_hex@plt>
mov    -0x5c(%rbp),%esi
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11ea <unpack+0x6a>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unpack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <read_sha1_file@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11a1 <unpack+0x31>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1090 <usage@plt>
mov    $0x65657274,%eax
xor    0x10(%rsp),%eax
movzbl 0x14(%rsp),%ecx
or     %eax,%ecx
je     11c1 <unpack+0x51>
lea    0xe64(%rip),%rdi
xor    %eax,%eax
call   1090 <usage@plt>
mov    (%rsp),%r14
test   %r14,%r14
je     1296 <unpack+0x126>
movslq %ebx,%rbx
lea    0xe74(%rip),%r13
jmp    1220 <unpack+0xb0>
nopw   0x0(%rax,%rax,1)
lea    0xe51(%rip),%rdi
xor    %eax,%eax
call   1090 <usage@plt>
lea    0x14(%rbp),%rbx
sub    %r15,(%rsp)
mov    0xc(%rsp),%r15d
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <sha1_to_hex@plt>
mov    %r13,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%r14
test   %r14,%r14
je     1296 <unpack+0x126>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
shl    $0x20,%r15
movabs $0x100000000,%rax
lea    (%r15,%rax,1),%rbp
sar    $0x20,%rbp
add    %rbx,%rbp
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <strchr@plt>
mov    %rax,%r12
add    $0x1,%r12
movabs $0x1500000000,%rax
add    %rax,%r15
sar    $0x20,%r15
cmp    %r15,%r14
jb     11e0 <unpack+0x70>
mov    %rbx,%rdi
lea    0xdb9(%rip),%rsi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    11e0 <unpack+0x70>
jmp    11ee <unpack+0x7e>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"unable to read sha1 file\00"
@.str.1 = [5 x i8] c"tree\00"
@.str.2 = [23 x i8] c"expected a 'tree' node\00"
@.str.3 = [3 x i8] c"%o\00"
@.str.4 = [20 x i8] c"corrupt 'tree' file\00"
@.str.5 = [12 x i8] c"%o %s (%s)\0A\00"
define i32 @unpack(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [20 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%12 = call i32 (i8*, i8*, i64*, ...) bitcast (i32 (...)* @read_sha1_file to i32 (i8*, i8*, i64*, ...)*)(i8* %10, i8* %11, i64* %4)
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %1
%18 = call i32 (i8*, ...) bitcast (i32 (...)* @usage to i32 (i8*, ...)*)(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %1
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%21 = call i32 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 (i8*, ...) bitcast (i32 (...)* @usage to i32 (i8*, ...)*)(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
br label %26
26: ; preds = %52, %25
%27 = load i64, i64* %4
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %65
29: ; preds = %26
%30 = load i8*, i8** %3
%31 = call i64 @strlen(i8* %30)
%32 = add i64 %31, 1
%33 = trunc i64 %32 to i32
store i32 %33, i32* %6
%34 = load i8*, i8** %3
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr i8, i8* %34, i64 %36
store i8* %37, i8** %7
%38 = load i8*, i8** %3
%39 = call i8* @strchr(i8* %38, i32 32)
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8* %40, i8** %8
%41 = load i64, i64* %4
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 20
%44 = sext i32 %43 to i64
%45 = icmp ult i64 %41, %44
br i1 %45, label %50, label %46
46: ; preds = %29
%47 = load i8*, i8** %3
%48 = call i32 (i8*, i8*, ...) @sscanf(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %9)
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %52
50: ; preds = %46, %29
%51 = call i32 (i8*, ...) bitcast (i32 (...)* @usage to i32 (i8*, ...)*)(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i8*, i8** %7
%54 = getelementptr inbounds i8, i8* %53, i64 20
store i8* %54, i8** %3
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 20
%57 = sext i32 %56 to i64
%58 = load i64, i64* %4
%59 = sub i64 %58, %57
store i64 %59, i64* %4
%60 = load i32, i32* %9
%61 = load i8*, i8** %8
%62 = load i8*, i8** %7
%63 = call i32 (i8*, ...) bitcast (i32 (...)* @sha1_to_hex to i32 (i8*, ...)*)(i8* %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %60, i8* %61, i32 %63)
br label %26
65: ; preds = %26
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"unable to read sha1 file\00"
@.str.1 = [5 x i8] c"tree\00"
@.str.2 = [23 x i8] c"expected a 'tree' node\00"
@.str.3 = [3 x i8] c"%o\00"
@.str.4 = [20 x i8] c"corrupt 'tree' file\00"
@.str.5 = [12 x i8] c"%o %s (%s)\0A\00"
define i32 @unpack(i8* %0) {
%2 = alloca i64
%3 = alloca [20 x i8]
%4 = alloca i32
%5 = bitcast i64* %2 to i8*
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = call i32 (i8*, i8*, i64*, ...) bitcast (i32 (...)* @read_sha1_file to i32 (i8*, i8*, i64*, ...)*)(i8* %0, i8* %6, i64* %2)
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i32 %7, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 (i8*, ...) bitcast (i32 (...)* @usage to i32 (i8*, ...)*)(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = call i32 @bcmp(i8* dereferenceable(5) %6, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 (i8*, ...) bitcast (i32 (...)* @usage to i32 (i8*, ...)*)(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i64, i64* %2
%20 = icmp eq i64 %19, 0
br i1 %20, label %50, label %21
21: ; preds = %18
%22 = bitcast i32* %4 to i8*
br label %23
23: ; preds = %21, %41
%24 = phi i64 [ %19, %21 ], [ %48, %41 ]
%25 = phi i8* [ %9, %21 ], [ %42, %41 ]
%26 = call i64 @strlen(i8* dereferenceable(1) %25)
%27 = shl i64 %26, 32
%28 = add i64 %27, 4294967296
%29 = ashr exact i64 %28, 32
%30 = getelementptr i8, i8* %25, i64 %29
%31 = call i8* @strchr(i8* dereferenceable(1) %25, i32 32)
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = add i64 %27, 90194313216
%34 = ashr exact i64 %33, 32
%35 = icmp ult i64 %24, %34
br i1 %35, label %39, label %36
36: ; preds = %23
%37 = call i32 (i8*, i8*, ...) @sscanf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%38 = icmp eq i32 %37, 1
br i1 %38, label %41, label %39
39: ; preds = %36, %23
%40 = call i32 (i8*, ...) bitcast (i32 (...)* @usage to i32 (i8*, ...)*)(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = getelementptr inbounds i8, i8* %30, i64 20
%43 = load i64, i64* %2
%44 = sub i64 %43, %34
store i64 %44, i64* %2
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) bitcast (i32 (...)* @sha1_to_hex to i32 (i8*, ...)*)(i8* %30)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %45, i8* %32, i32 %46)
%48 = load i64, i64* %2
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %23
50: ; preds = %41, %18
ret i32 0
}