#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;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int send_cmd(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  int n;
  char buf[4096];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s command\n", argv[0]);
    return 1;
  }
  fd = send_cmd(argv[1]);
  if (fd < 0) {
    return 1;
  }
  for (;;) {
    n = read(fd, buf, sizeof(buf));
    if (n < 0) {
      perror("read");
      printf("\n");
      close(fd);
      return 1;
    }
    if (n == 0) {
      printf("\n");
      close(fd);
      return 0;
    }
    write(1, buf, n);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bb <main+0x4b>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe56(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <main+0x106>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <send_cmd@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e1 <main+0x71>
movl   $0x1,-0x4(%rbp)
jmp    1276 <main+0x106>
jmp    11e6 <main+0x76>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1233 <main+0xc3>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
lea    0xdf7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1276 <main+0x106>
cmpl   $0x0,-0x18(%rbp)
jne    125d <main+0xed>
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1276 <main+0x106>
lea    -0x1020(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11e6 <main+0x76>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
cmp    $0x2,%edi
jne    11f5 <main+0x85>
mov    0x8(%rsi),%rdi
call   1090 <send_cmd@plt>
mov    $0x1,%r14d
test   %eax,%eax
js     1229 <main+0xb9>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
js     11d6 <main+0x66>
mov    %rsp,%rbx
nop
test   %eax,%eax
je     1215 <main+0xa5>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jns    11b0 <main+0x40>
lea    0xe36(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1229 <main+0xb9>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdf8(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%r14d
jmp    1229 <main+0xb9>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Usage: %s command\0A\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %48
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @send_cmd(i8* %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 1, i32* %3
br label %48
25: ; preds = %17
br label %26
26: ; preds = %44, %25
%27 = load i32, i32* %6
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 4096)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 1, i32* %3
br label %48
37: ; preds = %26
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
store i32 0, i32* %3
br label %48
44: ; preds = %37
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%46 = load i32, i32* %7
%47 = call i32 @write(i32 1, i8* %45, i32 %46)
br label %26
48: ; preds = %40, %32, %24, %11
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Usage: %s command\0A\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %32
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @send_cmd(i8* %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %32, label %15
15: ; preds = %10
%16 = call i32 @read(i32 %13, i8* %4, i32 4096)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %28, %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @putchar(i32 10)
%21 = tail call i32 @close(i32 %13)
br label %32
22: ; preds = %15, %28
%23 = phi i32 [ %30, %28 ], [ %16, %15 ]
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = tail call i32 @putchar(i32 10)
%27 = tail call i32 @close(i32 %13)
br label %32
28: ; preds = %22
%29 = call i32 @write(i32 1, i8* %4, i32 %23)
%30 = call i32 @read(i32 %13, i8* %4, i32 4096)
%31 = icmp slt i32 %30, 0
br i1 %31, label %18, label %22
32: ; preds = %10, %25, %18, %6
%33 = phi i32 [ 1, %6 ], [ 1, %18 ], [ 0, %25 ], [ 1, %10 ]
ret i32 %33
}