#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);
int /*<<< orphan*/ 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");
  write(s, command, sizeof(command));
  for (;;) {
    l = read(s, buf, sizeof(buf));
    if (l < 0) {
      perror("read");
      break;
    }
    if (l == 0)
      break;
    fflush(stdout);
    write(fileno(stdout), buf, l);
  }
  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    131e <main+0x17e>
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    131e <main+0x17e>
lea    0xdc2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x118(%rbp),%edi
lea    0xd80(%rip),%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    -0x118(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    12c5 <main+0x125>
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
jmp    130c <main+0x16c>
cmpl   $0x0,-0x114(%rbp)
jne    12d7 <main+0x137>
jmp    130c <main+0x16c>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1040 <write@plt>
jmp    128a <main+0xea>
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   %rbx
sub    $0x118,%rsp
mov    0x2e34(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12aa <main+0x10a>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe33(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     12bd <main+0x11d>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
lea    0xdea(%rip),%rsi
mov    %r14d,%edi
mov    $0xa,%edx
call   1050 <write@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x100,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1286 <main+0xe6>
mov    %eax,%ebp
mov    0x2d8e(%rip),%r15
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
test   %ebp,%ebp
je     1292 <main+0xf2>
mov    (%r15),%edi
call   1090 <fflush@plt>
mov    (%r15),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1250 <main+0xb0>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd59(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    129c <main+0xfc>
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    $0x1,%eax
jmp    129c <main+0xfc>

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 = [5 x i8] c"read\00"
@stdout = global i32 0
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 %58
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 %58
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 i32 @write(i32 %33, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @main.command, i64 0, i64 0), i32 10)
br label %35
35: ; preds = %47, %31
%36 = load i32, i32* %8
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 256)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %55
43: ; preds = %35
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
br label %55
47: ; preds = %43
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
%50 = load i32, i32* @stdout
%51 = call i32 @fileno(i32 %50)
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%53 = load i32, i32* %7
%54 = call i32 @write(i32 %51, i8* %52, i32 %53)
br label %35
55: ; preds = %46, %41
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
store i32 0, i32* %3
br label %58
58: ; preds = %55, %27, %15
%59 = load i32, i32* %3
ret i32 %59
}

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 = [5 x i8] c"read\00"
@stdout = global i32 0
@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 %43
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 %43
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 i32 @write(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @main.command, i64 0, i64 0), i32 10)
%26 = call i32 @read(i32 %9, i8* %5, i32 256)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %33, %23
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %41
30: ; preds = %23, %33
%31 = phi i32 [ %39, %33 ], [ %26, %23 ]
%32 = icmp eq i32 %31, 0
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
%36 = load i32, i32* @stdout
%37 = call i32 @fileno(i32 %36)
%38 = call i32 @write(i32 %37, i8* %5, i32 %31)
%39 = call i32 @read(i32 %9, i8* %5, i32 256)
%40 = icmp slt i32 %39, 0
br i1 %40, label %28, label %30
41: ; preds = %30, %28
%42 = call i32 @close(i32 %9)
br label %43
43: ; preds = %41, %20, %11
%44 = phi i32 [ 1, %11 ], [ 1, %20 ], [ 0, %41 ]
ret i32 %44
}