#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 */
struct servent {
  int /*<<< orphan*/ s_port;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ line;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ STDOUT_FILENO;
char *__UNCONST(char *);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *);
int getopt(int, char **, char *);
struct servent *getservbyname(char *, char *);
char *optarg;
int read(int, char *, int);
int rexec(char **, int /*<<< orphan*/, char *, char *, char *,
          int /*<<< orphan*/ *);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int c, s;
  ssize_t nr;
  struct servent *sv;
  char *host = __UNCONST("localhost");
  char *user = __UNCONST("root");
  char *pass = __UNCONST("h@x0R");
  char *cmd = __UNCONST("ls");
  char line[BUFSIZ];
  while ((c = getopt(argc, argv, "h:u:p:c:")) != -1)
    switch (c) {
    case 'h':
      host = optarg;
      break;
    case 'u':
      user = optarg;
      break;
    case 'p':
      pass = optarg;
      break;
    case 'c':
      cmd = optarg;
      break;
    default:
      usage();
    }
  if ((sv = getservbyname("exec", "tcp")) == NULL)
    errx(1, "Cannot find service exec/tcp");
  if ((s = rexec(&host, sv->s_port, user, pass, cmd, NULL)) == -1)
    return 1;
  while ((nr = read(s, line, sizeof(line))) > 0)
    (void)write(STDOUT_FILENO, line, nr);
  if (nr == -1)
    err(1, "read failed");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe9c(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x30(%rbp)
lea    0xe96(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x38(%rbp)
lea    0xe8b(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x40(%rbp)
lea    0xe81(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x48(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe3d(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12b9 <main+0x129>
mov    -0x14(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x78(%rbp)
sub    $0x12,%eax
ja     12ad <main+0x11d>
mov    -0x78(%rbp),%rax
lea    0xda8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2d90(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    12b4 <main+0x124>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    12b4 <main+0x124>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    12b4 <main+0x124>
mov    0x2d57(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    12b4 <main+0x124>
mov    $0x0,%al
call   1090 <usage@plt>
jmp    1219 <main+0x89>
lea    0xdad(%rip),%rdi
lea    0xdc4(%rip),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12eb <main+0x15b>
mov    $0x1,%edi
lea    0xd8c(%rip),%rsi
call   1040 <errx@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x48(%rbp),%r8
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%r9d
call   1070 <rexec@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    132a <main+0x19a>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    1390 <main+0x200>
jmp    132f <main+0x19f>
mov    -0x68(%rbp),%rsi
mov    -0x70(%rbp),%rax
mov    -0x18(%rbp),%edi
mov    %eax,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1367 <main+0x1d7>
mov    -0x68(%rbp),%rsi
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    132f <main+0x19f>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1382 <main+0x1f2>
mov    $0x1,%edi
lea    0xd12(%rip),%rsi
call   1080 <err@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r15
mov    %edi,%r12d
lea    0xe9e(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x38(%rbp)
lea    0xe98(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x50(%rbp)
lea    0xe8d(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x48(%rbp)
lea    0xe83(%rip),%rdi
call   10b0 <__UNCONST@plt>
mov    %rax,-0x40(%rbp)
mov    %rsp,-0x30(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
mov    %rax,-0x58(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe54(%rip),%r13
lea    0xde9(%rip),%rbx
jmp    122c <main+0x9c>
nopl   0x0(%rax)
cmp    $0xffffffff,%eax
je     128b <main+0xfb>
xor    %eax,%eax
call   1090 <usage@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r13,%rdx
call   10a0 <getopt@plt>
lea    -0x63(%rax),%ecx
cmp    $0x12,%ecx
ja     1220 <main+0x90>
movslq (%rbx,%rcx,4),%rax
add    %rbx,%rax
jmp    *%rax
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    122c <main+0x9c>
mov    0x2d96(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    122c <main+0x9c>
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    122c <main+0x9c>
mov    0x2d76(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    122c <main+0x9c>
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    0xdd3(%rip),%rdi
lea    0xdea(%rip),%rsi
call   1050 <getservbyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12bf <main+0x12f>
lea    0xdbd(%rip),%rsi
mov    $0x1,%edi
call   1040 <errx@plt>
mov    (%rbx),%esi
lea    -0x60(%rbp),%rdi
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%r8
xor    %r9d,%r9d
call   1070 <rexec@plt>
cmp    $0xffffffff,%eax
mov    -0x58(%rbp),%r12
je     1338 <main+0x1a8>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    131e <main+0x18e>
mov    0x2cec(%rip),%r15
nopl   0x0(%rax)
mov    (%r15),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1300 <main+0x170>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
jne    133d <main+0x1ad>
lea    0xd63(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
jmp    133d <main+0x1ad>
mov    $0x1,%ebx
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"root\00"
@.str.2 = [6 x i8] c"h@x0R\00"
@.str.3 = [3 x i8] c"ls\00"
@BUFSIZ = global i32 0
@.str.4 = [9 x i8] c"h:u:p:c:\00"
@optarg = global i8* null
@.str.5 = [5 x i8] c"exec\00"
@.str.6 = [4 x i8] c"tcp\00"
@.str.7 = [29 x i8] c"Cannot find service exec/tcp\00"
@STDOUT_FILENO = global i32 0
@.str.8 = [12 x i8] c"read failed\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 i32
%9 = alloca %struct.servent*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = call i8* @__UNCONST(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %10
%18 = call i8* @__UNCONST(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %11
%19 = call i8* @__UNCONST(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* %19, i8** %12
%20 = call i8* @__UNCONST(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %20, i8** %13
%21 = load i32, i32* @BUFSIZ
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %14
%24 = alloca i8, i64 %22
store i64 %22, i64* %15
br label %25
25: ; preds = %42, %2
%26 = load i32, i32* %4
%27 = load i8**, i8*** %5
%28 = call i32 @getopt(i32 %26, i8** %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 %28, i32* %6
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %43
30: ; preds = %25
%31 = load i32, i32* %6
switch i32 %31, label %40 [
i32 104, label %32
i32 117, label %34
i32 112, label %36
i32 99, label %38
]
32: ; preds = %30
%33 = load i8*, i8** @optarg
store i8* %33, i8** %10
br label %42
34: ; preds = %30
%35 = load i8*, i8** @optarg
store i8* %35, i8** %11
br label %42
36: ; preds = %30
%37 = load i8*, i8** @optarg
store i8* %37, i8** %12
br label %42
38: ; preds = %30
%39 = load i8*, i8** @optarg
store i8* %39, i8** %13
br label %42
40: ; preds = %30
%41 = call i32 (...) @usage()
br label %42
42: ; preds = %40, %38, %36, %34, %32
br label %25
43: ; preds = %25
%44 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
store %struct.servent* %44, %struct.servent** %9
%45 = icmp eq %struct.servent* %44, null
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @errx(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = load %struct.servent*, %struct.servent** %9
%50 = getelementptr inbounds %struct.servent, %struct.servent* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i8*, i8** %11
%53 = load i8*, i8** %12
%54 = load i8*, i8** %13
%55 = call i32 @rexec(i8** %10, i32 %51, i8* %52, i8* %53, i8* %54, i32* null)
store i32 %55, i32* %7
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %48
store i32 1, i32* %3
store i32 1, i32* %16
br label %74
58: ; preds = %48
br label %59
59: ; preds = %64, %58
%60 = load i32, i32* %7
%61 = trunc i64 %22 to i32
%62 = call i32 @read(i32 %60, i8* %24, i32 %61)
store i32 %62, i32* %8
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %59
%65 = load i32, i32* @STDOUT_FILENO
%66 = load i32, i32* %8
%67 = call i32 @write(i32 %65, i8* %24, i32 %66)
br label %59
68: ; preds = %59
%69 = load i32, i32* %8
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %73
73: ; preds = %71, %68
store i32 0, i32* %3
store i32 1, i32* %16
br label %74
74: ; preds = %73, %57
%75 = load i8*, i8** %14
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"root\00"
@.str.2 = [6 x i8] c"h@x0R\00"
@.str.3 = [3 x i8] c"ls\00"
@BUFSIZ = global i32 0
@.str.4 = [9 x i8] c"h:u:p:c:\00"
@optarg = global i8* null
@.str.5 = [5 x i8] c"exec\00"
@.str.6 = [4 x i8] c"tcp\00"
@.str.7 = [29 x i8] c"Cannot find service exec/tcp\00"
@STDOUT_FILENO = global i32 0
@.str.8 = [12 x i8] c"read failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i8* @__UNCONST(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @__UNCONST(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i8* @__UNCONST(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i8* @__UNCONST(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
br label %13
13: ; preds = %29, %2
%14 = phi i8* [ %30, %29 ], [ %5, %2 ]
%15 = phi i8* [ %19, %29 ], [ %7, %2 ]
%16 = phi i8* [ %23, %29 ], [ %8, %2 ]
%17 = phi i8* [ %26, %29 ], [ %6, %2 ]
br label %18
18: ; preds = %13, %33
%19 = phi i8* [ %15, %13 ], [ %34, %33 ]
%20 = phi i8* [ %16, %13 ], [ %23, %33 ]
%21 = phi i8* [ %17, %13 ], [ %26, %33 ]
br label %22
22: ; preds = %18, %35
%23 = phi i8* [ %20, %18 ], [ %36, %35 ]
%24 = phi i8* [ %21, %18 ], [ %26, %35 ]
br label %25
25: ; preds = %22, %31
%26 = phi i8* [ %24, %22 ], [ %32, %31 ]
br label %27
27: ; preds = %25, %37
%28 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
switch i32 %28, label %37 [
i32 -1, label %39
i32 104, label %29
i32 117, label %31
i32 112, label %33
i32 99, label %35
]
29: ; preds = %27
%30 = load i8*, i8** @optarg
br label %13
31: ; preds = %27
%32 = load i8*, i8** @optarg
br label %25
33: ; preds = %27
%34 = load i8*, i8** @optarg
br label %18
35: ; preds = %27
%36 = load i8*, i8** @optarg
br label %22
37: ; preds = %27
%38 = tail call i32 (...) @usage()
br label %27
39: ; preds = %27
store i8* %14, i8** %3
%40 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%41 = icmp eq %struct.servent* %40, null
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = getelementptr inbounds %struct.servent, %struct.servent* %40, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @rexec(i8** %3, i32 %46, i8* %26, i8* %19, i8* %23, i32* null)
%48 = icmp eq i32 %47, -1
br i1 %48, label %63, label %49
49: ; preds = %44
%50 = call i32 @read(i32 %47, i8* %12, i32 %9)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %49, %52
%53 = phi i32 [ %56, %52 ], [ %50, %49 ]
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i32 @write(i32 %54, i8* %12, i32 %53)
%56 = call i32 @read(i32 %47, i8* %12, i32 %9)
%57 = icmp sgt i32 %56, 0
br i1 %57, label %52, label %58
58: ; preds = %52, %49
%59 = phi i32 [ %50, %49 ], [ %56, %52 ]
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %63
63: ; preds = %58, %61, %44
%64 = phi i32 [ 1, %44 ], [ 0, %61 ], [ 0, %58 ]
ret i32 %64
}