#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
char *ut_build_nm_cmd(char const *);
char *ut_dump_file(int /*<<< orphan*/ *);
char *ut_dump_nm_output(char const *filename) {
  char *cmd = NULL;
  FILE *fp = NULL;
  char *res = NULL;
  if ((cmd = ut_build_nm_cmd(filename)) == NULL) {
    goto _ut_nm_error_handler;
  }
  if ((fp = popen(cmd, "r")) == NULL) {
    fprintf(stderr, "\x1b[31m"
                    "ERROR"
                    "\x1b[39m"
                    ": failed to open pipe.\n");
    goto _ut_nm_error_handler;
  }
  if ((res = ut_dump_file(fp)) == NULL) {
    fprintf(stderr, "\x1b[31m"
                    "ERROR"
                    "\x1b[39m"
                    ": failed to read nm output.\n");
    goto _ut_nm_error_handler;
  }
  if (pclose(fp) != 0) {
    fprintf(stderr, "\x1b[31m"
                    "ERROR"
                    "\x1b[39m"
                    ": failed to close pipe.\n");
    goto _ut_nm_error_handler;
  }
  free(cmd);
  cmd = NULL;
  return (res);
_ut_nm_error_handler:
  if (cmd != NULL) {
    free(cmd);
  }
  if (fp != NULL) {
    pclose(fp);
  }
  if (res != NULL) {
    free(res);
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<ut_dump_nm_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <ut_build_nm_cmd@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a0 <ut_dump_nm_output+0x40>
jmp    1254 <ut_dump_nm_output+0xf4>
mov    -0x18(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d8 <ut_dump_nm_output+0x78>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1254 <ut_dump_nm_output+0xf4>
mov    -0x20(%rbp),%rdi
call   1040 <ut_dump_file@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1209 <ut_dump_nm_output+0xa9>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1254 <ut_dump_nm_output+0xf4>
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
cmp    $0x0,%rax
je     1236 <ut_dump_nm_output+0xd6>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1254 <ut_dump_nm_output+0xf4>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1298 <ut_dump_nm_output+0x138>
cmpq   $0x0,-0x18(%rbp)
je     1268 <ut_dump_nm_output+0x108>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
je     127c <ut_dump_nm_output+0x11c>
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     1290 <ut_dump_nm_output+0x130>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ut_dump_nm_output>:
push   %r15
push   %r14
push   %rbx
call   1080 <ut_build_nm_cmd@plt>
test   %rax,%rax
je     1224 <ut_dump_nm_output+0xc4>
mov    %rax,%r14
lea    0xe83(%rip),%rsi
mov    %rax,%rdi
call   1070 <popen@plt>
test   %rax,%rax
je     11de <ut_dump_nm_output+0x7e>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <ut_dump_file@plt>
test   %rax,%rax
je     11ff <ut_dump_nm_output+0x9f>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <pclose@plt>
test   %rax,%rax
je     11f5 <ut_dump_nm_output+0x95>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <pclose@plt>
mov    %rbx,%r14
test   %rbx,%rbx
mov    $0x0,%ebx
jne    11f5 <ut_dump_nm_output+0x95>
jmp    1226 <ut_dump_nm_output+0xc6>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1226 <ut_dump_nm_output+0xc6>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <pclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"\1B[31mERROR\1B[39m: failed to open pipe.\0A\00"
@.str.2 = [44 x i8] c"\1B[31mERROR\1B[39m: failed to read nm output.\0A\00"
@.str.3 = [40 x i8] c"\1B[31mERROR\1B[39m: failed to close pipe.\0A\00"
define i8* @ut_dump_nm_output(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
store i32* null, i32** %5
store i8* null, i8** %6
%7 = load i8*, i8** %3
%8 = call i8* @ut_build_nm_cmd(i8* %7)
store i8* %8, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %36
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %5
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %36
18: ; preds = %11
%19 = load i32*, i32** %5
%20 = call i8* @ut_dump_file(i32* %19)
store i8* %20, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %36
25: ; preds = %18
%26 = load i32*, i32** %5
%27 = call i64 @pclose(i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %36
32: ; preds = %25
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
store i8* null, i8** %4
%35 = load i8*, i8** %6
store i8* %35, i8** %2
br label %55
36: ; preds = %29, %22, %15, %10
%37 = load i8*, i8** %4
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i8*, i8** %4
%41 = call i32 @free(i8* %40)
br label %42
42: ; preds = %39, %36
%43 = load i32*, i32** %5
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %5
%47 = call i64 @pclose(i32* %46)
br label %48
48: ; preds = %45, %42
%49 = load i8*, i8** %6
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %51, %48
store i8* null, i8** %2
br label %55
55: ; preds = %54, %32
%56 = load i8*, i8** %2
ret i8* %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"\1B[31mERROR\1B[39m: failed to open pipe.\0A\00"
@.str.2 = [44 x i8] c"\1B[31mERROR\1B[39m: failed to read nm output.\0A\00"
@.str.3 = [40 x i8] c"\1B[31mERROR\1B[39m: failed to close pipe.\0A\00"
define i8* @ut_dump_nm_output(i8* %0) {
%2 = tail call i8* @ut_build_nm_cmd(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %30, label %4
4: ; preds = %1
%5 = tail call i32* @popen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = tail call i8* @ut_dump_file(i32* %5)
%9 = icmp eq i8* %8, null
br i1 %9, label %25, label %10
10: ; preds = %7
%11 = tail call i64 @pclose(i32* %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %16
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %21
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @free(i8* %2)
%20 = tail call i64 @pclose(i32* %5)
br i1 %9, label %30, label %21
21: ; preds = %16, %10, %13
%22 = phi i8* [ %2, %13 ], [ %2, %10 ], [ %8, %16 ]
%23 = phi i8* [ null, %13 ], [ %8, %10 ], [ null, %16 ]
%24 = tail call i32 @free(i8* %22)
br label %30
25: ; preds = %7
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @free(i8* %2)
%29 = tail call i64 @pclose(i32* %5)
br label %30
30: ; preds = %25, %21, %1, %16
%31 = phi i8* [ null, %16 ], [ null, %1 ], [ %23, %21 ], [ null, %25 ]
ret i8* %31
}