#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 sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ command;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
scalar_t__ write(int, char const *, int);
int main(int argc, char **argv) {
  static const char command[] = "show all\n";
  char buf[256];
  int l;
  int s;
  struct sockaddr_un addr;
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s < 0) {
    perror("socket");
    return 1;
  }
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, "/var/run/miniupnpd.ctl", sizeof(addr.sun_path));
  if (connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
    perror("connect");
    close(s);
    return 1;
  }
  printf("Connected.\n");
  if (write(s, command, sizeof(command)) < 0) {
    perror("write");
    close(s);
    return 1;
  }
  for (;;) {
    l = read(s, buf, sizeof(buf));
    if (l < 0) {
      perror("read");
      break;
    }
    if (l == 0)
      break;
    fflush(stdout);
    if (write(fileno(stdout), buf, l) < 0) {
      perror("error writing to stdout");
    }
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jge    11fd <main+0x5d>
lea    0xe1e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1366 <main+0x1c6>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x120(%rbp),%edi
lea    0xdf8(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x118(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1267 <main+0xc7>
lea    0xddd(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x118(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1366 <main+0x1c6>
lea    0xdc2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x118(%rbp),%edi
lea    0xd80(%rip),%rsi
mov    $0xa,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12b7 <main+0x117>
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x118(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1366 <main+0x1c6>
jmp    12bc <main+0x11c>
mov    -0x118(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    12f7 <main+0x157>
lea    0xd55(%rip),%rdi
call   10a0 <perror@plt>
jmp    1354 <main+0x1b4>
cmpl   $0x0,-0x114(%rbp)
jne    1309 <main+0x169>
jmp    1354 <main+0x1b4>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    134f <main+0x1af>
lea    0xcfd(%rip),%rdi
call   10a0 <perror@plt>
jmp    12bc <main+0x11c>
mov    -0x118(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    0x2e32(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12cd <main+0x12d>
mov    %eax,%r15d
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe31(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0x8,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     12e0 <main+0x140>
lea    0xe47(%rip),%rdi
call   1040 <puts@plt>
lea    0xde8(%rip),%rsi
mov    %r15d,%edi
mov    $0xa,%edx
call   1050 <write@plt>
test   %rax,%rax
js     12e9 <main+0x149>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x100,%edx
call   1070 <read@plt>
test   %eax,%eax
js     12a7 <main+0x107>
mov    %eax,%ebp
mov    0x2d83(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xde2(%rip),%r14
jmp    1276 <main+0xd6>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12a7 <main+0x107>
test   %ebp,%ebp
je     12b3 <main+0x113>
mov    (%r12),%edi
call   1090 <fflush@plt>
mov    (%r12),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
test   %rax,%rax
jns    1260 <main+0xc0>
mov    %r14,%rdi
call   10a0 <perror@plt>
jmp    1260 <main+0xc0>
lea    0xd88(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd36(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    12bd <main+0x11d>
lea    0xd41(%rip),%rdi
jmp    12f0 <main+0x150>
lea    0xd40(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    $0x1,%eax
jmp    12bd <main+0x11d>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@main.command = internal constant [10 x i8] c"show all\0A\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [23 x i8] c"/var/run/miniupnpd.ctl\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [12 x i8] c"Connected.\0A\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [5 x i8] c"read\00"
@stdout = global i32 0
@.str.6 = [24 x i8] c"error writing to stdout\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %68
17: ; preds = %2
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strncpy(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 4)
%23 = load i32, i32* %8
%24 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 1, i32* %3
br label %68
31: ; preds = %17
%32 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* %8
%34 = call i64 @write(i32 %33, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @main.command, i64 0, i64 0), i32 10)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
store i32 1, i32* %3
br label %68
40: ; preds = %31
br label %41
41: ; preds = %64, %40
%42 = load i32, i32* %8
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 256)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %65
49: ; preds = %41
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %49
br label %65
53: ; preds = %49
%54 = load i32, i32* @stdout
%55 = call i32 @fflush(i32 %54)
%56 = load i32, i32* @stdout
%57 = call i32 @fileno(i32 %56)
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%59 = load i32, i32* %7
%60 = call i64 @write(i32 %57, i8* %58, i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %53
%63 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %64
64: ; preds = %62, %53
br label %41
65: ; preds = %52, %47
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 0, i32* %3
br label %68
68: ; preds = %65, %36, %27, %15
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@main.command = internal constant [10 x i8] c"show all\0A\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [23 x i8] c"/var/run/miniupnpd.ctl\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [5 x i8] c"read\00"
@stdout = global i32 0
@.str.6 = [24 x i8] c"error writing to stdout\00"
@str = [11 x i8] c"Connected.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.sockaddr_un
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %52
13: ; preds = %2
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%18 = call i64 @connect(i32 %9, %struct.sockaddr* %17, i32 8)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @close(i32 %9)
br label %52
23: ; preds = %13
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%25 = call i64 @write(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @main.command, i64 0, i64 0), i32 10)
%26 = icmp slt i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i32 @read(i32 %9, i8* %5, i32 256)
%29 = icmp slt i32 %28, 0
br i1 %29, label %33, label %35
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @close(i32 %9)
br label %52
33: ; preds = %47, %27
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %50
35: ; preds = %27, %47
%36 = phi i32 [ %48, %47 ], [ %28, %27 ]
%37 = icmp eq i32 %36, 0
br i1 %37, label %50, label %38
38: ; preds = %35
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
%41 = load i32, i32* @stdout
%42 = call i32 @fileno(i32 %41)
%43 = call i64 @write(i32 %42, i8* %5, i32 %36)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %45, %38
%48 = call i32 @read(i32 %9, i8* %5, i32 256)
%49 = icmp slt i32 %48, 0
br i1 %49, label %33, label %35
50: ; preds = %35, %33
%51 = call i32 @close(i32 %9)
br label %52
52: ; preds = %50, %30, %20, %11
%53 = phi i32 [ 1, %11 ], [ 1, %20 ], [ 1, %30 ], [ 0, %50 ]
ret i32 %53
}