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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ MSG_OOB;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sockfd;
  struct sockaddr_in addr;
  sockfd = socket(PF_INET, SOCK_STREAM, 0);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9999);
  inet_aton("127.0.0.1", &addr.sin_addr);
  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
    perror("connect");
  printf("bytes written=%d (OOB)\n", send(sockfd, "A", 1, MSG_OOB));
  sleep(1);
  printf("bytes written=%d (normal)\n", send(sockfd, "ABCDE", 6, 0));
  sleep(5);
  printf("bytes written=%d (OOB)\n", send(sockfd, "B", 1, MSG_OOB));
  sleep(1);
  printf("bytes written=%d (normal)\n", send(sockfd, "12345678", 9, 0));
  sleep(5);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x270f,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xe21(%rip),%rdi
call   1070 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     120b <main+0x7b>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%ecx
lea    0xe0c(%rip),%rsi
mov    $0x1,%edx
call   1040 <send@plt>
mov    %eax,%esi
lea    0xde1(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    0xdfd(%rip),%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,%esi
lea    0xdcd(%rip),%rdi
call   1050 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%ecx
lea    0xdcc(%rip),%rsi
mov    $0x1,%edx
call   1040 <send@plt>
mov    %eax,%esi
lea    0xd7e(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    0xda2(%rip),%rsi
mov    $0x9,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,%esi
lea    0xd6a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x270f,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe28(%rip),%rdi
lea    0x8(%rsp),%r14
mov    %r14,%rsi
call   1070 <inet_aton@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
jne    12cf <main+0x13f>
mov    0x2ddc(%rip),%r12
mov    (%r12),%ecx
lea    0xe1b(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <send@plt>
lea    0xdf0(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xe0a(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
lea    0xdda(%rip),%r15
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    (%r12),%ecx
lea    0xddc(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <send@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xdb7(%rip),%rsi
mov    %ebx,%edi
mov    $0x9,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd34(%rip),%rdi
call   1080 <perror@plt>
jmp    11fd <main+0x6d>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [24 x i8] c"bytes written=%d (OOB)\0A\00"
@.str.3 = [2 x i8] c"A\00"
@MSG_OOB = global i32 0
@.str.4 = [27 x i8] c"bytes written=%d (normal)\0A\00"
@.str.5 = [6 x i8] c"ABCDE\00"
@.str.6 = [2 x i8] c"B\00"
@.str.7 = [9 x i8] c"12345678\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %7, i32* %8
%9 = call i32 @htons(i32 9999)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%12 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = load i32, i32* %2
%14 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%15 = call i64 @connect(i32 %13, %struct.sockaddr* %14, i32 12)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = load i32, i32* %2
%21 = load i32, i32* @MSG_OOB
%22 = call i32 @send(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 1)
%25 = load i32, i32* %2
%26 = call i32 @send(i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 6, i32 0)
%27 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = call i32 @sleep(i32 5)
%29 = load i32, i32* %2
%30 = load i32, i32* @MSG_OOB
%31 = call i32 @send(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = call i32 @sleep(i32 1)
%34 = load i32, i32* %2
%35 = call i32 @send(i32 %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 9, i32 0)
%36 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = call i32 @sleep(i32 5)
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [24 x i8] c"bytes written=%d (OOB)\0A\00"
@.str.3 = [2 x i8] c"A\00"
@MSG_OOB = global i32 0
@.str.4 = [27 x i8] c"bytes written=%d (normal)\0A\00"
@.str.5 = [6 x i8] c"ABCDE\00"
@.str.6 = [2 x i8] c"B\00"
@.str.7 = [9 x i8] c"12345678\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 9999)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%11 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %10)
%12 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%13 = call i64 @connect(i32 %5, %struct.sockaddr* %12, i32 12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = load i32, i32* @MSG_OOB
%19 = call i32 @send(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 @sleep(i32 1)
%22 = call i32 @send(i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 6, i32 0)
%23 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 5)
%25 = load i32, i32* @MSG_OOB
%26 = call i32 @send(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = call i32 @sleep(i32 1)
%29 = call i32 @send(i32 %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 9, i32 0)
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = call i32 @sleep(i32 5)
%32 = call i32 @close(i32 %5)
ret i32 0
}