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

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
char *smprintf(char *, char *);
int strlen(char *);
char *runcmd(char *cmd) {
  FILE *fp = popen(cmd, "r");
  if (fp == NULL)
    return NULL;
  char ln[30];
  fgets(ln, sizeof(ln) - 1, fp);
  pclose(fp);
  ln[strlen(ln) - 1] = '\0';
  return smprintf("%s", ln);
}


x86 assembly (clang, -O0):
<runcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <runcmd+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11ca <runcmd+0x7a>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1d,%esi
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xe41(%rip),%rdi
call   1030 <smprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runcmd>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xea2(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11a7 <runcmd+0x57>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1050 <pclose@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r14,1)
lea    0xe65(%rip),%rdi
mov    %r14,%rsi
call   1030 <smprintf@plt>
jmp    11a9 <runcmd+0x59>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%s\00"
define i8* @runcmd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [30 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %24
11: ; preds = %1
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%13 = load i32*, i32** %4
%14 = call i32 @fgets(i8* %12, i32 29, i32* %13)
%15 = load i32*, i32** %4
%16 = call i32 @pclose(i32* %15)
%17 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 %20
store i8 0, i8* %21
%22 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%23 = call i8* @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i8* %23, i8** %2
br label %24
24: ; preds = %11, %10
%25 = load i8*, i8** %2
ret i8* %25
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%s\00"
define i8* @runcmd(i8* nocapture %0) {
%2 = alloca [30 x i8]
%3 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%7 = call i32 @fgets(i8* %6, i32 29, i32* %3)
%8 = call i32 @pclose(i32* %3)
%9 = call i32 @strlen(i8* %6)
%10 = add nsw i32 %9, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i8* @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %14
14: ; preds = %1, %5
%15 = phi i8* [ %13, %5 ], [ null, %1 ]
ret i8* %15
}