#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr1;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sock1;
  struct sockaddr_in addr1;
  unsigned short port = 5353;
  int recv_bytes;
  char buffer[512];
  sock1 = socket(AF_INET, SOCK_STREAM, 0);
  if (sock1 < 0) {
    printf("sock failed!\n");
  }
  memset(&addr1, 0, sizeof(addr1));
  addr1.sin_family = AF_INET;
  addr1.sin_addr.s_addr = inet_addr("127.0.0.1");
  addr1.sin_port = htons(port);
  if (connect(sock1, (struct sockaddr *)(&addr1), sizeof(struct sockaddr)) <
      0) {
    printf("connect failed!\n");
    close(sock1);
  }
  recv_bytes = read(sock1, buffer, 512);
  if (recv_bytes < 0) {
    printf("read error!\n");
    close(sock1);
  } else if (recv_bytes < 512) {
    buffer[recv_bytes] = '\0';
    printf("%s", buffer);
    close(sock1);
  } else {
    printf("buffer full!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movw   $0x14e9,-0x22(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d3 <main+0x53>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe18(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
movzwl -0x22(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    123b <main+0xbb>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1277 <main+0xf7>
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12c5 <main+0x145>
cmpl   $0x200,-0x28(%rbp)
jge    12b2 <main+0x132>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x230(%rbp,%rax,1)
lea    -0x230(%rbp),%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12c0 <main+0x140>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c5 <main+0x145>
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    0x2e45(%rip),%r15
mov    (%r15),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c8 <main+0x38>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
lea    0xe18(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x14e9,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    1225 <main+0x95>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1080 <read@plt>
test   %eax,%eax
js     125d <main+0xcd>
cmp    $0x1ff,%eax
ja     1272 <main+0xe2>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdbb(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1269 <main+0xd9>
lea    0xdb6(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    127e <main+0xee>
lea    0xd94(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"sock failed!\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [17 x i8] c"connect failed!\0A\00"
@.str.3 = [13 x i8] c"read error!\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [14 x i8] c"buffer full!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i16
%9 = alloca i32
%10 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i16 5353, i16* %8
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i16, i16* %8
%26 = call i32 @htons(i16 zeroext %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %18
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %32, %18
%37 = load i32, i32* %6
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 512)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
br label %60
46: ; preds = %36
%47 = load i32, i32* %9
%48 = icmp slt i32 %47, 512
br i1 %48, label %49, label %57
49: ; preds = %46
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %51
store i8 0, i8* %52
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
br label %59
57: ; preds = %46
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
br label %60
60: ; preds = %59, %42
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.4 = [3 x i8] c"%s\00"
@str = [13 x i8] c"buffer full!\00"
@str.6 = [12 x i8] c"read error!\00"
@str.7 = [16 x i8] c"connect failed!\00"
@str.8 = [13 x i8] c"sock failed!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%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 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = call i32 @htons(i16 zeroext 5353)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i64 @connect(i32 %9, %struct.sockaddr* %21, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
%26 = call i32 @close(i32 %9)
br label %27
27: ; preds = %24, %13
%28 = call i32 @read(i32 %9, i8* %6, i32 512)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%32 = call i32 @close(i32 %9)
br label %42
33: ; preds = %27
%34 = icmp ult i32 %28, 512
br i1 %34, label %35, label %40
35: ; preds = %33
%36 = zext i32 %28 to i64
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%39 = call i32 @close(i32 %9)
br label %42
40: ; preds = %33
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %42
42: ; preds = %35, %40, %30
ret i32 0
}