#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 flock {
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int strlen(char const *);
int write(int, char const *, int);
void writev_test(const char *filename) {
  int fd = -1;
  int writev_count = -1;
  struct flock lock = {
      0,
  };
  if (!filename) {
    printf("arg is NULL!\n");
    goto out;
  }
  fd = open(filename, O_RDWR | O_CREAT, 0644);
  if (fd == -1) {
    perror("open()");
    goto out;
  }
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  if (fcntl(fd, F_SETLK, &lock) == -1) {
    perror("fcntl()");
    goto out;
  }
  writev_count = write(fd, filename, strlen(filename));
  if (writev_count != strlen(filename)) {
    perror("write()");
    goto out;
  }
  close(fd);
out:
  return;
}


x86 assembly (clang, -O0):
<writev_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11c6 <writev_test+0x46>
lea    0xe44(%rip),%rdi
call   1060 <printf@plt>
jmp    12a8 <writev_test+0x128>
mov    -0x8(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1204 <writev_test+0x84>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a8 <writev_test+0x128>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    124b <writev_test+0xcb>
lea    0xdd4(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a8 <writev_test+0x128>
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
je     12a0 <writev_test+0x120>
lea    0xd87(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a8 <writev_test+0x128>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writev_test>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x8(%rsp)
test   %rdi,%rdi
je     1214 <writev_test+0xa4>
mov    %rdi,%rbx
mov    0x2e4d(%rip),%rax
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1228 <writev_test+0xb8>
mov    %eax,%ebp
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     123c <writev_test+0xcc>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    %eax,%r14d
jne    124a <writev_test+0xda>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfc(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <puts@plt>
lea    0xdd1(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <perror@plt>
lea    0xdc4(%rip),%rdi
call   1090 <perror@plt>
jmp    120b <writev_test+0x9b>
lea    0xdbe(%rip),%rdi
call   1090 <perror@plt>
jmp    120b <writev_test+0x9b>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32 }
@.str = [14 x i8] c"arg is NULL!\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [8 x i8] c"fcntl()\00"
@.str.3 = [8 x i8] c"write()\00"
define void @writev_test(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 -1, i32* %4
%6 = bitcast %struct.flock* %5 to i8*
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %47
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15, i32 420)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %47
21: ; preds = %11
%22 = load i32, i32* @F_WRLCK
%23 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @SEEK_SET
%25 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %3
%27 = load i32, i32* @F_SETLK
%28 = call i32 @fcntl(i32 %26, i32 %27, %struct.flock* %5)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %47
32: ; preds = %21
%33 = load i32, i32* %3
%34 = load i8*, i8** %2
%35 = load i8*, i8** %2
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = load i8*, i8** %2
%40 = call i32 @strlen(i8* %39)
%41 = icmp ne i32 %38, %40
br i1 %41, label %42, label %44
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %47
44: ; preds = %32
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44, %42, %30, %19, %9
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [8 x i8] c"fcntl()\00"
@.str.3 = [8 x i8] c"write()\00"
@str = [13 x i8] c"arg is NULL!\00"
define void @writev_test(i8* %0) {
%2 = alloca %struct.flock
%3 = bitcast %struct.flock* %2 to i8*
%4 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i32 0, i32* %4
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %35
8: ; preds = %1
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %11, i32 420)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %35
16: ; preds = %8
%17 = load i32, i32* @F_WRLCK
%18 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @SEEK_SET
%20 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @F_SETLK
%22 = call i32 @fcntl(i32 %12, i32 %21, %struct.flock* %2)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %35
26: ; preds = %16
%27 = call i32 @strlen(i8* %0)
%28 = call i32 @write(i32 %12, i8* %0, i32 %27)
%29 = call i32 @strlen(i8* %0)
%30 = icmp eq i32 %28, %29
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %35
33: ; preds = %26
%34 = call i32 @close(i32 %12)
br label %35
35: ; preds = %33, %31, %24, %14, %6
ret void
}