#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 scalar_t__ ssize_t;

/* Variables and functions */
int O_NONBLOCK;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, int);
int ktest_write(int dev_no, char *dev[], char *buf, size_t size, int nonblock,
                int nonewline) {
  int fd, flags, retval = 0;
  ssize_t count;
  if (dev_no != 1)
    fprintf(stderr, "Write only to %s\n", dev[0]);
  flags = nonblock ? (O_WRONLY | O_NONBLOCK) : (O_WRONLY);
  fd = open(dev[0], flags);
  if (fd < 0) {
    fprintf(stderr, "Can't open %s\n", dev[0]);
    retval = -1;
    goto err1;
  }
  count = write(fd, buf, size);
  if (!nonewline) {
    count += write(fd, "\n", 1);
  }
  close(fd);
  return retval;
err1:
  return retval;
}


x86 assembly (clang, -O0):
<ktest_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     118c <ktest_write+0x4c>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0x0,-0x24(%rbp)
je     11b0 <ktest_write+0x70>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    0x2e42(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
jmp    11bc <ktest_write+0x7c>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1206 <ktest_write+0xc6>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x34(%rbp)
jmp    1255 <ktest_write+0x115>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1242 <ktest_write+0x102>
mov    -0x2c(%rbp),%edi
lea    0xdef(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <ktest_write+0x11b>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ktest_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r9d,%r14d
mov    %r8d,%ebp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
cmp    $0x1,%edi
je     1174 <ktest_write+0x34>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe91(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e4d(%rip),%rax
test   %ebp,%ebp
je     1188 <ktest_write+0x48>
mov    0x2e62(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    (%rbx),%rdi
mov    %ebp,%esi
call   1060 <open@plt>
test   %eax,%eax
js     11ca <ktest_write+0x8a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
test   %r14d,%r14d
jne    11bf <ktest_write+0x7f>
lea    0xe6c(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    11e7 <ktest_write+0xa7>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Write only to %s\0A\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [15 x i8] c"Can't open %s\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @ktest_write(i32 %0, i8** %1, i8* %2, i64 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i64
store i32 %0, i32* %8
store i8** %1, i8*** %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %16
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %26
20: ; preds = %6
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %9
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %20, %6
%27 = load i32, i32* %12
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_NONBLOCK
%32 = or i32 %30, %31
br label %35
33: ; preds = %26
%34 = load i32, i32* @O_WRONLY
br label %35
35: ; preds = %33, %29
%36 = phi i32 [ %32, %29 ], [ %34, %33 ]
store i32 %36, i32* %15
%37 = load i8**, i8*** %9
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
%40 = load i32, i32* %15
%41 = call i32 @open(i8* %39, i32 %40)
store i32 %41, i32* %14
%42 = load i32, i32* %14
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %35
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %9
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %48)
store i32 -1, i32* %16
br label %67
50: ; preds = %35
%51 = load i32, i32* %14
%52 = load i8*, i8** %10
%53 = load i64, i64* %11
%54 = trunc i64 %53 to i32
%55 = call i64 @write(i32 %51, i8* %52, i32 %54)
store i64 %55, i64* %17
%56 = load i32, i32* %13
%57 = icmp ne i32 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %50
%59 = load i32, i32* %14
%60 = call i64 @write(i32 %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%61 = load i64, i64* %17
%62 = add nsw i64 %61, %60
store i64 %62, i64* %17
br label %63
63: ; preds = %58, %50
%64 = load i32, i32* %14
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %16
store i32 %66, i32* %7
br label %69
67: ; preds = %44
%68 = load i32, i32* %16
store i32 %68, i32* %7
br label %69
69: ; preds = %67, %63
%70 = load i32, i32* %7
ret i32 %70
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Write only to %s\0A\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [15 x i8] c"Can't open %s\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @ktest_write(i32 %0, i8** nocapture %1, i8* nocapture %2, i64 %3, i32 %4, i32 %5) {
%7 = icmp eq i32 %0, 1
br i1 %7, label %12, label %8
8: ; preds = %6
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %8, %6
%13 = icmp eq i32 %4, 0
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_NONBLOCK
%16 = select i1 %13, i32 0, i32 %15
%17 = or i32 %16, %14
%18 = load i8*, i8** %1
%19 = tail call i32 @open(i8* %18, i32 %17)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %1
%24 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %33
25: ; preds = %12
%26 = trunc i64 %3 to i32
%27 = tail call i64 @write(i32 %19, i8* %2, i32 %26)
%28 = icmp eq i32 %5, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i64 @write(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %31
31: ; preds = %29, %25
%32 = tail call i32 @close(i32 %19)
br label %33
33: ; preds = %21, %31
%34 = phi i32 [ -1, %21 ], [ 0, %31 ]
ret i32 %34
}