#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 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int read(int, unsigned char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, unsigned char *, int);
int main(void) {
  int s, l;
  unsigned long addr = 0x0100007f11110002;
  unsigned char buf[1024];
  if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    return -2;
  if (connect(s, (struct sockaddr *)&addr, 16) < 0)
    return -3;
  while (1) {
    if ((l = read(s, buf, 1024)) <= 0)
      break;
    write(1, buf, l);
    if (l < 1024)
      break;
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movabs $0x100007f11110002,%rax
mov    %rax,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a8 <main+0x58>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1233 <main+0xe3>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11cf <main+0x7f>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1233 <main+0xe3>
jmp    11d4 <main+0x84>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     11f9 <main+0xa9>
jmp    1224 <main+0xd4>
lea    -0x420(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmpl   $0x400,-0xc(%rbp)
jge    121f <main+0xcf>
jmp    1224 <main+0xd4>
jmp    11d4 <main+0x84>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
movabs $0x100007f11110002,%rax
mov    %rax,0x8(%rsp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11e7 <main+0x97>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11ee <main+0x9e>
lea    0x10(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11dc <main+0x8c>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x3ff,%ebp
ja     11b0 <main+0x60>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    11f3 <main+0xa3>
mov    $0xfffffffe,%eax
jmp    11f3 <main+0xa3>
mov    $0xfffffffd,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca [1024 x i8]
store i32 0, i32* %1
store i64 72058139785101314, i64* %4
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -2, i32* %1
br label %36
12: ; preds = %0
%13 = load i32, i32* %2
%14 = bitcast i64* %4 to %struct.sockaddr*
%15 = call i64 @connect(i32 %13, %struct.sockaddr* %14, i32 16)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -3, i32* %1
br label %36
18: ; preds = %12
br label %19
19: ; preds = %18, %32
%20 = load i32, i32* %2
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 1024)
store i32 %22, i32* %3
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %33
25: ; preds = %19
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = load i32, i32* %3
%28 = call i32 @write(i32 1, i8* %26, i32 %27)
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 1024
br i1 %30, label %31, label %32
31: ; preds = %25
br label %33
32: ; preds = %25
br label %19
33: ; preds = %31, %24
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
store i32 0, i32* %1
br label %36
36: ; preds = %33, %17, %11
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @main() {
%1 = alloca i64
%2 = alloca [1024 x i8]
%3 = bitcast i64* %1 to i8*
store i64 72058139785101314, i64* %1
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %0
%11 = bitcast i64* %1 to %struct.sockaddr*
%12 = call i64 @connect(i32 %8, %struct.sockaddr* %11, i32 16)
%13 = icmp slt i64 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %10, %17
%15 = call i32 @read(i32 %8, i8* %4, i32 1024)
%16 = icmp slt i32 %15, 1
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i32 @write(i32 1, i8* %4, i32 %15)
%19 = icmp ult i32 %15, 1024
br i1 %19, label %20, label %14
20: ; preds = %17, %14
%21 = call i32 @close(i32 %8)
br label %22
22: ; preds = %10, %0, %20
%23 = phi i32 [ 0, %20 ], [ -2, %0 ], [ -3, %10 ]
ret i32 %23
}