int read_tree(unsigned char *sha1, const char *base, int baselen) {
  void *buffer;
  unsigned long size;
  char type[20];
  buffer = read_sha1_file(sha1, type, &size);
  if (!buffer)
    return -1;
  if (strcmp(type, "tree"))
    return -1;
  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)
      return -1;
    buffer = sha1 + 20;
    size -= len + 20;
    if (S_ISDIR(mode)) {
      int retval;
      int pathlen = strlen(path);
      char *newbase = malloc(baselen + 1 + pathlen);
      memcpy(newbase, base, baselen);
      memcpy(newbase + baselen, path, pathlen);
      newbase[baselen + pathlen] = '/';
      retval = read_tree(sha1, newbase, baselen + pathlen + 1);
      free(newbase);
      if (retval)
        return -1;
      continue;
    }
    if (read_one_entry(sha1, base, baselen, path, mode) < 0)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1090 <read_sha1_file@plt>
cltq
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f6 <read_tree+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <read_tree+0x1f7>
lea    -0x50(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
je     121b <read_tree+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <read_tree+0x1f7>
jmp    1220 <read_tree+0x70>
cmpq   $0x0,-0x30(%rbp)
je     13a0 <read_tree+0x1f0>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x54(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x20,%esi
call   1060 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x68(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x54(%rbp),%ecx
add    $0x14,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jb     1295 <read_tree+0xe5>
mov    -0x28(%rbp),%rdi
lea    0xd84(%rip),%rsi
lea    -0x6c(%rbp),%rdx
mov    $0x0,%al
call   10c0 <sscanf@plt>
cmp    $0x1,%eax
je     12a1 <read_tree+0xf1>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <read_tree+0x1f7>
mov    -0x60(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x28(%rbp)
mov    -0x54(%rbp),%eax
add    $0x14,%eax
movslq %eax,%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x6c(%rbp),%edi
mov    $0x0,%al
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
je     136c <read_tree+0x1bc>
mov    -0x68(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x74(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
add    -0x74(%rbp),%eax
movslq %eax,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rdx
call   10a0 <memcpy@plt>
mov    -0x80(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x68(%rbp),%rsi
movslq -0x74(%rbp),%rdx
call   10a0 <memcpy@plt>
mov    -0x80(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    -0x74(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x60(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    -0x74(%rbp),%edx
add    $0x1,%edx
call   10d0 <read_tree@plt>
mov    %eax,-0x70(%rbp)
mov    -0x80(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x0,-0x70(%rbp)
je     1367 <read_tree+0x1b7>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <read_tree+0x1f7>
jmp    1220 <read_tree+0x70>
mov    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x68(%rbp),%rcx
mov    -0x6c(%rbp),%r8d
mov    $0x0,%al
call   1030 <read_one_entry@plt>
cmp    $0x0,%eax
jge    139b <read_tree+0x1eb>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <read_tree+0x1f7>
jmp    1220 <read_tree+0x70>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_tree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r12d
mov    %rsi,0x18(%rsp)
lea    0x30(%rsp),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1080 <read_sha1_file@plt>
mov    $0xffffffff,%esi
test   %eax,%eax
je     134f <read_tree+0x1af>
mov    $0x65657274,%ecx
xor    0x30(%rsp),%ecx
movzbl 0x34(%rsp),%edx
or     %ecx,%edx
jne    134f <read_tree+0x1af>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     134d <read_tree+0x1ad>
movslq %eax,%r15
movslq %r12d,%rax
mov    %rax,0x20(%rsp)
mov    %r12,0x28(%rsp)
jmp    1244 <read_tree+0xa4>
nopw   0x0(%rax,%rax,1)
mov    0x4(%rsp),%r8d
mov    %r15,%rdi
mov    0x18(%rsp),%rsi
mov    %r12d,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   1030 <read_one_entry@plt>
test   %eax,%eax
js     1346 <read_tree+0x1a6>
add    $0x14,%r15
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     134d <read_tree+0x1ad>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
shl    $0x20,%r14
mov    %r15,%rdi
mov    $0x20,%esi
call   1060 <strchr@plt>
mov    %rax,%r13
movabs $0x1500000000,%rax
lea    (%r14,%rax,1),%rbp
sar    $0x20,%rbp
cmp    %rbp,%rbx
jb     1346 <read_tree+0x1a6>
mov    %r15,%rdi
lea    0xd7d(%rip),%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   10b0 <sscanf@plt>
cmp    $0x1,%eax
jne    1346 <read_tree+0x1a6>
movabs $0x100000000,%rax
add    %rax,%r14
sar    $0x20,%r14
add    %r14,%r15
add    $0x1,%r13
sub    %rbp,0x8(%rsp)
mov    0x4(%rsp),%edi
xor    %eax,%eax
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     1210 <read_tree+0x70>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
add    %r12d,%eax
mov    %eax,0x14(%rsp)
mov    0x20(%rsp),%rbp
lea    (%rbx,%rbp,1),%eax
add    $0x1,%eax
movslq %eax,%r14
mov    %r14,%rdi
call   10a0 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    0x18(%rsp),%rsi
mov    %rbp,%rdx
call   1090 <memcpy@plt>
lea    (%r12,%rbp,1),%rdi
movslq %ebx,%rdx
mov    %r13,%rsi
call   1090 <memcpy@plt>
movslq 0x14(%rsp),%rax
movb   $0x2f,(%r12,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   10c0 <read_tree@plt>
mov    %eax,%ebx
mov    %r12,%rdi
mov    0x28(%rsp),%r12
call   1040 <free@plt>
test   %ebx,%ebx
je     1232 <read_tree+0x92>
mov    $0xffffffff,%esi
jmp    134f <read_tree+0x1af>
xor    %esi,%esi
mov    %esi,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"tree\00"
@.str.1 = [3 x i8] c"%o\00"
define i32 @read_tree(i8* %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = alloca [20 x i8]
%6 = alloca i32
%7 = bitcast i64* %4 to i8*
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%9 = call i32 (i8*, i8*, i64*, ...) bitcast (i32 (...)* @read_sha1_file to i32 (i8*, i8*, i64*, ...)*)(i8* %0, i8* %8, i64* %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %67, label %11
11: ; preds = %3
%12 = sext i32 %9 to i64
%13 = inttoptr i64 %12 to i8*
%14 = call i32 @bcmp(i8* dereferenceable(5) %8, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %67
16: ; preds = %11
%17 = load i64, i64* %4
%18 = icmp eq i64 %17, 0
br i1 %18, label %67, label %19
19: ; preds = %16
%20 = bitcast i32* %6 to i8*
%21 = sext i32 %2 to i64
br label %22
22: ; preds = %19, %64
%23 = phi i64 [ %17, %19 ], [ %65, %64 ]
%24 = phi i8* [ %13, %19 ], [ %39, %64 ]
%25 = call i64 @strlen(i8* dereferenceable(1) %24)
%26 = shl i64 %25, 32
%27 = add i64 %26, 4294967296
%28 = ashr exact i64 %27, 32
%29 = getelementptr i8, i8* %24, i64 %28
%30 = call i8* @strchr(i8* dereferenceable(1) %24, i32 32)
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = add i64 %26, 90194313216
%33 = ashr exact i64 %32, 32
%34 = icmp ult i64 %23, %33
br i1 %34, label %63, label %35
35: ; preds = %22
%36 = call i32 (i8*, i8*, ...) @sscanf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %63
38: ; preds = %35
%39 = getelementptr inbounds i8, i8* %29, i64 20
%40 = load i64, i64* %4
%41 = sub i64 %40, %33
store i64 %41, i64* %4
%42 = load i32, i32* %6
%43 = call i32 (i32, ...) bitcast (i32 (...)* @S_ISDIR to i32 (i32, ...)*)(i32 %42)
%44 = icmp eq i32 %43, 0
br i1 %44, label %59, label %45
45: ; preds = %38
%46 = call i64 @strlen(i8* %31)
%47 = trunc i64 %46 to i32
%48 = add i32 %47, %2
%49 = add i32 %48, 1
%50 = sext i32 %49 to i64
%51 = call i8* @malloc(i64 %50)
%52 = getelementptr inbounds i8, i8* %51, i64 %21
%53 = shl i64 %46, 32
%54 = ashr exact i64 %53, 32
%55 = sext i32 %48 to i64
%56 = getelementptr inbounds i8, i8* %51, i64 %55
store i8 47, i8* %56
%57 = call i32 @read_tree(i8* %29, i8* %51, i32 %49)
%58 = icmp eq i32 %57, 0
br i1 %58, label %64, label %63
59: ; preds = %38
%60 = load i32, i32* %6
%61 = call i32 (i8*, i8*, i32, i8*, i32, ...) bitcast (i32 (...)* @read_one_entry to i32 (i8*, i8*, i32, i8*, i32, ...)*)(i8* %29, i8* %1, i32 %2, i8* %31, i32 %60)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %45, %35, %22, %59
br label %67
64: ; preds = %45, %59
%65 = load i64, i64* %4
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %22
67: ; preds = %64, %16, %63, %11, %3
%68 = phi i32 [ -1, %3 ], [ -1, %11 ], [ -1, %63 ], [ 0, %16 ], [ 0, %64 ]
ret i32 %68
}