#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;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    perror("socket error");
    return 1;
  } else {
    printf("socket ok\n");
  }
  struct sockaddr_in client = {.sin_addr.s_addr = inet_addr("127.0.0.1"),
                               .sin_family = AF_INET,
                               .sin_port = htons(1100)};
  if (connect(sock, (struct sockaddr *)&client, sizeof(client)) == -1) {
    perror("connect error");
    close(sock);
    return 1;
  } else {
    printf("connect ok\n");
  }
  int numwrite = 0;
  do {
    numwrite = send(sock, "hello", 6, 0);
  } while (numwrite > 0);
  if (numwrite == -1) {
    perror("send error");
  } else {
    printf("send %d bytes\n", numwrite);
  }
  shutdown(sock, 2);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11dd <main+0x4d>
lea    0xe34(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cd <main+0x13d>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x44c,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xe0d(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    124d <main+0xbd>
lea    0xdee(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cd <main+0x13d>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    0xdd0(%rip),%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1262 <main+0xd2>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12a0 <main+0x110>
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
jmp    12b1 <main+0x121>
mov    -0x1c(%rbp),%esi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e3b(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1278 <main+0xd8>
mov    %eax,%ebx
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    $0x44c,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
lea    0xe1a(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     128b <main+0xeb>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
lea    0xe00(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %eax,%eax
jg     1230 <main+0x90>
cmp    $0xffffffff,%eax
je     129e <main+0xfe>
lea    0xde5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    126f <main+0xcf>
lea    0xd85(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    1268 <main+0xc8>
lea    0xd86(%rip),%rdi
call   10a0 <perror@plt>
jmp    125a <main+0xba>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@.str.1 = [11 x i8] c"socket ok\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [14 x i8] c"connect error\00"
@.str.4 = [12 x i8] c"connect ok\0A\00"
@.str.5 = [6 x i8] c"hello\00"
@.str.6 = [11 x i8] c"send error\00"
@.str.7 = [15 x i8] c"send %d bytes\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %52
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%16 = call i32 @htons(i32 1100)
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%18 = load i32, i32* @AF_INET
store i32 %18, i32* %17
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 %21, i32* %20
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
store i32 1, i32* %1
br label %52
30: ; preds = %14
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30
store i32 0, i32* %4
br label %33
33: ; preds = %36, %32
%34 = load i32, i32* %2
%35 = call i32 @send(i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 6, i32 0)
store i32 %35, i32* %4
br label %36
36: ; preds = %33
%37 = load i32, i32* %4
%38 = icmp sgt i32 %37, 0
br i1 %38, label %33, label %39
39: ; preds = %36
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %47
44: ; preds = %39
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %44, %42
%48 = load i32, i32* %2
%49 = call i32 @shutdown(i32 %48, i32 2)
%50 = load i32, i32* %2
%51 = call i32 @close(i32 %50)
store i32 0, i32* %1
br label %52
52: ; preds = %47, %26, %10
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [14 x i8] c"connect error\00"
@.str.5 = [6 x i8] c"hello\00"
@.str.6 = [11 x i8] c"send error\00"
@.str.7 = [15 x i8] c"send %d bytes\0A\00"
@str = [10 x i8] c"socket ok\00"
@str.8 = [11 x i8] c"connect ok\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %38
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%10 = bitcast %struct.sockaddr_in* %1 to i8*
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%12 = tail call i32 @htons(i32 1100)
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%14 = load i32, i32* @AF_INET
store i32 %14, i32* %13
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2, i32 0
%16 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 %16, i32* %15
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i32 @connect(i32 %4, %struct.sockaddr* %17, i32 12)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %8
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %35
22: ; preds = %8
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
br label %24
24: ; preds = %24, %22
%25 = call i32 @send(i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 6, i32 0)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %24, label %27
27: ; preds = %24
%28 = icmp eq i32 %25, -1
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %33
31: ; preds = %27
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %25)
br label %33
33: ; preds = %31, %29
%34 = call i32 @shutdown(i32 %4, i32 2)
br label %35
35: ; preds = %33, %20
%36 = phi i32 [ 1, %20 ], [ 0, %33 ]
%37 = call i32 @close(i32 %4)
br label %38
38: ; preds = %35, %6
%39 = phi i32 [ 1, %6 ], [ %36, %35 ]
ret i32 %39
}