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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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 main(void) {
  char buf[1024];
  FILE *in = popen("./exitstatus 123", "r");
  if (in == NULL) {
    perror("popen:");
    exit(EXIT_FAILURE);
  }
  while (fgets(buf, sizeof(buf), in) != NULL)
    printf("%s", buf);
  printf("\n\n");
  int pcloseRetVal = pclose(in);
  if (pcloseRetVal == -1) {
    perror("pclose");
    exit(EXIT_FAILURE);
  } else if (pcloseRetVal == 0) {
    printf("Program exited normally.\n");
  } else {
    if (WIFEXITED(pcloseRetVal))
      printf("The process exited NORMALLY with return value %u\n",
             WEXITSTATUS(pcloseRetVal));
    else if (WIFSIGNALED(pcloseRetVal))
      printf("The process was terminated due to an UNHANDLED SIGNAL %u\n",
             WTERMSIG(pcloseRetVal));
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    11f4 <main+0x54>
lea    0xe32(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    11f9 <main+0x59>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1235 <main+0x95>
lea    -0x410(%rbp),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f9 <main+0x59>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x418(%rbp),%rdi
call   1040 <pclose@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0xffffffff,-0x41c(%rbp)
jne    127c <main+0xdc>
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x41c(%rbp)
jne    129c <main+0xfc>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    130b <main+0x16b>
mov    -0x41c(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12d1 <main+0x131>
mov    -0x41c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1306 <main+0x166>
mov    -0x41c(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1301 <main+0x161>
mov    -0x41c(%rbp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1306 <main+0x166>
jmp    130b <main+0x16b>
jmp    1310 <main+0x170>
mov    0x2cc1(%rip),%rax
mov    (%rax),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xe3d(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <popen@plt>
test   %rax,%rax
je     12a4 <main+0xf4>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1222 <main+0x72>
lea    0xe23(%rip),%r14
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1200 <main+0x50>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <pclose@plt>
test   %eax,%eax
je     125d <main+0xad>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12ad <main+0xfd>
mov    %ebx,%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
je     126b <main+0xbb>
mov    %ebx,%edi
call   1090 <WEXITSTATUS@plt>
lea    0xdc9(%rip),%rdi
jmp    1285 <main+0xd5>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
jmp    128e <main+0xde>
mov    %ebx,%edi
call   1070 <WIFSIGNALED@plt>
test   %rax,%rax
je     128e <main+0xde>
mov    %ebx,%edi
call   1080 <WTERMSIG@plt>
lea    0xdd1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd68(%rip),%rdi
jmp    12b4 <main+0x104>
lea    0xd69(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"./exitstatus 123\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"popen:\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"\0A\0A\00"
@.str.5 = [7 x i8] c"pclose\00"
@.str.6 = [26 x i8] c"Program exited normally.\0A\00"
@.str.7 = [50 x i8] c"The process exited NORMALLY with return value %u\0A\00"
@.str.8 = [58 x i8] c"The process was terminated due to an UNHANDLED SIGNAL %u\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32* @popen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
br label %13
13: ; preds = %18, %12
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%15 = load i32*, i32** %3
%16 = call i32* @fgets(i8* %14, i32 1024, i32* %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %19)
br label %13
21: ; preds = %13
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%23 = load i32*, i32** %3
%24 = call i32 @pclose(i32* %23)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %54
36: ; preds = %31
%37 = load i32, i32* %4
%38 = call i64 @WIFEXITED(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i32, i32* %4
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0), i32 %42)
br label %53
44: ; preds = %36
%45 = load i32, i32* %4
%46 = call i64 @WIFSIGNALED(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i32, i32* %4
%50 = call i32 @WTERMSIG(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %48, %44
br label %53
53: ; preds = %52, %40
br label %54
54: ; preds = %53, %34
br label %55
55: ; preds = %54
%56 = load i32, i32* @EXIT_SUCCESS
ret i32 %56
}

LLVM IR (-O2):
@.str = [17 x i8] c"./exitstatus 123\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"popen:\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.5 = [7 x i8] c"pclose\00"
@.str.7 = [50 x i8] c"The process exited NORMALLY with return value %u\0A\00"
@.str.8 = [58 x i8] c"The process was terminated due to an UNHANDLED SIGNAL %u\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [2 x i8] c"\0A\00"
@str.9 = [25 x i8] c"Program exited normally.\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @popen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %2, i32 1024, i32* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %16, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %5, %12
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%14 = call i32* @fgets(i8* %2, i32 1024, i32* %3)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %12
16: ; preds = %12, %5
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%18 = call i32 @pclose(i32* %3)
switch i32 %18, label %25 [
i32 -1, label %19
i32 0, label %23
]
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %16
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
br label %37
25: ; preds = %16
%26 = call i64 @WIFEXITED(i32 %18)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i32 @WEXITSTATUS(i32 %18)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0), i32 %29)
br label %37
31: ; preds = %25
%32 = call i64 @WIFSIGNALED(i32 %18)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = call i32 @WTERMSIG(i32 %18)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %23, %31, %34, %28
%38 = load i32, i32* @EXIT_SUCCESS
ret i32 %38
}