#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 */
int /*<<< orphan*/ asprintf(char **, char *, char const *);
scalar_t__ exec_cmd(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ warn(char *, char *);
int main(int argc, char **argv) {
  FILE *fh = NULL, *fc = NULL;
  char *hfile, *cfile;
  const char *outdir;
  int ret = 1;
  if (argc < 2) {
    printf("usage: mkversion OUTDIR\n");
    exit(1);
  }
  outdir = argv[1];
  asprintf(&hfile, "%s/version.h", outdir);
  asprintf(&cfile, "%s/version.c", outdir);
  fh = fopen(hfile, "wt");
  if (fh == NULL) {
    warn("cannot open %s", hfile);
    goto out;
  }
  fc = fopen(cfile, "wt");
  if (fc == NULL) {
    warn("cannot open %s", cfile);
    goto out;
  }
  fprintf(fh, "extern const char app_version[];\n"
              "extern const char app_date[];\n");
  fprintf(fc, "const char app_version[] = \"");
  if (exec_cmd("git describe --tags --long", fc) != 0)
    goto out;
  fprintf(fc, "\";\n");
  fprintf(fc, "const char app_date[] = \"");
  if (exec_cmd("git log -n 1 --format=%ai", fc) != 0)
    goto out;
  fprintf(fc, "\";\n");
  ret = 0;
out:
  if (fh != NULL)
    fclose(fh);
  if (fc != NULL)
    fclose(fc);
  free(hfile);
  free(cfile);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x3c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11dd <main+0x4d>
lea    0xe32(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x38(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1245 <main+0xb5>
mov    -0x28(%rbp),%rsi
lea    0xdfb(%rip),%rdi
call   1040 <warn@plt>
jmp    130e <main+0x17e>
mov    -0x30(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1279 <main+0xe9>
mov    -0x30(%rbp),%rsi
lea    0xdc7(%rip),%rdi
call   1040 <warn@plt>
jmp    130e <main+0x17e>
mov    -0x18(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdfe(%rip),%rdi
call   1080 <exec_cmd@plt>
cmp    $0x0,%rax
je     12b8 <main+0x128>
jmp    130e <main+0x17e>
mov    -0x20(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdf8(%rip),%rdi
call   1080 <exec_cmd@plt>
cmp    $0x0,%rax
je     12f7 <main+0x167>
jmp    130e <main+0x17e>
mov    -0x20(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1322 <main+0x192>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     1336 <main+0x1a6>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jle    1310 <main+0x180>
mov    0x8(%rsi),%rbx
lea    0xe54(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbx,%rdx
call   1080 <asprintf@plt>
lea    0xe4d(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1080 <asprintf@plt>
mov    0x8(%rsp),%rdi
lea    0xe43(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12c2 <main+0x132>
mov    %rax,%r14
mov    (%rsp),%rdi
lea    0xe27(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12d5 <main+0x145>
mov    %rax,%rbx
lea    0xe21(%rip),%rdi
mov    $0x3f,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
lea    0xe48(%rip),%rdi
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
lea    0xe4c(%rip),%rdi
mov    %rbx,%rsi
call   1070 <exec_cmd@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    12b0 <main+0x120>
lea    0xe4e(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
lea    0xe39(%rip),%rdi
mov    $0x19,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
lea    0xe3a(%rip),%rdi
mov    %rbx,%rsi
call   1070 <exec_cmd@plt>
test   %rax,%rax
jne    12b0 <main+0x120>
lea    0xe08(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    12f2 <main+0x162>
mov    0x8(%rsp),%rsi
lea    0xd4f(%rip),%rdi
call   1040 <warn@plt>
jmp    12ed <main+0x15d>
mov    (%rsp),%rsi
lea    0xd3d(%rip),%rdi
call   1040 <warn@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    $0x1,%ebp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc5(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"usage: mkversion OUTDIR\0A\00"
@.str.1 = [13 x i8] c"%s/version.h\00"
@.str.2 = [13 x i8] c"%s/version.c\00"
@.str.3 = [3 x i8] c"wt\00"
@.str.4 = [15 x i8] c"cannot open %s\00"
@.str.5 = [64 x i8] c"extern const char app_version[];\0Aextern const char app_date[];\0A\00"
@.str.6 = [29 x i8] c"const char app_version[] = \22\00"
@.str.7 = [27 x i8] c"git describe --tags --long\00"
@.str.8 = [4 x i8] c"\22;\0A\00"
@.str.9 = [26 x i8] c"const char app_date[] = \22\00"
@.str.10 = [26 x i8] c"git log -n 1 --format=%ai\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
store i32* null, i32** %7
store i32 1, i32* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %10
%24 = call i32 @asprintf(i8** %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %8
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %26, i32** %6
%27 = load i32*, i32** %6
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = load i8*, i8** %8
%31 = call i32 @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %30)
br label %61
32: ; preds = %17
%33 = load i8*, i8** %9
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %34, i32** %7
%35 = load i32*, i32** %7
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i8*, i8** %9
%39 = call i32 @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %38)
br label %61
40: ; preds = %32
%41 = load i32*, i32** %6
%42 = call i32 @fprintf(i32* %41, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.5, i64 0, i64 0))
%43 = load i32*, i32** %7
%44 = call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%45 = load i32*, i32** %7
%46 = call i64 @exec_cmd(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %40
br label %61
49: ; preds = %40
%50 = load i32*, i32** %7
%51 = call i32 @fprintf(i32* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%52 = load i32*, i32** %7
%53 = call i32 @fprintf(i32* %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0))
%54 = load i32*, i32** %7
%55 = call i64 @exec_cmd(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i32* %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %49
br label %61
58: ; preds = %49
%59 = load i32*, i32** %7
%60 = call i32 @fprintf(i32* %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %11
br label %61
61: ; preds = %58, %57, %48, %37, %29
%62 = load i32*, i32** %6
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
br label %67
67: ; preds = %64, %61
%68 = load i32*, i32** %7
%69 = icmp ne i32* %68, null
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i32*, i32** %7
%72 = call i32 @fclose(i32* %71)
br label %73
73: ; preds = %70, %67
%74 = load i8*, i8** %8
%75 = call i32 @free(i8* %74)
%76 = load i8*, i8** %9
%77 = call i32 @free(i8* %76)
%78 = load i32, i32* %11
ret i32 %78
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"%s/version.h\00"
@.str.2 = [13 x i8] c"%s/version.c\00"
@.str.3 = [3 x i8] c"wt\00"
@.str.4 = [15 x i8] c"cannot open %s\00"
@.str.5 = [64 x i8] c"extern const char app_version[];\0Aextern const char app_date[];\0A\00"
@.str.6 = [29 x i8] c"const char app_version[] = \22\00"
@.str.7 = [27 x i8] c"git describe --tags --long\00"
@.str.8 = [4 x i8] c"\22;\0A\00"
@.str.9 = [26 x i8] c"const char app_date[] = \22\00"
@.str.10 = [26 x i8] c"git log -n 1 --format=%ai\00"
@str = [24 x i8] c"usage: mkversion OUTDIR\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast i8** %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%16 = load i8*, i8** %3
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %35, label %19
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %38, label %23
23: ; preds = %19
%24 = call i64 @fwrite(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.5, i64 0, i64 0), i64 63, i64 1, i32* %17)
%25 = call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i64 28, i64 1, i32* %21)
%26 = call i64 @exec_cmd(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32* %21)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %23
%29 = call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i64 3, i64 1, i32* %21)
%30 = call i64 @fwrite(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i64 25, i64 1, i32* %21)
%31 = call i64 @exec_cmd(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i32* %21)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %28
%34 = call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i64 3, i64 1, i32* %21)
br label %42
35: ; preds = %11
%36 = load i8*, i8** %3
%37 = call i32 @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %46
38: ; preds = %19
%39 = load i8*, i8** %4
%40 = call i32 @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = call i32 @fclose(i32* %17)
br label %46
42: ; preds = %33, %28, %23
%43 = phi i32 [ 1, %23 ], [ 1, %28 ], [ 0, %33 ]
%44 = call i32 @fclose(i32* %17)
%45 = call i32 @fclose(i32* %21)
br label %46
46: ; preds = %38, %35, %42
%47 = phi i32 [ 1, %35 ], [ %43, %42 ], [ 1, %38 ]
%48 = load i8*, i8** %3
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %4
%51 = call i32 @free(i8* %50)
ret i32 %47
}