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

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ errno;
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *, char *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strchr(char *, char);
char *strdup(char *);
char *get_ostemplate_name(char *ostmpl) {
  FILE *fd;
  char buf[512];
  char *p;
  int status;
  snprintf(buf, sizeof(buf),
           "/usr/bin/vzosname"
           " %s",
           ostmpl);
  if ((fd = popen(buf, "r")) == NULL) {
    logger(-1, errno, "Error in popen(%s)", buf);
    return NULL;
  }
  *buf = 0;
  while ((p = fgets(buf, sizeof(buf), fd)) != NULL)
    ;
  status = pclose(fd);
  if (WEXITSTATUS(status) || *buf == 0) {
    logger(-1, 0, "Unable to get full ostemplate name for %s", ostmpl);
    return NULL;
  }
  if ((p = strchr(buf, '\n')))
    *p = 0;
  return strdup(buf);
}


x86 assembly (clang, -O0):
<get_ostemplate_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
lea    0xe5a(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11fa <get_ostemplate_name+0x7a>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    -0x220(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe2f(%rip),%rdx
call   1070 <logger@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c0 <get_ostemplate_name+0x140>
movb   $0x0,-0x220(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x200,%esi
call   1060 <fgets@plt>
mov    %rax,-0x228(%rbp)
cmp    $0x0,%rax
je     122c <get_ostemplate_name+0xac>
jmp    1201 <get_ostemplate_name+0x81>
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
mov    %eax,-0x22c(%rbp)
mov    -0x22c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    1260 <get_ostemplate_name+0xe0>
movsbl -0x220(%rbp),%eax
cmp    $0x0,%eax
jne    1284 <get_ostemplate_name+0x104>
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
xor    %esi,%esi
lea    0xdb8(%rip),%rdx
call   1070 <logger@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c0 <get_ostemplate_name+0x140>
lea    -0x220(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x228(%rbp)
cmp    $0x0,%rax
je     12b0 <get_ostemplate_name+0x130>
mov    -0x228(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x220(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ostemplate_name>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%r14
lea    0xe6a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     1221 <get_ostemplate_name+0xa1>
mov    %rax,%rbx
movb   $0x0,(%rsp)
mov    %rsp,%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11d0 <get_ostemplate_name+0x50>
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    %eax,%edi
call   1080 <WEXITSTATUS@plt>
test   %rax,%rax
jne    1242 <get_ostemplate_name+0xc2>
cmpb   $0x0,(%rsp)
je     1242 <get_ostemplate_name+0xc2>
mov    %rsp,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1214 <get_ostemplate_name+0x94>
movb   $0x0,(%rax)
mov    %rsp,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
jmp    125a <get_ostemplate_name+0xda>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    0xde6(%rip),%rdx
mov    %rsp,%rcx
mov    $0xffffffff,%edi
call   1070 <logger@plt>
xor    %ebx,%ebx
jmp    125a <get_ostemplate_name+0xda>
lea    0xde1(%rip),%rdx
xor    %ebx,%ebx
mov    $0xffffffff,%edi
xor    %esi,%esi
mov    %r14,%rcx
call   1070 <logger@plt>
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/usr/bin/vzosname %s\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [19 x i8] c"Error in popen(%s)\00"
@.str.3 = [42 x i8] c"Unable to get full ostemplate name for %s\00"
define i8* @get_ostemplate_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [512 x i8]
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%12 = call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%17 = call i32 @logger(i32 -1, i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %16)
store i8* null, i8** %2
br label %49
18: ; preds = %1
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
store i8 0, i8* %19
br label %20
20: ; preds = %25, %18
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** %4
%23 = call i8* @fgets(i8* %21, i32 512, i32* %22)
store i8* %23, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
br label %20
26: ; preds = %20
%27 = load i32*, i32** %4
%28 = call i32 @pclose(i32* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = call i64 @WEXITSTATUS(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %26
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32, %26
%38 = load i8*, i8** %3
%39 = call i32 @logger(i32 -1, i32 0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %38)
store i8* null, i8** %2
br label %49
40: ; preds = %32
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%42 = call i8* @strchr(i8* %41, i8 signext 10)
store i8* %42, i8** %6
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i8*, i8** %6
store i8 0, i8* %45
br label %46
46: ; preds = %44, %40
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%48 = call i8* @strdup(i8* %47)
store i8* %48, i8** %2
br label %49
49: ; preds = %46, %37, %14
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [21 x i8] c"/usr/bin/vzosname %s\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [19 x i8] c"Error in popen(%s)\00"
@.str.3 = [42 x i8] c"Unable to get full ostemplate name for %s\00"
define noalias i8* @get_ostemplate_name(i8* %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 512, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32* @popen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = call i32 @logger(i32 -1, i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %29
10: ; preds = %1
store i8 0, i8* %3
br label %11
11: ; preds = %11, %10
%12 = call i8* @fgets(i8* %3, i32 512, i32* %5)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %11
14: ; preds = %11
%15 = call i32 @pclose(i32* %5)
%16 = call i64 @WEXITSTATUS(i32 %15)
%17 = icmp ne i64 %16, 0
%18 = load i8, i8* %3
%19 = icmp eq i8 %18, 0
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @logger(i32 -1, i32 0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %29
23: ; preds = %14
%24 = call i8* @strchr(i8* %3, i8 signext 10)
%25 = icmp eq i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %23
store i8 0, i8* %24
br label %27
27: ; preds = %26, %23
%28 = call i8* @strdup(i8* %3)
br label %29
29: ; preds = %27, %21, %7
%30 = phi i8* [ null, %7 ], [ null, %21 ], [ %28, %27 ]
ret i8* %30
}