#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 */
scalar_t__ get_sha1_hex(void *, unsigned char *);
int /*<<< orphan*/ mark_needs_sha1(unsigned char *, char *, unsigned char *);
scalar_t__ memcmp(void *, char *, int);
int /*<<< orphan*/ printf(char *, char *);
char *sha1_to_hex(unsigned char *);
int fsck_commit(unsigned char *sha1, void *data, unsigned long size) {
  int parents;
  unsigned char tree_sha1[20];
  unsigned char parent_sha1[20];
  if (memcmp(data, "tree ", 5))
    return -1;
  if (get_sha1_hex(data + 5, tree_sha1) < 0)
    return -1;
  mark_needs_sha1(sha1, "tree", tree_sha1);
  data += 5 + 40 + 1;
  parents = 0;
  while (!memcmp(data, "parent ", 7)) {
    if (get_sha1_hex(data + 7, parent_sha1) < 0)
      return -1;
    mark_needs_sha1(sha1, "commit", parent_sha1);
    data += 7 + 40 + 1;
    parents++;
  }
  if (!parents)
    printf("root: %s\n", sha1_to_hex(sha1));
  return 0;
}


x86 assembly (clang, -O0):
<fsck_commit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
mov    $0x5,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     118f <fsck_commit+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <fsck_commit+0x136>
mov    -0x18(%rbp),%rdi
add    $0x5,%rdi
lea    -0x40(%rbp),%rsi
call   1070 <get_sha1_hex@plt>
cmp    $0x0,%rax
jge    11b6 <fsck_commit+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <fsck_commit+0x136>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xe41(%rip),%rsi
call   1030 <mark_needs_sha1@plt>
mov    -0x18(%rbp),%rax
add    $0x2e,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe23(%rip),%rsi
mov    $0x7,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1208 <fsck_commit+0xb8>
jmp    125d <fsck_commit+0x10d>
mov    -0x18(%rbp),%rdi
add    $0x7,%rdi
lea    -0x60(%rbp),%rsi
call   1070 <get_sha1_hex@plt>
cmp    $0x0,%rax
jge    122f <fsck_commit+0xdf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <fsck_commit+0x136>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rdx
lea    0xdd5(%rip),%rsi
call   1030 <mark_needs_sha1@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11dd <fsck_commit+0x8d>
cmpl   $0x0,-0x24(%rbp)
jne    127f <fsck_commit+0x12f>
mov    -0x10(%rbp),%rdi
call   1050 <sha1_to_hex@plt>
mov    %rax,%rsi
lea    0xda0(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsck_commit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe95(%rip),%rsi
mov    %rbp,%rdi
mov    $0x5,%edx
call   1060 <memcmp@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    11de <fsck_commit+0x8e>
lea    0x5(%rbp),%rdi
lea    0x10(%rsp),%rsi
call   1070 <get_sha1_hex@plt>
test   %rax,%rax
js     11de <fsck_commit+0x8e>
lea    0xe69(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   1030 <mark_needs_sha1@plt>
lea    0x2e(%rbp),%rdi
lea    0xe56(%rip),%rsi
mov    $0x7,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     11f0 <fsck_commit+0xa0>
mov    %rbx,%rdi
call   1050 <sha1_to_hex@plt>
lea    0xe47(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,0x8(%rsp)
add    $0x35,%rbp
lea    0x30(%rsp),%r12
lea    0xe0e(%rip),%r13
lea    0xdff(%rip),%r15
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <get_sha1_hex@plt>
test   %rax,%rax
js     11de <fsck_commit+0x8e>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <mark_needs_sha1@plt>
lea    0x29(%rbp),%rdi
mov    %r15,%rsi
mov    $0x7,%edx
call   1060 <memcmp@plt>
add    $0x30,%rbp
test   %rax,%rax
je     1210 <fsck_commit+0xc0>
cmpq   $0x0,0x8(%rsp)
jne    11c4 <fsck_commit+0x74>
jmp    11db <fsck_commit+0x8b>

LLVM IR (-O0):
@.str = [6 x i8] c"tree \00"
@.str.1 = [5 x i8] c"tree\00"
@.str.2 = [8 x i8] c"parent \00"
@.str.3 = [7 x i8] c"commit\00"
@.str.4 = [10 x i8] c"root: %s\0A\00"
define i32 @fsck_commit(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca [20 x i8]
%10 = alloca [20 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = call i64 @memcmp(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %56
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = getelementptr i8, i8* %16, i64 5
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%19 = call i64 @get_sha1_hex(i8* %17, i8* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 -1, i32* %4
br label %56
22: ; preds = %15
%23 = load i8*, i8** %5
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%25 = call i32 @mark_needs_sha1(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %6
%27 = getelementptr i8, i8* %26, i64 46
store i8* %27, i8** %6
store i32 0, i32* %8
br label %28
28: ; preds = %40, %22
%29 = load i8*, i8** %6
%30 = call i64 @memcmp(i8* %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%31 = icmp ne i64 %30, 0
%32 = xor i1 %31, true
br i1 %32, label %33, label %48
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = getelementptr i8, i8* %34, i64 7
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%37 = call i64 @get_sha1_hex(i8* %35, i8* %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -1, i32* %4
br label %56
40: ; preds = %33
%41 = load i8*, i8** %5
%42 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%43 = call i32 @mark_needs_sha1(i8* %41, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load i8*, i8** %6
%45 = getelementptr i8, i8* %44, i64 48
store i8* %45, i8** %6
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %28
48: ; preds = %28
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %48
%52 = load i8*, i8** %5
%53 = call i8* @sha1_to_hex(i8* %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %48
store i32 0, i32* %4
br label %56
56: ; preds = %55, %39, %21, %14
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@.str = [6 x i8] c"tree \00"
@.str.1 = [5 x i8] c"tree\00"
@.str.2 = [8 x i8] c"parent \00"
@.str.3 = [7 x i8] c"commit\00"
@.str.4 = [10 x i8] c"root: %s\0A\00"
define i32 @fsck_commit(i8* %0, i8* %1, i64 %2) {
%4 = alloca [20 x i8]
%5 = alloca [20 x i8]
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%8 = tail call i64 @memcmp(i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %33
10: ; preds = %3
%11 = getelementptr i8, i8* %1, i64 5
%12 = call i64 @get_sha1_hex(i8* %11, i8* %6)
%13 = icmp slt i64 %12, 0
br i1 %13, label %33, label %14
14: ; preds = %10
%15 = call i32 @mark_needs_sha1(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%16 = getelementptr i8, i8* %1, i64 46
%17 = call i64 @memcmp(i8* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %14, %24
%20 = phi i8* [ %26, %24 ], [ %16, %14 ]
%21 = getelementptr i8, i8* %20, i64 7
%22 = call i64 @get_sha1_hex(i8* %21, i8* %7)
%23 = icmp slt i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %19
%25 = call i32 @mark_needs_sha1(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%26 = getelementptr i8, i8* %20, i64 48
%27 = call i64 @memcmp(i8* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%28 = icmp eq i64 %27, 0
br i1 %28, label %19, label %29
29: ; preds = %24
br i1 %18, label %33, label %30
30: ; preds = %14, %29
%31 = call i8* @sha1_to_hex(i8* %0)
%32 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %19, %29, %30, %10, %3
%34 = phi i32 [ -1, %3 ], [ -1, %10 ], [ 0, %30 ], [ 0, %29 ], [ -1, %19 ]
ret i32 %34
}