#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int uint32_t;
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr const *, int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(scalar_t__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
scalar_t__ write(int, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int sock;
  sock = socket(PF_INET, SOCK_DGRAM, 0);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(2000);
  addr.sin_addr.s_addr = 0;
  if (argc == 1) {
    printf("%s [dest hostname]\n", argv[0]);
    exit(0);
  }
  struct hostent *host = gethostbyname(argv[1]);
  memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
  int result;
  result = connect(sock, (const struct sockaddr *)&addr, sizeof(addr));
  if (result)
    perror("connect");
  uint8_t buf[10240];
  memset(buf, 0x66, 20);
  memset(buf + 20, 0x67, 1004);
  for (uint32_t i = 0; i < 1000; i++) {
    ssize_t n;
    n = write(sock, buf, 1024);
    if (n < 0) {
      printf("write: %s %d\n", strerror(errno), errno);
      break;
    }
  }
  printf("did write all buffers\n");
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    123c <main+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1090 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10c0 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1290 <main+0xd0>
lea    0xd89(%rip),%rdi
call   10a0 <perror@plt>
lea    -0xa040(%rbp),%rdi
mov    $0x66,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
lea    -0xa040(%rbp),%rdi
add    $0x50,%rdi
mov    $0x67,%esi
mov    $0x3ec,%edx
call   1060 <memset@plt>
movl   $0x0,-0xa044(%rbp)
cmpl   $0x3e8,-0xa044(%rbp)
jge    1349 <main+0x189>
mov    -0x14(%rbp),%edi
lea    -0xa040(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %rax,-0xa050(%rbp)
cmpq   $0x0,-0xa050(%rbp)
jge    1330 <main+0x170>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cb5(%rip),%rax
mov    (%rax),%edx
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1349 <main+0x189>
jmp    1335 <main+0x175>
mov    -0xa044(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa044(%rbp)
jmp    12ca <main+0x10a>
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xa050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa018,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r15d
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x7d0,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
cmp    $0x1,%ebx
je     1302 <main+0x132>
mov    0x8(%r14),%rdi
call   1090 <gethostbyname@plt>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <memcpy@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   10d0 <connect@plt>
test   %eax,%eax
jne    12f1 <main+0x121>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x66,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
lea    0x60(%rsp),%rdi
mov    $0x67,%esi
mov    $0x3ec,%edx
call   1070 <memset@plt>
mov    $0x3e8,%ebp
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
test   %rax,%rax
js     12ac <main+0xdc>
add    $0xffffffff,%ebp
jne    1290 <main+0xc0>
jmp    12cd <main+0xfd>
mov    0x2d1d(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd59(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd56(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xa018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1c(%rip),%rdi
call   10b0 <perror@plt>
jmp    125e <main+0x8e>
mov    (%r14),%rsi
lea    0xcf4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@.str = [20 x i8] c"%s [dest hostname]\0A\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [14 x i8] c"write: %s %d\0A\00"
@errno = global i32 0
@.str.3 = [23 x i8] c"did write all buffers\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 %struct.hostent*
%9 = alloca i32
%10 = alloca [10240 x i32]
%11 = alloca i32
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 2000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i64 0, i64* %21
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %2
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call %struct.hostent* @gethostbyname(i8* %33)
store %struct.hostent* %34, %struct.hostent** %8
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = load %struct.hostent*, %struct.hostent** %8
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 1
%39 = load i32*, i32** %38
%40 = getelementptr inbounds i32, i32* %39, i64 0
%41 = load i32, i32* %40
%42 = load %struct.hostent*, %struct.hostent** %8
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @memcpy(i64* %36, i32 %41, i32 %44)
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%48 = call i32 @connect(i32 %46, %struct.sockaddr* %47, i32 16)
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %30
%52 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %53
53: ; preds = %51, %30
%54 = getelementptr inbounds [10240 x i32], [10240 x i32]* %10, i64 0, i64 0
%55 = call i32 @memset(i32* %54, i32 102, i32 20)
%56 = getelementptr inbounds [10240 x i32], [10240 x i32]* %10, i64 0, i64 0
%57 = getelementptr inbounds i32, i32* %56, i64 20
%58 = call i32 @memset(i32* %57, i32 103, i32 1004)
store i32 0, i32* %11
br label %59
59: ; preds = %74, %53
%60 = load i32, i32* %11
%61 = icmp slt i32 %60, 1000
br i1 %61, label %62, label %77
62: ; preds = %59
%63 = load i32, i32* %6
%64 = getelementptr inbounds [10240 x i32], [10240 x i32]* %10, i64 0, i64 0
%65 = call i64 @write(i32 %63, i32* %64, i32 1024)
store i64 %65, i64* %12
%66 = load i64, i64* %12
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %62
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = load i32, i32* @errno
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %70, i32 %71)
br label %77
73: ; preds = %62
br label %74
74: ; preds = %73
%75 = load i32, i32* %11
%76 = add nsw i32 %75, 1
store i32 %76, i32* %11
br label %59
77: ; preds = %68, %59
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@.str = [20 x i8] c"%s [dest hostname]\0A\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [14 x i8] c"write: %s %d\0A\00"
@errno = global i32 0
@str = [22 x i8] c"did write all buffers\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [10240 x i32]
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 2000)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 0, i64* %13
%14 = icmp eq i32 %0, 1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8*, i8** %1
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call %struct.hostent* @gethostbyname(i8* %21)
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 1
%24 = load i32*, i32** %23
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memcpy(i64* %13, i32 %25, i32 %27)
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %7, %struct.sockaddr* %29, i32 16)
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %19
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %19
%35 = bitcast [10240 x i32]* %4 to i8*
%36 = getelementptr inbounds [10240 x i32], [10240 x i32]* %4, i64 0, i64 0
%37 = call i32 @memset(i32* %36, i32 102, i32 20)
%38 = getelementptr inbounds [10240 x i32], [10240 x i32]* %4, i64 0, i64 20
%39 = call i32 @memset(i32* %38, i32 103, i32 1004)
br label %43
40: ; preds = %43
%41 = add nuw nsw i32 %44, 1
%42 = icmp eq i32 %41, 1000
br i1 %42, label %52, label %43
43: ; preds = %34, %40
%44 = phi i32 [ 0, %34 ], [ %41, %40 ]
%45 = call i64 @write(i32 %7, i32* %36, i32 1024)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %40
47: ; preds = %43
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = load i32, i32* @errno
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %49, i32 %50)
br label %52
52: ; preds = %40, %47
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%54 = call i32 @close(i32 %7)
ret i32 0
}