#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
char *calloc(int, unsigned int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
size_t recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
char *strerror(int);
int main(int argc, char *argv[]) {
  char msg[512];
  char *buf = NULL;
  char *tmp = NULL;
  int s = 0;
  unsigned int msgSize = 0;
  size_t bytes = 0;
  int result = 0;
  buf = calloc(1, 512);
  memset(&msg[0], 0x0, 512);
  struct sockaddr_un addr;
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, "/var/run/lldpd.sock");
  result = connect(s, (struct sockaddr *)&addr, sizeof(addr));
  if (result < 0) {
    printf("\n%s couldn't connect to the OpenLLDP transport socket.  Is lldpd "
           "running?\n",
           argv[0]);
    goto cleanup;
  }
  while ((bytes = recv(s, msg, 512, 0))) {
    if (bytes > 0) {
      tmp = calloc(1, msgSize + bytes + 1);
      if (buf != NULL) {
        memcpy(tmp, buf, msgSize);
        free(buf);
        buf = NULL;
      }
      memcpy(&tmp[msgSize], msg, bytes);
      msgSize += bytes;
      buf = tmp;
      tmp = NULL;
    } else {
      if (0) {
        printf("Error reading %d bytes: %d:%s\n", 512, errno, strerror(errno));
      }
    }
    if (0) {
      printf("Read %d bytes.  Total size is now: %d\n", (int)bytes, msgSize);
      printf("Buffer is: 0x%08X and Temporary Buffer is 0x%08X.\n", (int)&buf,
             (int)&tmp);
    }
  }
  if (buf != NULL) {
    printf("%s\n", buf);
  }
cleanup:
  if (buf != NULL) {
    free(buf);
    msgSize = 0;
    buf = NULL;
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x218(%rbp)
movq   $0x0,-0x220(%rbp)
movl   $0x0,-0x224(%rbp)
movl   $0x0,-0x228(%rbp)
movq   $0x0,-0x230(%rbp)
movl   $0x0,-0x234(%rbp)
mov    $0x1,%edi
mov    $0x200,%esi
call   1090 <calloc@plt>
mov    %rax,-0x218(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x224(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x23c(%rbp)
mov    -0x240(%rbp),%edi
lea    0xda4(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x224(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <connect@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jge    12a5 <main+0x105>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13cf <main+0x22f>
jmp    12aa <main+0x10a>
mov    -0x224(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rax,-0x230(%rbp)
cmp    $0x0,%rax
je     13a7 <main+0x207>
cmpq   $0x0,-0x230(%rbp)
jbe    139d <main+0x1fd>
mov    -0x228(%rbp),%eax
add    -0x230(%rbp),%rax
add    $0x1,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   1090 <calloc@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     1346 <main+0x1a6>
mov    -0x220(%rbp),%rdi
mov    -0x218(%rbp),%rsi
mov    -0x228(%rbp),%eax
mov    %eax,%edx
call   10a0 <memcpy@plt>
mov    -0x218(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x218(%rbp)
mov    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%eax
add    %rax,%rdi
lea    -0x210(%rbp),%rsi
mov    -0x230(%rbp),%rdx
call   10a0 <memcpy@plt>
mov    -0x230(%rbp),%rcx
mov    -0x228(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x228(%rbp)
mov    -0x220(%rbp),%rax
mov    %rax,-0x218(%rbp)
movq   $0x0,-0x220(%rbp)
jmp    13a2 <main+0x202>
jmp    13a2 <main+0x202>
jmp    12aa <main+0x10a>
cmpq   $0x0,-0x218(%rbp)
je     13ca <main+0x22a>
mov    -0x218(%rbp),%rsi
lea    0xc9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13cf <main+0x22f>
cmpq   $0x0,-0x218(%rbp)
je     13fe <main+0x25e>
mov    -0x218(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x228(%rbp)
movq   $0x0,-0x218(%rbp)
mov    -0x224(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    $0x1,%edi
mov    $0x200,%esi
call   10a0 <calloc@plt>
mov    %rax,%r12
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1080 <memset@plt>
mov    0x2dfa(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
lea    0xdef(%rip),%rsi
call   1050 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   10c0 <connect@plt>
test   %eax,%eax
js     12c4 <main+0x114>
lea    0x10(%rsp),%rsi
mov    %ebx,0x4(%rsp)
mov    %ebx,%edi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %rax,%rax
je     12dc <main+0x12c>
mov    %rax,%rbp
xor    %r14d,%r14d
lea    0x10(%rsp),%r15
jmp    128f <main+0xdf>
nopl   0x0(%rax)
mov    %r13,%rdi
add    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   10b0 <memcpy@plt>
mov    0x4(%rsp),%edi
mov    %r15,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rax,%rbp
mov    %r13,%r12
test   %rax,%rax
je     12df <main+0x12f>
mov    %r14d,%ebx
lea    (%rbx,%rbp,1),%r14
lea    0x1(%r14),%esi
mov    $0x1,%edi
call   10a0 <calloc@plt>
mov    %rax,%r13
test   %r12,%r12
je     1260 <main+0xb0>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10b0 <memcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1260 <main+0xb0>
mov    (%r14),%rsi
lea    0xd46(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r12,%r12
jne    12f3 <main+0x143>
jmp    12fb <main+0x14b>
mov    %r12,%r13
test   %r13,%r13
mov    0x4(%rsp),%ebx
je     12fb <main+0x14b>
mov    %r13,%rdi
call   1060 <puts@plt>
mov    %r13,%r12
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"/var/run/lldpd.sock\00"
@.str.1 = [75 x i8] c"\0A%s couldn't connect to the OpenLLDP transport socket. Is lldpd running?\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
%13 = alloca %struct.sockaddr_un
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i8* null, i8** %8
store i32 0, i32* %9
store i32 0, i32* %10
store i64 0, i64* %11
store i32 0, i32* %12
%14 = call i8* @calloc(i32 1, i32 512)
store i8* %14, i8** %7
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 512)
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %9
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %13, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %13, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @strcpy(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %9
%26 = bitcast %struct.sockaddr_un* %13 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 8)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %2
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %85
35: ; preds = %2
br label %36
36: ; preds = %77, %35
%37 = load i32, i32* %9
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%39 = call i64 @recv(i32 %37, i8* %38, i32 512, i32 0)
store i64 %39, i64* %11
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %78
41: ; preds = %36
%42 = load i64, i64* %11
%43 = icmp ugt i64 %42, 0
br i1 %43, label %44, label %76
44: ; preds = %41
%45 = load i32, i32* %10
%46 = zext i32 %45 to i64
%47 = load i64, i64* %11
%48 = add i64 %46, %47
%49 = add i64 %48, 1
%50 = trunc i64 %49 to i32
%51 = call i8* @calloc(i32 1, i32 %50)
store i8* %51, i8** %8
%52 = load i8*, i8** %7
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %62
54: ; preds = %44
%55 = load i8*, i8** %8
%56 = load i8*, i8** %7
%57 = load i32, i32* %10
%58 = zext i32 %57 to i64
%59 = call i32 @memcpy(i8* %55, i8* %56, i64 %58)
%60 = load i8*, i8** %7
%61 = call i32 @free(i8* %60)
store i8* null, i8** %7
br label %62
62: ; preds = %54, %44
%63 = load i8*, i8** %8
%64 = load i32, i32* %10
%65 = zext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%68 = load i64, i64* %11
%69 = call i32 @memcpy(i8* %66, i8* %67, i64 %68)
%70 = load i64, i64* %11
%71 = load i32, i32* %10
%72 = zext i32 %71 to i64
%73 = add i64 %72, %70
%74 = trunc i64 %73 to i32
store i32 %74, i32* %10
%75 = load i8*, i8** %8
store i8* %75, i8** %7
store i8* null, i8** %8
br label %77
76: ; preds = %41
br label %77
77: ; preds = %76, %62
br label %36
78: ; preds = %36
%79 = load i8*, i8** %7
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = load i8*, i8** %7
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %81, %78
br label %85
85: ; preds = %84, %30
%86 = load i8*, i8** %7
%87 = icmp ne i8* %86, null
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = load i8*, i8** %7
%90 = call i32 @free(i8* %89)
store i32 0, i32* %10
store i8* null, i8** %7
br label %91
91: ; preds = %88, %85
%92 = load i32, i32* %9
%93 = call i32 @close(i32 %92)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"/var/run/lldpd.sock\00"
@.str.1 = [75 x i8] c"\0A%s couldn't connect to the OpenLLDP transport socket. Is lldpd running?\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = alloca %struct.sockaddr_un
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = tail call dereferenceable_or_null(512) i8* @calloc(i32 1, i32 512)
%7 = call i32 @memset(i8* %5, i32 0, i32 512)
%8 = bitcast %struct.sockaddr_un* %4 to i8*
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %12, i32* %13
%14 = call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%16 = call i32 @connect(i32 %11, %struct.sockaddr* %15, i32 8)
%17 = icmp slt i32 %16, 0
br i1 %17, label %44, label %18
18: ; preds = %2
%19 = call i64 @recv(i32 %11, i8* %5, i32 512, i32 0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %39, label %21
21: ; preds = %18, %34
%22 = phi i64 [ %37, %34 ], [ %19, %18 ]
%23 = phi i8* [ %29, %34 ], [ %6, %18 ]
%24 = phi i64 [ %26, %34 ], [ 0, %18 ]
%25 = and i64 %24, 4294967295
%26 = add i64 %22, %25
%27 = trunc i64 %26 to i32
%28 = add i32 %27, 1
%29 = call i8* @calloc(i32 1, i32 %28)
%30 = icmp eq i8* %23, null
br i1 %30, label %34, label %31
31: ; preds = %21
%32 = call i32 @memcpy(i8* %29, i8* %23, i64 %25)
%33 = call i32 @free(i8* %23)
br label %34
34: ; preds = %31, %21
%35 = getelementptr inbounds i8, i8* %29, i64 %25
%36 = call i32 @memcpy(i8* %35, i8* %5, i64 %22)
%37 = call i64 @recv(i32 %11, i8* %5, i32 512, i32 0)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %21
39: ; preds = %34, %18
%40 = phi i8* [ %6, %18 ], [ %29, %34 ]
%41 = icmp eq i8* %40, null
br i1 %41, label %51, label %42
42: ; preds = %39
%43 = call i32 @puts(i8* %40)
br label %48
44: ; preds = %2
%45 = load i8*, i8** %1
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = icmp eq i8* %6, null
br i1 %47, label %51, label %48
48: ; preds = %42, %44
%49 = phi i8* [ %40, %42 ], [ %6, %44 ]
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %39, %48, %44
%52 = call i32 @close(i32 %11)
ret i32 0
}