#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;
  (void)(argc);
  (void)(argv);
  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* %4
%11 = load i8**, i8*** %5
%12 = load i32, i32* @AF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %70
19: ; preds = %2
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @strncpy(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 4)
%25 = load i32, i32* %8
%26 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%27 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 8)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
store i32 1, i32* %3
br label %70
33: ; preds = %19
%34 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* %8
%36 = call i64 @write(i32 %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @main.command, i64 0, i64 0), i32 10)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
store i32 1, i32* %3
br label %70
42: ; preds = %33
br label %43
43: ; preds = %66, %42
%44 = load i32, i32* %8
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 256)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %67
51: ; preds = %43
%52 = load i32, i32* %7
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
br label %67
55: ; preds = %51
%56 = load i32, i32* @stdout
%57 = call i32 @fflush(i32 %56)
%58 = load i32, i32* @stdout
%59 = call i32 @fileno(i32 %58)
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%61 = load i32, i32* %7
%62 = call i64 @write(i32 %59, i8* %60, i32 %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %55
%65 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %64, %55
br label %43
67: ; preds = %54, %49
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
store i32 0, i32* %3
br label %70
70: ; preds = %67, %38, %29, %17
%71 = load i32, i32* %3
ret i32 %71
}

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
}