#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 fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int run_cmd(char *cmd, char *buf, int buflen) {
  FILE *f = popen(cmd, "r");
  if (!f) {
    perror("!run_cmd():");
    return 0;
  }
  *buf = 0;
  int len = fread(buf, 1, buflen, f);
  pclose(f);
  if (!*buf)
    return 0;
  return len;
}


x86 assembly (clang, -O0):
<run_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118a <run_cmd+0x4a>
lea    0xe89(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <run_cmd+0x91>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11cb <run_cmd+0x8b>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <run_cmd+0x91>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_cmd>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
lea    0xeaf(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     118d <run_cmd+0x4d>
mov    %rax,%rbp
movb   $0x0,(%rbx)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
cmpb   $0x0,(%rbx)
cmovne %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6e(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
jmp    1188 <run_cmd+0x48>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"!run_cmd():\00"
define i32 @run_cmd(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %30
16: ; preds = %3
%17 = load i8*, i8** %6
store i8 0, i8* %17
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = load i32*, i32** %8
%21 = call i32 @fread(i8* %18, i32 1, i32 %19, i32* %20)
store i32 %21, i32* %9
%22 = load i32*, i32** %8
%23 = call i32 @pclose(i32* %22)
%24 = load i8*, i8** %6
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %16
store i32 0, i32* %4
br label %30
28: ; preds = %16
%29 = load i32, i32* %9
store i32 %29, i32* %4
br label %30
30: ; preds = %28, %27, %14
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"!run_cmd():\00"
define i32 @run_cmd(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %14
8: ; preds = %3
store i8 0, i8* %1
%9 = tail call i32 @fread(i8* %1, i32 1, i32 %2, i32* %4)
%10 = tail call i32 @pclose(i32* %4)
%11 = load i8, i8* %1
%12 = icmp eq i8 %11, 0
%13 = select i1 %12, i32 0, i32 %9
br label %14
14: ; preds = %8, %6
%15 = phi i32 [ %13, %8 ], [ 0, %6 ]
ret i32 %15
}