#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 */
char *callocr(int);
scalar_t__ errno;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *exec(char *cmd) {
  FILE *fp;
  char *state = callocr(175);
  if ((fp = popen(cmd, "r")) == NULL) {
    printf("popen failed\n");
    if (errno != 0)
      perror("popen() failed");
    strncpy(state, "popen() failed!", 100);
    return state;
  }
  if ((errno = 0) == 0 && fgets(state, 175, fp) == NULL && errno != 0) {
    perror("fgets failed");
    strncpy(state, "fgets() failed!", 100);
    return state;
  }
  if ((pclose(fp)) == -1) {
    printf("warn: pclose failed\n");
    perror("pclose");
  }
  int len = strlen(state);
  if (len != 0 && state[len - 1] == '\n')
    state[len - 1] = '\0';
  return state;
}


x86 assembly (clang, -O0):
<exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0xaf,%edi
call   1080 <callocr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1203 <exec+0x83>
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e15(%rip),%rax
cmpq   $0x0,(%rax)
je     11e1 <exec+0x61>
lea    0xe34(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
lea    0xe33(%rip),%rsi
mov    $0x64,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ee <exec+0x16e>
mov    0x2dd6(%rip),%rax
movq   $0x0,(%rax)
mov    $0x1,%al
test   $0x1,%al
jne    1220 <exec+0xa0>
jmp    127b <exec+0xfb>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xaf,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    127b <exec+0xfb>
mov    0x2d9d(%rip),%rax
cmpq   $0x0,(%rax)
je     127b <exec+0xfb>
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
lea    0xdd8(%rip),%rsi
mov    $0x64,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ee <exec+0x16e>
mov    -0x18(%rbp),%rdi
call   1060 <pclose@plt>
cmp    $0xffffffff,%eax
jne    12a5 <exec+0x125>
lea    0xdb8(%rip),%rdi
call   1050 <printf@plt>
lea    0xdc1(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12e6 <exec+0x166>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12e6 <exec+0x166>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0xaf,%edi
call   1080 <callocr@plt>
mov    %rax,%r14
lea    0xe64(%rip),%rsi
mov    %rbx,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     1200 <exec+0x80>
mov    %rax,%rbx
mov    0x2e2d(%rip),%r15
movq   $0x0,(%r15)
mov    %r14,%rdi
mov    $0xaf,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    11d5 <exec+0x55>
cmpq   $0x0,(%r15)
jne    1236 <exec+0xb6>
mov    %rbx,%rdi
call   1060 <pclose@plt>
cmp    $0xffffffff,%eax
je     124b <exec+0xcb>
mov    %r14,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     122d <exec+0xad>
cltq
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    122d <exec+0xad>
movb   $0x0,-0x1(%r14,%rax,1)
jmp    122d <exec+0xad>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dcd(%rip),%rax
cmpq   $0x0,(%rax)
jne    1268 <exec+0xe8>
lea    0xdf1(%rip),%rsi
mov    %r14,%rdi
mov    $0x64,%edx
call   1030 <strncpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
lea    0xde5(%rip),%rsi
jmp    1220 <exec+0xa0>
lea    0xdf3(%rip),%rdi
call   1040 <puts@plt>
lea    0xde0(%rip),%rdi
call   10a0 <perror@plt>
jmp    11e2 <exec+0x62>
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
jmp    1219 <exec+0x99>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"popen failed\0A\00"
@errno = global i64 0
@.str.2 = [15 x i8] c"popen() failed\00"
@.str.3 = [16 x i8] c"popen() failed!\00"
@.str.4 = [13 x i8] c"fgets failed\00"
@.str.5 = [16 x i8] c"fgets() failed!\00"
@.str.6 = [21 x i8] c"warn: pclose failed\0A\00"
@.str.7 = [7 x i8] c"pclose\00"
define i8* @exec(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i8* @callocr(i32 175)
store i8* %7, i8** %5
%8 = load i8*, i8** %3
%9 = call i32* @popen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %21
11: ; preds = %1
%12 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%13 = load i64, i64* @errno
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = load i8*, i8** %5
%19 = call i32 @strncpy(i8* %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 100)
%20 = load i8*, i8** %5
store i8* %20, i8** %2
br label %64
21: ; preds = %1
store i64 0, i64* @errno
br i1 true, label %22, label %35
22: ; preds = %21
%23 = load i8*, i8** %5
%24 = load i32*, i32** %4
%25 = call i32* @fgets(i8* %23, i32 175, i32* %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%32 = load i8*, i8** %5
%33 = call i32 @strncpy(i8* %32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 100)
%34 = load i8*, i8** %5
store i8* %34, i8** %2
br label %64
35: ; preds = %27, %22, %21
%36 = load i32*, i32** %4
%37 = call i32 @pclose(i32* %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %42
42: ; preds = %39, %35
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %62
47: ; preds = %42
%48 = load i8*, i8** %5
%49 = load i32, i32* %6
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %62
56: ; preds = %47
%57 = load i8*, i8** %5
%58 = load i32, i32* %6
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
store i8 0, i8* %61
br label %62
62: ; preds = %56, %47, %42
%63 = load i8*, i8** %5
store i8* %63, i8** %2
br label %64
64: ; preds = %62, %30, %17
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@.str.2 = [15 x i8] c"popen() failed\00"
@.str.3 = [16 x i8] c"popen() failed!\00"
@.str.4 = [13 x i8] c"fgets failed\00"
@.str.5 = [16 x i8] c"fgets() failed!\00"
@.str.7 = [7 x i8] c"pclose\00"
@str = [20 x i8] c"warn: pclose failed\00"
@str.8 = [13 x i8] c"popen failed\00"
define i8* @exec(i8* nocapture %0) {
%2 = tail call i8* @callocr(i32 175)
%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 %5, label %13
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%7 = load i64, i64* @errno
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = tail call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 100)
br label %38
13: ; preds = %1
store i64 0, i64* @errno
%14 = tail call i32* @fgets(i8* %2, i32 175, i32* %3)
%15 = icmp eq i32* %14, null
%16 = load i64, i64* @errno
%17 = icmp ne i64 %16, 0
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 100)
br label %38
22: ; preds = %13
%23 = tail call i32 @pclose(i32* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %28
28: ; preds = %25, %22
%29 = tail call i32 @strlen(i8* %2)
%30 = icmp eq i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %28
%32 = add nsw i32 %29, -1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %2, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 10
br i1 %36, label %37, label %38
37: ; preds = %31
store i8 0, i8* %34
br label %38
38: ; preds = %28, %31, %37, %19, %11
ret i8* %2
}