#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*/ FILE;

/* Variables and functions */
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcmp(char *, char const *, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
unsigned int file_matchsha1(const char *path, const char *sha1) {
  FILE *file;
  char command[128];
  char data[40];
  size_t count;
  snprintf(command, 128, "sha1sum %s", path);
  file = popen(command, "r");
  if (!file)
    return 0;
  count = fread(data, 1, 40, file);
  pclose(file);
  return (count == 40) ? !memcmp(data, sha1, 40) : 0;
}


x86 assembly (clang, -O0):
<file_matchsha1>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x80,%esi
lea    0xe86(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ad <file_matchsha1+0x5d>
movl   $0x0,-0x4(%rbp)
jmp    1228 <file_matchsha1+0xd8>
lea    -0xd0(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x28,%edx
call   1030 <fread@plt>
mov    %rax,-0xd8(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
cmpq   $0x28,-0xd8(%rbp)
28
jne    1212 <file_matchsha1+0xc2>
lea    -0xd0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x28,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xdc(%rbp)
jmp    121f <file_matchsha1+0xcf>
xor    %eax,%eax
mov    %eax,-0xdc(%rbp)
jmp    121f <file_matchsha1+0xcf>
mov    -0xdc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_matchsha1>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
mov    %rsi,%r14
mov    %rdi,%rcx
lea    0xe96(%rip),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1050 <snprintf@plt>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1070 <popen@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11d3 <file_matchsha1+0x83>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x28,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <pclose@plt>
cmp    $0x28,%r15
jne    11d3 <file_matchsha1+0x83>
mov    %rsp,%rdi
mov    %r14,%rsi
mov    $0x28,%edx
call   1060 <memcmp@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
mov    %ebp,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"sha1sum %s\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @file_matchsha1(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [128 x i8]
%8 = alloca [40 x i8]
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = call i32 @snprintf(i8* %10, i32 128, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%14 = call i32* @popen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i32 0, i32* %3
br label %36
18: ; preds = %2
%19 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i64 @fread(i8* %19, i32 1, i32 40, i32* %20)
store i64 %21, i64* %9
%22 = load i32*, i32** %6
%23 = call i32 @pclose(i32* %22)
%24 = load i64, i64* %9
%25 = icmp eq i64 %24, 40
br i1 %25, label %26, label %33
26: ; preds = %18
%27 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%28 = load i8*, i8** %5
%29 = call i32 @memcmp(i8* %27, i8* %28, i32 40)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
%32 = zext i1 %31 to i32
br label %34
33: ; preds = %18
br label %34
34: ; preds = %33, %26
%35 = phi i32 [ %32, %26 ], [ 0, %33 ]
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %17
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [11 x i8] c"sha1sum %s\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @file_matchsha1(i8* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = alloca [40 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = call i64 @fread(i8* %6, i32 1, i32 40, i32* %8)
%12 = call i32 @pclose(i32* %8)
%13 = icmp eq i64 %11, 40
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = call i32 @memcmp(i8* %6, i8* %1, i32 40)
%16 = icmp eq i32 %15, 0
%17 = zext i1 %16 to i32
br label %18
18: ; preds = %14, %10, %2
%19 = phi i32 [ 0, %2 ], [ %17, %14 ], [ 0, %10 ]
ret i32 %19
}