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

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ execv(char *, char *const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
scalar_t__ fork();
int pipe(int *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int cm_run_cmd(char *path, char *const args[], int (*fn)(const char *)) {
  char buf[2048];
  size_t bs = sizeof(buf);
  pid_t p;
  int pfd[2], i, s;
  FILE *f;
  errno = 0;
  if ((i = pipe(pfd)) != 0) {
    return -errno;
  }
  p = fork();
  if (p == 0) {
    close(pfd[0]);
    dup2(pfd[1], STDOUT_FILENO);
    dup2(pfd[1], STDERR_FILENO);
    execv(path, args);
  }
  close(pfd[1]);
  if ((f = fdopen(pfd[0], "r")) == NULL) {
    return -errno;
  }
  waitpid(p, &s, 0);
  if (fn != NULL) {
    while (fgets(buf, bs, f) != NULL) {
      if (fn(buf) != 0) {
        break;
      }
    }
  }
  close(pfd[0]);
  fclose(f);
  if (WIFEXITED(s)) {
    return WEXITSTATUS(s);
  }
  return 0;
}


x86 assembly (clang, -O0):
<cm_run_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x800,-0x828(%rbp)
mov    0x2dff(%rip),%rax
movl   $0x0,(%rax)
lea    -0x838(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x83c(%rbp)
cmp    $0x0,%eax
je     120d <cm_run_cmd+0x5d>
xor    %eax,%eax
mov    0x2dd5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    135e <cm_run_cmd+0x1ae>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x830(%rbp)
cmpq   $0x0,-0x830(%rbp)
jne    1269 <cm_run_cmd+0xb9>
mov    -0x838(%rbp),%edi
call   1050 <close@plt>
mov    -0x834(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x834(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <execv@plt>
mov    -0x834(%rbp),%edi
call   1050 <close@plt>
mov    -0x838(%rbp),%edi
lea    0xd7f(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x848(%rbp)
cmp    $0x0,%rax
jne    12aa <cm_run_cmd+0xfa>
xor    %eax,%eax
mov    0x2d38(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    135e <cm_run_cmd+0x1ae>
mov    -0x830(%rbp),%rdi
lea    -0x840(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmpq   $0x0,-0x20(%rbp)
je     1318 <cm_run_cmd+0x168>
jmp    12cf <cm_run_cmd+0x11f>
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rsi
mov    -0x848(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1313 <cm_run_cmd+0x163>
mov    -0x20(%rbp),%rax
lea    -0x820(%rbp),%rdi
call   *%rax
cmp    $0x0,%eax
je     130e <cm_run_cmd+0x15e>
jmp    1313 <cm_run_cmd+0x163>
jmp    12cf <cm_run_cmd+0x11f>
jmp    1318 <cm_run_cmd+0x168>
mov    -0x838(%rbp),%edi
call   1050 <close@plt>
mov    -0x848(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x840(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1357 <cm_run_cmd+0x1a7>
mov    -0x840(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    135e <cm_run_cmd+0x1ae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cm_run_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x2e07(%rip),%r13
movl   $0x0,0x0(%r13)
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
test   %eax,%eax
je     1203 <cm_run_cmd+0x53>
xor    %r12d,%r12d
sub    0x0(%r13),%r12d
mov    %r12d,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %rax,%r14
test   %rax,%rax
jne    124a <cm_run_cmd+0x9a>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10c0 <execv@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
lea    0xda2(%rip),%rsi
call   1080 <fdopen@plt>
test   %rax,%rax
je     11e7 <cm_run_cmd+0x37>
mov    %rax,%rbx
lea    0xc(%rsp),%rsi
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %edx,%edx
call   10a0 <waitpid@plt>
test   %r15,%r15
je     12af <cm_run_cmd+0xff>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    $0x800,%esi
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12af <cm_run_cmd+0xff>
mov    %rbp,%rdi
call   *%r15
test   %eax,%eax
je     1290 <cm_run_cmd+0xe0>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
je     11ee <cm_run_cmd+0x3e>
mov    0xc(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%r12d
jmp    11ee <cm_run_cmd+0x3e>

LLVM IR (-O0):
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @cm_run_cmd(i8* %0, i8** %1, i32 (i8*)* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32 (i8*)*
%8 = alloca [2048 x i8]
%9 = alloca i64
%10 = alloca i64
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 (i8*)* %2, i32 (i8*)** %7
store i64 2048, i64* %9
store i32 0, i32* @errno
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
store i32 %16, i32* %12
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %84
21: ; preds = %3
%22 = call i64 (...) @fork()
store i64 %22, i64* %10
%23 = load i64, i64* %10
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %40
25: ; preds = %21
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%30 = load i32, i32* %29
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i32, i32* @STDERR_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
%37 = load i8*, i8** %5
%38 = load i8**, i8*** %6
%39 = call i32 @execv(i8* %37, i8** %38)
br label %40
40: ; preds = %25, %21
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32* @fdopen(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %46, i32** %14
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
store i32 %50, i32* %4
br label %84
51: ; preds = %40
%52 = load i64, i64* %10
%53 = call i32 @waitpid(i64 %52, i32* %13, i32 0)
%54 = load i32 (i8*)*, i32 (i8*)** %7
%55 = icmp ne i32 (i8*)* %54, null
br i1 %55, label %56, label %71
56: ; preds = %51
br label %57
57: ; preds = %69, %56
%58 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%59 = load i64, i64* %9
%60 = load i32*, i32** %14
%61 = call i32* @fgets(i8* %58, i64 %59, i32* %60)
%62 = icmp ne i32* %61, null
br i1 %62, label %63, label %70
63: ; preds = %57
%64 = load i32 (i8*)*, i32 (i8*)** %7
%65 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%66 = call i32 %64(i8* %65)
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %63
br label %70
69: ; preds = %63
br label %57
70: ; preds = %68, %57
br label %71
71: ; preds = %70, %51
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = load i32*, i32** %14
%76 = call i32 @fclose(i32* %75)
%77 = load i32, i32* %13
%78 = call i64 @WIFEXITED(i32 %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %71
%81 = load i32, i32* %13
%82 = call i32 @WEXITSTATUS(i32 %81)
store i32 %82, i32* %4
br label %84
83: ; preds = %71
store i32 0, i32* %4
br label %84
84: ; preds = %83, %80, %48, %18
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @cm_run_cmd(i8* %0, i8** %1, i32 (i8*)* %2) {
%4 = alloca [2048 x i8]
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%8 = bitcast [2 x i32]* %5 to i8*
%9 = bitcast i32* %6 to i8*
store i32 0, i32* @errno
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
br label %59
16: ; preds = %3
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %16
%20 = load i32, i32* %10
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%23 = load i32, i32* %22
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %23, i32 %24)
%26 = load i32, i32* %22
%27 = load i32, i32* @STDERR_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = call i32 @execv(i8* %0, i8** %1)
br label %30
30: ; preds = %19, %16
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %10
%35 = call i32* @fdopen(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = load i32, i32* @errno
%39 = sub nsw i32 0, %38
br label %59
40: ; preds = %30
%41 = call i32 @waitpid(i64 %17, i32* %6, i32 0)
%42 = icmp eq i32 (i8*)* %2, null
br i1 %42, label %49, label %43
43: ; preds = %40, %46
%44 = call i32* @fgets(i8* %7, i64 2048, i32* %35)
%45 = icmp eq i32* %44, null
br i1 %45, label %49, label %46
46: ; preds = %43
%47 = call i32 %2(i8* %7)
%48 = icmp eq i32 %47, 0
br i1 %48, label %43, label %49
49: ; preds = %43, %46, %40
%50 = load i32, i32* %10
%51 = call i32 @close(i32 %50)
%52 = call i32 @fclose(i32* %35)
%53 = load i32, i32* %6
%54 = call i64 @WIFEXITED(i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %49
%57 = load i32, i32* %6
%58 = call i32 @WEXITSTATUS(i32 %57)
br label %59
59: ; preds = %49, %56, %37, %13
%60 = phi i32 [ %15, %13 ], [ %39, %37 ], [ %58, %56 ], [ 0, %49 ]
ret i32 %60
}