#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 */
typedef int /*<<< orphan*/ uid_t;
struct iovec {
  char *iov_base;
  int iov_len;
};

/* Variables and functions */
int cli_conn(char *);
int /*<<< orphan*/ err_sys(char *);
int recv_fd(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
void *strlen(char *);
int /*<<< orphan*/ write;
int writev(int, struct iovec *, int);
int csopen(char *name, int oflag) {
  int len = 0;
  char buf[10] = {0};
  struct iovec iov[3];
  static int csfd = -1;
  if (csfd < 0) {
    if ((csfd = cli_conn("/tmp/opend")) < 0)
      err_sys("cli_conn error");
  }
  sprintf(buf, " %d", oflag);
  iov[0].iov_base = "open ";
  iov[0].iov_len = strlen("open ");
  iov[1].iov_base = name;
  iov[1].iov_len = strlen(name);
  iov[2].iov_base = buf;
  iov[2].iov_len = strlen(buf) + 1;
  len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
  if (writev(csfd, &iov[0], 3) != len)
    err_sys("writev error");
  uid_t uid = 0;
  return recv_fd(csfd, &uid, write);
}


x86 assembly (clang, -O0):
<csopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x1a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
cmpl   $0x0,0x2ebb(%rip)
jge    11cf <csopen+0x5f>
lea    0xe56(%rip),%rdi
call   1070 <cli_conn@plt>
mov    %eax,0x2ea3(%rip)
cmp    $0x0,%eax
jge    11ca <csopen+0x5a>
lea    0xe46(%rip),%rdi
call   1040 <err_sys@plt>
jmp    11cf <csopen+0x5f>
lea    -0x1a(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe3d(%rip),%rsi
call   1090 <sprintf@plt>
lea    0xe35(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe2a(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x38(%rbp)
lea    -0x1a(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x1a(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %eax,-0x28(%rbp)
mov    -0x48(%rbp),%eax
add    -0x38(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e1e(%rip),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
cmp    -0x10(%rbp),%eax
je     125d <csopen+0xed>
lea    0xdcc(%rip),%rdi
call   1040 <err_sys@plt>
movl   $0x0,-0x54(%rbp)
mov    0x2dee(%rip),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rsi
call   1080 <recv_fd@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<csopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
movw   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
cmpl   $0x0,0x2ec9(%rip)
jns    11ab <csopen+0x4b>
lea    0xe70(%rip),%rdi
call   1060 <cli_conn@plt>
mov    %eax,0x2eb5(%rip)
test   %eax,%eax
jns    11ab <csopen+0x4b>
lea    0xe65(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xe68(%rip),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %r14d,%edx
call   1080 <sprintf@plt>
lea    0xe55(%rip),%rdi
mov    %rdi,0x20(%rsp)
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %ebp,0x28(%rsp)
mov    %rbx,0x30(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %ebx,0x38(%rsp)
mov    %r15,0x40(%rsp)
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%ecx
mov    %ecx,0x48(%rsp)
add    %ebp,%ebx
add    %eax,%ebx
add    $0x1,%ebx
mov    0x2e41(%rip),%edi
lea    0x20(%rsp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
cmp    %ebx,%eax
je     122e <csopen+0xce>
lea    0xdfb(%rip),%rdi
call   1040 <err_sys@plt>
movl   $0x0,0xc(%rsp)
mov    0x2e14(%rip),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
call   1070 <recv_fd@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@csopen.csfd = internal global i32 -1
@.str = [11 x i8] c"/tmp/opend\00"
@.str.1 = [15 x i8] c"cli_conn error\00"
@.str.2 = [4 x i8] c" %d\00"
@.str.3 = [6 x i8] c"open \00"
@.str.4 = [13 x i8] c"writev error\00"
@write = global i32 0
define i32 @csopen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [10 x i8]
%7 = alloca [3 x %struct.iovec]
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%9 = bitcast [10 x i8]* %6 to i8*
%10 = load i32, i32* @csopen.csfd
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = call i32 @cli_conn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %13, i32* @csopen.csfd
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
br label %18
18: ; preds = %17, %2
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%20 = load i32, i32* %4
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %22, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %23
%24 = call i8* @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%25 = ptrtoint i8* %24 to i32
%26 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %26, i32 0, i32 1
store i32 %25, i32* %27
%28 = load i8*, i8** %3
%29 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 1
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 0
store i8* %28, i8** %30
%31 = load i8*, i8** %3
%32 = call i8* @strlen(i8* %31)
%33 = ptrtoint i8* %32 to i32
%34 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 1
%35 = getelementptr inbounds %struct.iovec, %struct.iovec* %34, i32 0, i32 1
store i32 %33, i32* %35
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%37 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 2
%38 = getelementptr inbounds %struct.iovec, %struct.iovec* %37, i32 0, i32 0
store i8* %36, i8** %38
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%40 = call i8* @strlen(i8* %39)
%41 = getelementptr i8, i8* %40, i64 1
%42 = ptrtoint i8* %41 to i32
%43 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 2
%44 = getelementptr inbounds %struct.iovec, %struct.iovec* %43, i32 0, i32 1
store i32 %42, i32* %44
%45 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%46 = getelementptr inbounds %struct.iovec, %struct.iovec* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 1
%49 = getelementptr inbounds %struct.iovec, %struct.iovec* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = add nsw i32 %47, %50
%52 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 2
%53 = getelementptr inbounds %struct.iovec, %struct.iovec* %52, i32 0, i32 1
%54 = load i32, i32* %53
%55 = add nsw i32 %51, %54
store i32 %55, i32* %5
%56 = load i32, i32* @csopen.csfd
%57 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%58 = call i32 @writev(i32 %56, %struct.iovec* %57, i32 3)
%59 = load i32, i32* %5
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %63
61: ; preds = %18
%62 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %61, %18
store i32 0, i32* %8
%64 = load i32, i32* @csopen.csfd
%65 = load i32, i32* @write
%66 = call i32 @recv_fd(i32 %64, i32* %8, i32 %65)
ret i32 %66
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@csopen.csfd = internal unnamed_addr global i32 -1
@.str = [11 x i8] c"/tmp/opend\00"
@.str.1 = [15 x i8] c"cli_conn error\00"
@.str.2 = [4 x i8] c" %d\00"
@.str.3 = [6 x i8] c"open \00"
@.str.4 = [13 x i8] c"writev error\00"
@write = global i32 0
define i32 @csopen(i8* %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = alloca [3 x %struct.iovec]
%5 = alloca i32
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%7 = bitcast [3 x %struct.iovec]* %4 to i8*
%8 = load i32, i32* @csopen.csfd
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = tail call i32 @cli_conn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* @csopen.csfd
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %10, %13, %2
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%17 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0
%18 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %18
%19 = tail call i8* @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%20 = ptrtoint i8* %19 to i64
%21 = trunc i64 %20 to i32
%22 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 1, i32 0
store i8* %0, i8** %23
%24 = tail call i8* @strlen(i8* %0)
%25 = ptrtoint i8* %24 to i64
%26 = trunc i64 %25 to i32
%27 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 1, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 2, i32 0
store i8* %6, i8** %28
%29 = call i8* @strlen(i8* %6)
%30 = getelementptr i8, i8* %29, i64 1
%31 = ptrtoint i8* %30 to i64
%32 = trunc i64 %31 to i32
%33 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 2, i32 1
store i32 %32, i32* %33
%34 = add nsw i32 %26, %21
%35 = add nsw i32 %34, %32
%36 = load i32, i32* @csopen.csfd
%37 = call i32 @writev(i32 %36, %struct.iovec* %17, i32 3)
%38 = icmp eq i32 %37, %35
br i1 %38, label %41, label %39
39: ; preds = %15
%40 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %15
%42 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%43 = load i32, i32* @csopen.csfd
%44 = load i32, i32* @write
%45 = call i32 @recv_fd(i32 %43, i32* %5, i32 %44)
ret i32 %45
}