#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*/ cmd_buf;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ isspace(unsigned char);
int /*<<< orphan*/ *popen(unsigned char *, char *);
int /*<<< orphan*/ snprintf(unsigned char *, int, char *,
                            unsigned char const *);
int /*<<< orphan*/ stderr;
int strlen(unsigned char *);
int read_git_commit_id_by_version(unsigned char *id_buf, int id_buf_size,
                                  const unsigned char *version) {
  unsigned char cmd_buf[1024];
  snprintf(cmd_buf, sizeof(cmd_buf), "git show --format=%%h \"v%s\"", version);
  FILE *f = popen(cmd_buf, "r");
  if (!f) {
    fprintf(stderr, "cannot invoke git\n");
    exit(1);
  }
  unsigned char buf[1024];
  if (!fgets(buf, sizeof(buf), f)) {
    fprintf(stderr, "unexpected EOF in call to git show\n");
    exit(1);
  }
  int len = strlen(buf);
  while (len > 0 && isspace(buf[len - 1]))
    --len;
  buf[len] = 0;
  if (len <= 0) {
    fprintf(stderr, "empty commit_id for version '%s'\n", version);
    exit(1);
  }
  snprintf(id_buf, id_buf_size, "%s", buf);
  while (fgets(buf, sizeof(buf), f)) {
  }
  fclose(f);
  f = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<read_git_commit_id_by_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x400,%esi
lea    0xe53(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11fb <read_git_commit_id_by_version+0x7b>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x830(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    123e <read_git_commit_id_by_version+0xbe>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x830(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x834(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x834(%rbp)
mov    %al,-0x835(%rbp)
jle    128a <read_git_commit_id_by_version+0x10a>
mov    -0x834(%rbp),%eax
sub    $0x1,%eax
cltq
movzbl -0x830(%rbp,%rax,1),%edi
ff
call   1060 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x835(%rbp)
mov    -0x835(%rbp),%al
test   $0x1,%al
jne    129d <read_git_commit_id_by_version+0x11d>
jmp    12b1 <read_git_commit_id_by_version+0x131>
mov    -0x834(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x834(%rbp)
jmp    1250 <read_git_commit_id_by_version+0xd0>
movslq -0x834(%rbp),%rax
movb   $0x0,-0x830(%rbp,%rax,1)
cmpl   $0x0,-0x834(%rbp)
jg     12f2 <read_git_commit_id_by_version+0x172>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x830(%rbp),%rcx
lea    0xd70(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x830(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1333 <read_git_commit_id_by_version+0x1b3>
jmp    130c <read_git_commit_id_by_version+0x18c>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x428(%rbp)
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_git_commit_id_by_version>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%r12
lea    0xe5f(%rip),%rdx
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     1298 <read_git_commit_id_by_version+0x118>
mov    %rax,%r13
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12aa <read_git_commit_id_by_version+0x12a>
mov    %rsp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
cltq
lea    (%rsp,%rax,1),%rbx
nopl   (%rax)
test   %ebp,%ebp
jle    1271 <read_git_commit_id_by_version+0xf1>
movzbl -0x1(%rsp,%rbp,1),%edi
add    $0xffffffffffffffff,%rbp
call   1060 <isspace@plt>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1200 <read_git_commit_id_by_version+0x80>
movb   $0x0,0x1(%rsp,%rbp,1)
lea    0xe50(%rip),%rdx
mov    %rsp,%rbp
mov    %r12,%rdi
mov    %r15d,%esi
mov    %rbp,%rcx
call   1050 <snprintf@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1240 <read_git_commit_id_by_version+0xc0>
mov    %r13,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%rbx)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
jmp    12ba <read_git_commit_id_by_version+0x13a>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"git show --format=%%h \22v%s\22\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"cannot invoke git\0A\00"
@.str.3 = [36 x i8] c"unexpected EOF in call to git show\0A\00"
@.str.4 = [34 x i8] c"empty commit_id for version '%s'\0A\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @read_git_commit_id_by_version(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i32*
%9 = alloca [1024 x i8]
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%12 = load i8*, i8** %6
%13 = call i32 @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%15 = call i32* @popen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %22, label %18
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%24 = load i32*, i32** %8
%25 = call i64 @fgets(i8* %23, i32 1024, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %22
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %10
br label %34
34: ; preds = %47, %31
%35 = load i32, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = load i32, i32* %10
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = call i64 @isspace(i8 zeroext %42)
%44 = icmp ne i64 %43, 0
br label %45
45: ; preds = %37, %34
%46 = phi i1 [ false, %34 ], [ %44, %37 ]
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = load i32, i32* %10
%49 = add nsw i32 %48, -1
store i32 %49, i32* %10
br label %34
50: ; preds = %45
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %52
store i8 0, i8* %53
%54 = load i32, i32* %10
%55 = icmp sle i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %6
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %58)
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %50
%62 = load i8*, i8** %4
%63 = load i32, i32* %5
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%65 = call i32 @snprintf(i8* %62, i32 %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %71, %61
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%68 = load i32*, i32** %8
%69 = call i64 @fgets(i8* %67, i32 1024, i32* %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %66
br label %66
72: ; preds = %66
%73 = load i32*, i32** %8
%74 = call i32 @fclose(i32* %73)
store i32* null, i32** %8
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"git show --format=%%h \22v%s\22\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"cannot invoke git\0A\00"
@.str.3 = [36 x i8] c"unexpected EOF in call to git show\0A\00"
@.str.4 = [34 x i8] c"empty commit_id for version '%s'\0A\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @read_git_commit_id_by_version(i8* %0, i32 %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %2)
%8 = call i32* @popen(i8* %6, 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 %10, label %14
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i64 @fgets(i8* %15, i32 1024, i32* %8)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = call i32 @strlen(i8* %15)
%24 = zext i32 %23 to i64
br label %25
25: ; preds = %29, %22
%26 = phi i64 [ %30, %29 ], [ %24, %22 ]
%27 = trunc i64 %26 to i32
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = add nsw i64 %26, -1
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = call i64 @isspace(i8 zeroext %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %42, label %25
35: ; preds = %25
%36 = shl i64 %26, 32
%37 = ashr exact i64 %36, 32
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
%43 = and i64 %26, 4294967295
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %15)
br label %46
46: ; preds = %46, %42
%47 = call i64 @fgets(i8* %15, i32 1024, i32* %8)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %46
49: ; preds = %46
%50 = call i32 @fclose(i32* %8)
ret i32 0
}