#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*/ serverAddress;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
void cSocketsDemo(void) {
  int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock < 0) {
    printf("socket() failed: %d", errno);
    return;
  }
  struct sockaddr_in serverAddress;
  bzero(&serverAddress, 0);
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
  serverAddress.sin_port = htons(80);
  int error =
      connect(sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
  if (error < 0) {
    printf("connect() failed: %d", errno);
    return;
  }
  const char *request = "GET / HTTP/1.0\n\n";
  write(sock, request, strlen(request));
  size_t bytesReceived;
  do {
    char buffer[1000];
    buffer[0] = '\0';
    bytesReceived = read(sock, buffer, sizeof(buffer));
    printf("%s", buffer);
  } while (bytesReceived > 0);
  printf("\n");
  close(sock);
}


x86 assembly (clang, -O0):
<cSocketsDemo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11f4 <cSocketsDemo+0x54>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12f2 <cSocketsDemo+0x152>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
call   10a0 <bzero@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    0xe02(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0xc(%rbp)
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1261 <cSocketsDemo+0xc1>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12f2 <cSocketsDemo+0x152>
lea    0xdcb(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x41c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x41c(%rbp),%edi
mov    -0x418(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
movb   $0x0,-0x410(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3e8,%edx
call   1080 <read@plt>
mov    %rax,-0x28(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
ja     129d <cSocketsDemo+0xfd>
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cSocketsDemo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3f8,%rsp
mov    0x2e24(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     129e <cSocketsDemo+0xee>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0xe10(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x50,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %eax,%eax
js     12c2 <cSocketsDemo+0x112>
lea    0xdfa(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0x10(%rsp),%rbx
lea    0xdea(%rip),%r14
nopw   0x0(%rax,%rax,1)
movb   $0x0,0x10(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
call   1090 <read@plt>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
test   %rbp,%rbp
jne    1260 <cSocketsDemo+0xb0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
call   1080 <close@plt>
jmp    12d9 <cSocketsDemo+0x129>
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
lea    0xd52(%rip),%rdi
xor    %eax,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <printf@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
lea    0xd4c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x3f8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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
@IPPROTO_TCP = global i32 0
@.str = [20 x i8] c"socket() failed: %d\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [21 x i8] c"connect() failed: %d\00"
@.str.3 = [17 x i8] c"GET / HTTP/1.0\0A\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c"\0A\00"
define void @cSocketsDemo() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [1000 x i8]
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %1
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i32, i32* @errno
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %53
16: ; preds = %0
%17 = call i32 @bzero(%struct.sockaddr_in* %2, i32 0)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = call i32 @htons(i32 80)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %1
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %16
%31 = load i32, i32* @errno
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %53
33: ; preds = %16
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8** %4
%34 = load i32, i32* %1
%35 = load i8*, i8** %4
%36 = load i8*, i8** %4
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
br label %39
39: ; preds = %46, %33
%40 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
store i8 0, i8* %40
%41 = load i32, i32* %1
%42 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%43 = call i64 @read(i32 %41, i8* %42, i32 1000)
store i64 %43, i64* %5
%44 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %39
%47 = load i64, i64* %5
%48 = icmp ugt i64 %47, 0
br i1 %48, label %39, label %49
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32, i32* %1
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %49, %30, %13
ret void
}

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
@IPPROTO_TCP = global i32 0
@.str = [20 x i8] c"socket() failed: %d\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [21 x i8] c"connect() failed: %d\00"
@.str.3 = [17 x i8] c"GET / HTTP/1.0\0A\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define void @cSocketsDemo() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1000 x i8]
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %38
11: ; preds = %0
%12 = bitcast %struct.sockaddr_in* %1 to i8*
%13 = call i32 @bzero(%struct.sockaddr_in* %1, i32 0)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 80)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @connect(i32 %6, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %11
%24 = load i32, i32* @errno
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %37
26: ; preds = %11
%27 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @write(i32 %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
br label %30
30: ; preds = %30, %26
store i8 0, i8* %29
%31 = call i64 @read(i32 %6, i8* %29, i32 1000)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%33 = icmp eq i64 %31, 0
br i1 %33, label %34, label %30
34: ; preds = %30
%35 = call i32 @putchar(i32 10)
%36 = call i32 @close(i32 %6)
br label %37
37: ; preds = %34, %23
br label %38
38: ; preds = %37, %8
ret void
}