#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, scalar_t__);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void *conn(void *arg) {
  struct timeval b, e;
  struct sockaddr *client = arg;
  int r;
  int s;
  gettimeofday(&b, NULL);
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  r = connect(s, client, sizeof(*client));
  if (r != 0) {
    perror("connect");
    exit(EXIT_FAILURE);
  }
  gettimeofday(&e, NULL);
  printf("%ld\n", (e.tv_sec - b.tv_sec) * 1000000 + (e.tv_usec - b.tv_usec));
  write(s, "hello world\n", 12);
  close(s);
  return (void *)0;
}


x86 assembly (clang, -O0):
<conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11e1 <conn+0x61>
lea    0xe32(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1219 <conn+0x99>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x28(%rbp),%eax
sub    -0x18(%rbp),%eax
imul   $0xf4240,%eax,%eax
movslq %eax,%rsi
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
add    %rax,%rsi
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x38(%rbp),%edi
lea    0xdbe(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conn>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1226 <conn+0xa6>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
test   %eax,%eax
jne    122f <conn+0xaf>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
movslq 0x18(%rsp),%rcx
sub    %rcx,%rax
imul   $0xf4240,%rax,%rax
mov    0x10(%rsp),%rsi
sub    0x20(%rsp),%rsi
add    %rax,%rsi
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
lea    0xe0b(%rip),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd3(%rip),%rdi
jmp    1236 <conn+0xb6>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [13 x i8] c"hello world\0A\00"
define i8* @conn(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = bitcast i8* %8 to %struct.sockaddr*
store %struct.sockaddr* %9, %struct.sockaddr** %5
%10 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
%21 = load i32, i32* %7
%22 = load %struct.sockaddr*, %struct.sockaddr** %5
%23 = call i32 @connect(i32 %21, %struct.sockaddr* %22, i32 4)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%35 = load i32, i32* %34
%36 = sub nsw i32 %33, %35
%37 = mul nsw i32 %36, 1000000
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%40 = load i64, i64* %39
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%42 = load i64, i64* %41
%43 = sub nsw i64 %40, %42
%44 = add nsw i64 %38, %43
%45 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %44)
%46 = load i32, i32* %7
%47 = call i32 @write(i32 %46, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 12)
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
ret i8* null
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [13 x i8] c"hello world\0A\00"
define noalias i8* @conn(i8* %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = bitcast i8* %0 to %struct.sockaddr*
%17 = tail call i32 @connect(i32 %9, %struct.sockaddr* %16, i32 4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%28 = load i32, i32* %27
%29 = sub nsw i32 %26, %28
%30 = mul nsw i32 %29, 1000000
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%33 = load i64, i64* %32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%35 = load i64, i64* %34
%36 = sub i64 %33, %35
%37 = add nsw i64 %36, %31
%38 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %37)
%39 = tail call i32 @write(i32 %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 12)
%40 = tail call i32 @close(i32 %9)
ret i8* null
}