#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 /*<<< 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(0, 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    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jge    11ed <main+0x5d>
lea    0xe2e(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x170>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x120(%rbp),%edi
lea    0xe08(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x118(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1257 <main+0xc7>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    -0x118(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x170>
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x118(%rbp),%edi
lea    0xd90(%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    12b5 <main+0x125>
lea    0xd91(%rip),%rdi
call   1090 <perror@plt>
jmp    12ee <main+0x15e>
cmpl   $0x0,-0x114(%rbp)
jne    12c7 <main+0x137>
jmp    12ee <main+0x15e>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
xor    %edi,%edi
call   1040 <write@plt>
jmp    127a <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    0x2e44(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1292 <main+0x102>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe43(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     12a5 <main+0x115>
lea    0xe3b(%rip),%rdi
call   1040 <puts@plt>
lea    0xdfa(%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     126e <main+0xde>
mov    %eax,%ebp
mov    0x2d9e(%rip),%r15
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
test   %ebp,%ebp
je     127a <main+0xea>
mov    (%r15),%edi
call   1080 <fflush@plt>
xor    %edi,%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    1240 <main+0xb0>
lea    0xdbb(%rip),%rdi
call   1090 <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    0xd71(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    1284 <main+0xf4>
lea    0xd7c(%rip),%rdi
call   1090 <perror@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    $0x1,%eax
jmp    1284 <main+0xf4>

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 %56
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 %56
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 %53
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 %53
47: ; preds = %43
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%51 = load i32, i32* %7
%52 = call i32 @write(i32 0, i8* %50, i32 %51)
br label %35
53: ; preds = %46, %41
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
store i32 0, i32* %3
br label %56
56: ; preds = %53, %27, %15
%57 = load i32, i32* %3
ret i32 %57
}

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 %41
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 %41
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 %39
30: ; preds = %23, %33
%31 = phi i32 [ %37, %33 ], [ %26, %23 ]
%32 = icmp eq i32 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %30
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
%36 = call i32 @write(i32 0, i8* %5, i32 %31)
%37 = call i32 @read(i32 %9, i8* %5, i32 256)
%38 = icmp slt i32 %37, 0
br i1 %38, label %28, label %30
39: ; preds = %30, %28
%40 = call i32 @close(i32 %9)
br label %41
41: ; preds = %39, %20, %11
%42 = phi i32 [ 1, %11 ], [ 1, %20 ], [ 0, %39 ]
ret i32 %42
}