#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*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ _PATH_BSHELL;
int /*<<< orphan*/ __UNCONST(char const **);
int /*<<< orphan*/ addchars(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execv(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ getstring();
int /*<<< orphan*/ pbstr(int /*<<< orphan*/);
int pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ wait(int *);
void doesyscmd(const char *cmd) {
  int p[2];
  pid_t pid, cpid;
  const char *argv[4];
  int cc;
  int status;
  fflush(NULL);
  argv[0] = "sh";
  argv[1] = "-c";
  argv[2] = cmd;
  argv[3] = NULL;
  if (pipe(p) == -1)
    err(1, "bad pipe");
  switch (cpid = fork()) {
  case -1:
    err(1, "bad fork");
  case 0:
    (void)close(p[0]);
    (void)dup2(p[1], 1);
    (void)close(p[1]);
    execv(_PATH_BSHELL, __UNCONST(argv));
    exit(1);
  default:
    (void)close(p[1]);
    do {
      char result[4096];
      cc = read(p[0], result, sizeof result);
      if (cc > 0)
        addchars(result, cc);
    } while (cc > 0 || (cc == -1 && errno == EINTR));
    (void)close(p[0]);
    while ((pid = wait(&status)) != cpid && pid >= 0)
      continue;
    pbstr(getstring());
  }
}


x86 assembly (clang, -O0):
<doesyscmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <fflush@plt>
lea    0xe01(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xdf9(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1241 <doesyscmd+0x61>
mov    $0x1,%edi
lea    0xdca(%rip),%rsi
call   1080 <err@plt>
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x1054(%rbp)
sub    $0xffffffff,%eax
je     1274 <doesyscmd+0x94>
jmp    1261 <doesyscmd+0x81>
mov    -0x1054(%rbp),%eax
test   %eax,%eax
je     1285 <doesyscmd+0xa5>
jmp    12d1 <doesyscmd+0xf1>
mov    $0x1,%edi
lea    0xd8f(%rip),%rsi
call   1080 <err@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1058(%rbp)
lea    -0x40(%rbp),%rdi
call   10c0 <__UNCONST@plt>
mov    -0x1058(%rbp),%edi
mov    %eax,%esi
call   10f0 <execv@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x1050(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jle    1309 <doesyscmd+0x129>
lea    -0x1050(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   10a0 <addchars@plt>
jmp    130e <doesyscmd+0x12e>
mov    $0x1,%al
cmpl   $0x0,-0x44(%rbp)
mov    %al,-0x1059(%rbp)
jg     1359 <doesyscmd+0x179>
xor    %eax,%eax
cmpl   $0xffffffff,-0x44(%rbp)
mov    %al,-0x105a(%rbp)
jne    134d <doesyscmd+0x16d>
mov    0x2c9f(%rip),%rax
mov    (%rax),%eax
mov    0x2c8e(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x105a(%rbp)
mov    -0x105a(%rbp),%al
mov    %al,-0x1059(%rbp)
mov    -0x1059(%rbp),%al
test   $0x1,%al
jne    12d9 <doesyscmd+0xf9>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
lea    -0x48(%rbp),%rdi
call   10e0 <wait@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x105b(%rbp)
je     139b <doesyscmd+0x1bb>
cmpl   $0x0,-0x14(%rbp)
setge  %al
mov    %al,-0x105b(%rbp)
mov    -0x105b(%rbp),%al
test   $0x1,%al
jne    13ae <doesyscmd+0x1ce>
jmp    13b3 <doesyscmd+0x1d3>
jmp    136f <doesyscmd+0x18f>
mov    $0x0,%al
call   1030 <getstring@plt>
mov    %eax,%edi
call   10b0 <pbstr@plt>
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doesyscmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1038,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
call   1090 <fflush@plt>
lea    0xe02(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xdf9(%rip),%rax
mov    %rax,0x18(%rsp)
mov    %rbx,0x20(%rsp)
movq   $0x0,0x28(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    123d <doesyscmd+0x5d>
lea    0xdd3(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12fd <doesyscmd+0x11d>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12ec <doesyscmd+0x10c>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0x30(%rsp),%rbx
mov    0x2d6c(%rip),%r14
mov    0x2d5d(%rip),%r15
jmp    128a <doesyscmd+0xaa>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %eax,%esi
call   10a0 <addchars@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     1280 <doesyscmd+0xa0>
cmp    $0xffffffff,%eax
jne    12ac <doesyscmd+0xcc>
mov    (%r15),%eax
cmp    %eax,(%r14)
je     128a <doesyscmd+0xaa>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
lea    0x30(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10e0 <wait@plt>
cmp    %ebp,%eax
je     12d0 <doesyscmd+0xf0>
test   %eax,%eax
jns    12c0 <doesyscmd+0xe0>
xor    %eax,%eax
call   1030 <getstring@plt>
mov    %eax,%edi
call   10b0 <pbstr@plt>
add    $0x1038,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1c(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%ebx
lea    0x10(%rsp),%rdi
call   10c0 <__UNCONST@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   10f0 <execv@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [9 x i8] c"bad pipe\00"
@.str.3 = [9 x i8] c"bad fork\00"
@_PATH_BSHELL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define void @doesyscmd(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x i8*]
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
store i8* %0, i8** %2
%10 = call i32 @fflush(i32* null)
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %12
%13 = load i8*, i8** %2
%14 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 2
store i8* %13, i8** %14
%15 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 3
store i8* null, i8** %15
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %1
%22 = call i32 (...) @fork()
store i32 %22, i32* %5
switch i32 %22, label %40 [
i32 -1, label %23
i32 0, label %25
]
23: ; preds = %21
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %21, %23
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @dup2(i32 %30, i32 1)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* @_PATH_BSHELL
%36 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 0
%37 = call i32 @__UNCONST(i8** %36)
%38 = call i32 @execv(i32 %35, i32 %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %21
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %68, %40
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%46 = load i32, i32* %45
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 4096)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %44
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%53 = load i32, i32* %7
%54 = call i32 @addchars(i8* %52, i32 %53)
br label %55
55: ; preds = %51, %44
br label %56
56: ; preds = %55
%57 = load i32, i32* %7
%58 = icmp sgt i32 %57, 0
br i1 %58, label %68, label %59
59: ; preds = %56
%60 = load i32, i32* %7
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = load i32, i32* @errno
%64 = load i32, i32* @EINTR
%65 = icmp eq i32 %63, %64
br label %66
66: ; preds = %62, %59
%67 = phi i1 [ false, %59 ], [ %65, %62 ]
br label %68
68: ; preds = %66, %56
%69 = phi i1 [ true, %56 ], [ %67, %66 ]
br i1 %69, label %44, label %70
70: ; preds = %68
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %83, %70
%75 = call i32 @wait(i32* %8)
store i32 %75, i32* %4
%76 = load i32, i32* %5
%77 = icmp ne i32 %75, %76
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = load i32, i32* %4
%80 = icmp sge i32 %79, 0
br label %81
81: ; preds = %78, %74
%82 = phi i1 [ false, %74 ], [ %80, %78 ]
br i1 %82, label %83, label %84
83: ; preds = %81
br label %74
84: ; preds = %81
%85 = call i32 (...) @getstring()
%86 = call i32 @pbstr(i32 %85)
br label %87
87: ; preds = %84
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [9 x i8] c"bad pipe\00"
@.str.3 = [9 x i8] c"bad fork\00"
@_PATH_BSHELL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define void @doesyscmd(i8* %0) {
%2 = alloca [2 x i32]
%3 = alloca [4 x i8*]
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = bitcast [2 x i32]* %2 to i8*
%7 = bitcast [4 x i8*]* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call i32 @fflush(i32* null)
%10 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 2
store i8* %0, i8** %12
%13 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 3
store i8* null, i8** %13
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %1
%20 = call i32 (...) @fork()
switch i32 %20, label %35 [
i32 -1, label %21
i32 0, label %23
]
21: ; preds = %19
%22 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %19, %21
%24 = load i32, i32* %14
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @dup2(i32 %27, i32 1)
%29 = load i32, i32* %26
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @_PATH_BSHELL
%32 = call i32 @__UNCONST(i8** %10)
%33 = call i32 @execv(i32 %31, i32 %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %19
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
br label %40
40: ; preds = %52, %35
%41 = load i32, i32* %14
%42 = call i32 @read(i32 %41, i8* %39, i32 4096)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @addchars(i8* %39, i32 %42)
br label %52
46: ; preds = %40
%47 = icmp eq i32 %42, -1
%48 = load i32, i32* @errno
%49 = load i32, i32* @EINTR
%50 = icmp eq i32 %48, %49
%51 = select i1 %47, i1 %50, i1 false
br i1 %51, label %52, label %53
52: ; preds = %46, %44
br label %40
53: ; preds = %46
%54 = load i32, i32* %14
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %56, %53
%57 = call i32 @wait(i32* %4)
%58 = icmp ne i32 %57, %20
%59 = icmp sgt i32 %57, -1
%60 = and i1 %58, %59
br i1 %60, label %56, label %61
61: ; preds = %56
%62 = call i32 (...) @getstring()
%63 = call i32 @pbstr(i32 %62)
ret void
}