#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*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ handle(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int main() {
  int sockfd, connfd;
  struct sockaddr_in addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("Create socket error\n");
    exit(1);
  }
  memset(&addr, 0, sizeof addr);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9890);
  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
  connfd = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  if (connfd < 0) {
    printf("connect error: %s\n", strerror(connfd));
  }
  handle(sockfd);
  exit(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   $0x0,-0x8(%rbp)
jge    11dd <main+0x4d>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x26a2,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
lea    0xdfb(%rip),%rsi
call   1070 <inet_pton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1256 <main+0xc6>
mov    -0xc(%rbp),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <handle@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e38(%rip),%r15
mov    (%r15),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
jns    11dd <main+0x3d>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0x26a2,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
lea    0xdf0(%rip),%rsi
mov    %r14,%rdx
call   1080 <inet_pton@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %eax,%eax
jns    1243 <main+0xa3>
mov    %eax,%edi
call   10b0 <strerror@plt>
lea    0xdd1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <handle@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Create socket error\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [19 x i8] c"connect error: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
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 slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 9890)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%21 = call i32 @inet_pton(i32 %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %20)
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 12)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %13
%28 = load i32, i32* %3
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %27, %13
%32 = load i32, i32* %2
%33 = call i32 @handle(i32 %32)
%34 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [19 x i8] c"connect error: %s\0A\00"
@str = [20 x i8] c"Create socket error\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 9890)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%18 = call i32 @inet_pton(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %17)
%19 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%20 = call i32 @connect(i32 %5, %struct.sockaddr* %19, i32 12)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = call i8* @strerror(i32 %20)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %10
%26 = call i32 @handle(i32 %5)
%27 = call i32 @exit(i32 0)
unreachable
}