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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int get_hash(char *pathname, char *hash) {
  char *cmd[] = {"md5sum", pathname, NULL};
  int fd[2];
  int exec_code = 0;
  pid_t wait_code = 0;
  pipe(fd);
  switch (fork()) {
  case -1:
    perror("Fork Error");
    return -1;
    break;
  case 0:
    dup2(fd[1], 1);
    close(fd[0]);
    exec_code = execvp(cmd[0], cmd);
    if (exec_code < 0) {
      perror("Exec Error");
      exit(EXIT_FAILURE);
    }
    break;
  default:
    wait_code = wait(NULL);
    if (wait_code < 0) {
      perror("Wait Error");
      close(fd[1]);
      close(fd[0]);
      return -1;
    }
    size_t nbytes = read(fd[0], hash, sizeof(char) * 32);
    if (nbytes != (sizeof(char) * 32)) {
      fprintf(stderr, "Unable to form hash.\n");
      return -1;
    }
    hash[32] = 0;
    close(fd[1]);
    close(fd[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe49(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
lea    -0x38(%rbp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x4c(%rbp)
sub    $0xffffffff,%eax
je     120c <get_hash+0x6c>
jmp    11fc <get_hash+0x5c>
mov    -0x4c(%rbp),%eax
test   %eax,%eax
je     1224 <get_hash+0x84>
jmp    1272 <get_hash+0xd2>
lea    0xdf4(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <get_hash+0x170>
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <execvp@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    126d <get_hash+0xcd>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1309 <get_hash+0x169>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    12b0 <get_hash+0x110>
lea    0xd8e(%rip),%rdi
call   1080 <perror@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <get_hash+0x170>
mov    -0x38(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x20,-0x48(%rbp)
je     12f1 <get_hash+0x151>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xd48(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <get_hash+0x170>
mov    -0x18(%rbp),%rax
movb   $0x0,0x20(%rax)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hash>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
lea    0xe50(%rip),%rax
mov    %rax,0x10(%rsp)
mov    %rdi,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1223 <get_hash+0x83>
cmp    $0xffffffff,%eax
je     128c <get_hash+0xec>
xor    %edi,%edi
call   10b0 <wait@plt>
test   %eax,%eax
js     129a <get_hash+0xfa>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1060 <read@plt>
cmp    $0x20,%rax
jne    1269 <get_hash+0xc9>
movb   $0x0,0x20(%rbx)
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
xor    %ebp,%ebp
jmp    1283 <get_hash+0xe3>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xdbf(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1090 <execvp@plt>
test   %eax,%eax
jns    1283 <get_hash+0xe3>
lea    0xdbc(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd74(%rip),%rdi
call   1080 <perror@plt>
jmp    127e <get_hash+0xde>
lea    0xd7c(%rip),%rdi
call   1080 <perror@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
jmp    127e <get_hash+0xde>

LLVM IR (-O0):
@.str = [7 x i8] c"md5sum\00"
@.str.1 = [11 x i8] c"Fork Error\00"
@.str.2 = [11 x i8] c"Exec Error\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [11 x i8] c"Wait Error\00"
@stderr = global i32 0
@.str.4 = [22 x i8] c"Unable to form hash.\0A\00"
define i32 @get_hash(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [3 x i8*]
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %4
store i8* %13, i8** %12
%14 = getelementptr inbounds i8*, i8** %12, i64 1
store i8* null, i8** %14
store i32 0, i32* %8
store i32 0, i32* %9
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = call i32 (...) @fork()
switch i32 %17, label %38 [
i32 -1, label %18
i32 0, label %20
]
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %70
20: ; preds = %2
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @dup2(i32 %22, i32 1)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%30 = call i32 @execvp(i8* %28, i8** %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %20
br label %69
38: ; preds = %2
%39 = call i32 @wait(i32* null)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %3
br label %70
50: ; preds = %38
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%52 = load i32, i32* %51
%53 = load i8*, i8** %5
%54 = call i64 @read(i32 %52, i8* %53, i32 32)
store i64 %54, i64* %10
%55 = load i64, i64* %10
%56 = icmp ne i64 %55, 32
br i1 %56, label %57, label %60
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %70
60: ; preds = %50
%61 = load i8*, i8** %5
%62 = getelementptr inbounds i8, i8* %61, i64 32
store i8 0, i8* %62
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
br label %69
69: ; preds = %60, %37
store i32 0, i32* %3
br label %70
70: ; preds = %69, %57, %42, %18
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [7 x i8] c"md5sum\00"
@.str.1 = [11 x i8] c"Fork Error\00"
@.str.2 = [11 x i8] c"Exec Error\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [11 x i8] c"Wait Error\00"
@stderr = global i32 0
@.str.4 = [22 x i8] c"Unable to form hash.\0A\00"
define i32 @get_hash(i8* %0, i8* nocapture %1) {
%3 = alloca [3 x i8*]
%4 = alloca [2 x i32]
%5 = bitcast [3 x i8*]* %3 to i8*
%6 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* %0, i8** %7
%8 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %8
%9 = bitcast [2 x i32]* %4 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i32 (...) @fork()
switch i32 %12, label %27 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %51
15: ; preds = %2
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @dup2(i32 %17, i32 1)
%19 = load i32, i32* %10
%20 = call i32 @close(i32 %19)
%21 = call i32 @execvp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %6)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %51
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %2
%28 = call i32 @wait(i32* null)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %10
%36 = call i32 @close(i32 %35)
br label %51
37: ; preds = %27
%38 = load i32, i32* %10
%39 = call i64 @read(i32 %38, i8* %1, i32 32)
%40 = icmp eq i64 %39, 32
br i1 %40, label %41, label %48
41: ; preds = %37
%42 = getelementptr inbounds i8, i8* %1, i64 32
store i8 0, i8* %42
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %10
%47 = call i32 @close(i32 %46)
br label %51
48: ; preds = %37
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %15, %41, %48, %30, %13
%52 = phi i32 [ -1, %30 ], [ -1, %48 ], [ -1, %13 ], [ 0, %41 ], [ 0, %15 ]
ret i32 %52
}