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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_SEQPACKET;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit_with_failure(char *);
int /*<<< orphan*/ init_memtrack_server_addr(struct sockaddr_un *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *const);
int /*<<< orphan*/ puts(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char const *const, scalar_t__, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char const *const);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Usage: %s <pid>\n", argv[0]);
    return EXIT_FAILURE;
  }
  const char *const pid = argv[1];
  printf("Requesting memtrack dump for pid %s\n", pid);
  int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  if (sock < 0)
    exit_with_failure("socket");
  struct sockaddr_un server_addr;
  init_memtrack_server_addr(&server_addr);
  if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)))
    exit_with_failure("connect");
  if (send(sock, pid, strlen(pid) + 1, 0) < 0)
    exit_with_failure("send");
  char buf[4096];
  memset(buf, 0, sizeof(buf));
  if (recv(sock, buf, sizeof(buf), 0) <= 0)
    exit_with_failure("recv");
  puts(buf);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2f(%rip),%rdi
call   1090 <printf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    130a <main+0x16a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe13(%rip),%rdi
call   1090 <printf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1235 <main+0x95>
lea    0xe06(%rip),%rdi
call   1040 <exit_with_failure@plt>
lea    -0x20(%rbp),%rdi
call   1050 <init_memtrack_server_addr@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
je     1265 <main+0xc5>
lea    0xddd(%rip),%rdi
call   1040 <exit_with_failure@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x102c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x102c(%rbp),%edi
mov    -0x1028(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0x0,%rax
jge    12b3 <main+0x113>
lea    0xd97(%rip),%rdi
call   1040 <exit_with_failure@plt>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   10a0 <memset@plt>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jg     12f2 <main+0x152>
lea    0xd5d(%rip),%rdi
call   1040 <exit_with_failure@plt>
lea    -0x1020(%rbp),%rdi
call   1060 <puts@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1010,%rsp
cmp    $0x1,%edi
jg     11cc <main+0x2c>
mov    (%rsi),%rsi
lea    0xe45(%rip),%rdi
call   1090 <printf@plt>
mov    0x2e11(%rip),%rax
jmp    12a7 <main+0x107>
mov    0x8(%rsi),%r14
lea    0xe3a(%rip),%rdi
mov    %r14,%rsi
call   1090 <printf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    120a <main+0x6a>
lea    0xe31(%rip),%rdi
call   1040 <exit_with_failure@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1050 <init_memtrack_server_addr@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   10b0 <connect@plt>
test   %rax,%rax
je     1237 <main+0x97>
lea    0xe0b(%rip),%rdi
call   1040 <exit_with_failure@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
call   1080 <send@plt>
test   %rax,%rax
jns    1260 <main+0xc0>
lea    0xdea(%rip),%rdi
call   1040 <exit_with_failure@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   10a0 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jg     1296 <main+0xf6>
lea    0xdb9(%rip),%rdi
call   1040 <exit_with_failure@plt>
lea    0x10(%rsp),%rdi
call   1060 <puts@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"Usage: %s <pid>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [37 x i8] c"Requesting memtrack dump for pid %s\0A\00"
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [5 x i8] c"send\00"
@.str.5 = [5 x i8] c"recv\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
%9 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %3
br label %62
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @AF_UNIX
%25 = load i32, i32* @SOCK_SEQPACKET
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = call i32 @exit_with_failure(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %18
%32 = call i32 @init_memtrack_server_addr(%struct.sockaddr_un* %8)
%33 = load i32, i32* %7
%34 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 4)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @exit_with_failure(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %31
%40 = load i32, i32* %7
%41 = load i8*, i8** %6
%42 = load i8*, i8** %6
%43 = call i64 @strlen(i8* %42)
%44 = add nsw i64 %43, 1
%45 = call i64 @send(i32 %40, i8* %41, i64 %44, i32 0)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %39
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%51 = call i32 @memset(i8* %50, i32 0, i32 4096)
%52 = load i32, i32* %7
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%54 = call i64 @recv(i32 %52, i8* %53, i32 4096, i32 0)
%55 = icmp sle i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %49
%57 = call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %56, %49
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%60 = call i32 @puts(i8* %59)
%61 = load i32, i32* @EXIT_SUCCESS
store i32 %61, i32* %3
br label %62
62: ; preds = %58, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"Usage: %s <pid>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [37 x i8] c"Requesting memtrack dump for pid %s\0A\00"
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [5 x i8] c"send\00"
@.str.5 = [5 x i8] c"recv\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [4096 x i8]
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
br label %45
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @AF_UNIX
%15 = load i32, i32* @SOCK_SEQPACKET
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @exit_with_failure(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %10
%21 = bitcast %struct.sockaddr_un* %3 to i8*
%22 = call i32 @init_memtrack_server_addr(%struct.sockaddr_un* %3)
%23 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%24 = call i64 @connect(i32 %16, %struct.sockaddr* %23, i32 4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %20
%27 = call i32 @exit_with_failure(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = call i64 @strlen(i8* dereferenceable(1) %12)
%30 = add nsw i64 %29, 1
%31 = call i64 @send(i32 %16, i8* %12, i64 %30, i32 0)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 4096)
%38 = call i64 @recv(i32 %16, i8* %36, i32 4096, i32 0)
%39 = icmp slt i64 %38, 1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = call i32 @puts(i8* %36)
%44 = load i32, i32* @EXIT_SUCCESS
br label %45
45: ; preds = %42, %6
%46 = phi i32 [ %9, %6 ], [ %44, %42 ]
ret i32 %46
}