#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 */

/* Variables and functions */
int O_NONBLOCK;
int O_WRONLY;
int close(int);
int /*<<< orphan*/ errno;
int open(char const *, int);
char *strchr(char const *, char);
char const *strerror(int /*<<< orphan*/);
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
int write(int, char const *, int);
const char *dm_text2fifo(int argc, const char *argv[]) {
  if (argc < 2) {
    return "Text message and file path arguments required";
  }
  const char *message = argv[0];
  const char *file_path = argv[1];
  int length = strlen(message);
  if (file_path[0] == '/') {
    return "File path may not start with \"/\"";
  }
  const char *name = file_path;
  while (*name) {
    const char *next = strchr(name, '/');
    if (next == NULL) {
      next = file_path + strlen(file_path);
    }
    if (strncmp(name, "..", next - name) == 0 && next - name >= 2) {
      return "File path may not contain .. parent directory";
    }
    name = next;
    if (*name) {
      name++;
    }
  }
  int fd = open(file_path, O_WRONLY | O_NONBLOCK);
  if (fd == -1) {
    return strerror(errno);
  }
  int rc = write(fd, message, length);
  if (rc == -1) {
    close(fd);
    return strerror(errno);
  }
  if (rc != length) {
    close(fd);
    return "Message truncated; FIFO has no readers or is full";
  }
  rc = close(fd);
  if (rc == -1) {
    return strerror(errno);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<dm_text2fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0x2,-0xc(%rbp)
jge    1199 <dm_text2fifo+0x29>
lea    0xe70(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1373 <dm_text2fifo+0x203>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11dc <dm_text2fifo+0x6c>
lea    0xe5b(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1373 <dm_text2fifo+0x203>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     12a1 <dm_text2fifo+0x131>
mov    -0x38(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    122f <dm_text2fifo+0xbf>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0xe08(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    127b <dm_text2fifo+0x10b>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x2,%rax
jl     127b <dm_text2fifo+0x10b>
lea    0xde0(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1373 <dm_text2fifo+0x203>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     129c <dm_text2fifo+0x12c>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11e4 <dm_text2fifo+0x74>
mov    -0x28(%rbp),%rdi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
mov    0x2d33(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    12e0 <dm_text2fifo+0x170>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x8(%rbp)
jmp    1373 <dm_text2fifo+0x203>
mov    -0x44(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    131b <dm_text2fifo+0x1ab>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x8(%rbp)
jmp    1373 <dm_text2fifo+0x203>
mov    -0x48(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     133f <dm_text2fifo+0x1cf>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
lea    0xd4a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1373 <dm_text2fifo+0x203>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    136b <dm_text2fifo+0x1fb>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x8(%rbp)
jmp    1373 <dm_text2fifo+0x203>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dm_text2fifo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jge    118f <dm_text2fifo+0x1f>
lea    0xe76(%rip),%r12
jmp    12a1 <dm_text2fifo+0x131>
mov    (%rsi),%rdi
mov    0x8(%rsi),%r14
mov    %rdi,0x10(%rsp)
call   1050 <strlen@plt>
mov    %eax,0xc(%rsp)
mov    (%r14),%al
test   %al,%al
je     1225 <dm_text2fifo+0xb5>
cmp    $0x2f,%al
jne    11bb <dm_text2fifo+0x4b>
lea    0xe78(%rip),%r12
jmp    12a1 <dm_text2fifo+0x131>
lea    0xe8d(%rip),%r13
lea    0xe89(%rip),%r12
mov    %r14,%rbx
jmp    11e1 <dm_text2fifo+0x71>
xchg   %ax,%ax
cmpb   $0x1,0x0(%rbp)
sbb    $0xffffffffffffffff,%rbp
cmpb   $0x0,0x0(%rbp)
mov    %rbp,%rbx
je     1225 <dm_text2fifo+0xb5>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1204 <dm_text2fifo+0x94>
mov    %r14,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbp
add    %r14,%rbp
mov    %rbp,%r15
sub    %rbx,%r15
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11d0 <dm_text2fifo+0x60>
cmp    $0x1,%r15
jle    11d0 <dm_text2fifo+0x60>
jmp    12a1 <dm_text2fifo+0x131>
mov    0x2d9c(%rip),%rax
mov    0x2db5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     127e <dm_text2fifo+0x10e>
mov    %eax,%ebx
mov    %eax,%edi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%r14d
mov    %r14d,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1277 <dm_text2fifo+0x107>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
cmp    %r14d,%ebp
jne    129a <dm_text2fifo+0x12a>
cmp    $0xffffffff,%eax
je     127e <dm_text2fifo+0x10e>
xor    %r12d,%r12d
jmp    12a1 <dm_text2fifo+0x131>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <strerror@plt>
lea    0xddf(%rip),%r12
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Text message and file path arguments required\00"
@.str.1 = [33 x i8] c"File path may not start with \22/\22\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [46 x i8] c"File path may not contain .. parent directory\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.4 = [50 x i8] c"Message truncated; FIFO has no readers or is full\00"
define i8* @dm_text2fifo(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8** %3
br label %114
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
store i8* %19, i8** %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %7
%23 = load i8*, i8** %6
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %8
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 47
br i1 %29, label %30, label %31
30: ; preds = %16
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %114
31: ; preds = %16
%32 = load i8*, i8** %7
store i8* %32, i8** %9
br label %33
33: ; preds = %74, %31
%34 = load i8*, i8** %9
%35 = load i8, i8* %34
%36 = icmp ne i8 %35, 0
br i1 %36, label %37, label %75
37: ; preds = %33
%38 = load i8*, i8** %9
%39 = call i8* @strchr(i8* %38, i8 signext 47)
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i8*, i8** %7
%44 = load i8*, i8** %7
%45 = call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8* %47, i8** %10
br label %48
48: ; preds = %42, %37
%49 = load i8*, i8** %9
%50 = load i8*, i8** %10
%51 = load i8*, i8** %9
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = trunc i64 %54 to i32
%56 = call i64 @strncmp(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %66
58: ; preds = %48
%59 = load i8*, i8** %10
%60 = load i8*, i8** %9
%61 = ptrtoint i8* %59 to i64
%62 = ptrtoint i8* %60 to i64
%63 = sub i64 %61, %62
%64 = icmp sge i64 %63, 2
br i1 %64, label %65, label %66
65: ; preds = %58
store i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8** %3
br label %114
66: ; preds = %58, %48
%67 = load i8*, i8** %10
store i8* %67, i8** %9
%68 = load i8*, i8** %9
%69 = load i8, i8* %68
%70 = icmp ne i8 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %66
%72 = load i8*, i8** %9
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %9
br label %74
74: ; preds = %71, %66
br label %33
75: ; preds = %33
%76 = load i8*, i8** %7
%77 = load i32, i32* @O_WRONLY
%78 = load i32, i32* @O_NONBLOCK
%79 = or i32 %77, %78
%80 = call i32 @open(i8* %76, i32 %79)
store i32 %80, i32* %11
%81 = load i32, i32* %11
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %86
83: ; preds = %75
%84 = load i32, i32* @errno
%85 = call i8* @strerror(i32 %84)
store i8* %85, i8** %3
br label %114
86: ; preds = %75
%87 = load i32, i32* %11
%88 = load i8*, i8** %6
%89 = load i32, i32* %8
%90 = call i32 @write(i32 %87, i8* %88, i32 %89)
store i32 %90, i32* %12
%91 = load i32, i32* %12
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %98
93: ; preds = %86
%94 = load i32, i32* %11
%95 = call i32 @close(i32 %94)
%96 = load i32, i32* @errno
%97 = call i8* @strerror(i32 %96)
store i8* %97, i8** %3
br label %114
98: ; preds = %86
%99 = load i32, i32* %12
%100 = load i32, i32* %8
%101 = icmp ne i32 %99, %100
br i1 %101, label %102, label %105
102: ; preds = %98
%103 = load i32, i32* %11
%104 = call i32 @close(i32 %103)
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0), i8** %3
br label %114
105: ; preds = %98
%106 = load i32, i32* %11
%107 = call i32 @close(i32 %106)
store i32 %107, i32* %12
%108 = load i32, i32* %12
%109 = icmp eq i32 %108, -1
br i1 %109, label %110, label %113
110: ; preds = %105
%111 = load i32, i32* @errno
%112 = call i8* @strerror(i32 %111)
store i8* %112, i8** %3
br label %114
113: ; preds = %105
store i8* null, i8** %3
br label %114
114: ; preds = %113, %110, %102, %93, %83, %65, %30, %15
%115 = load i8*, i8** %3
ret i8* %115
}

LLVM IR (-O2):
@.str = [46 x i8] c"Text message and file path arguments required\00"
@.str.1 = [33 x i8] c"File path may not start with \22/\22\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [46 x i8] c"File path may not contain .. parent directory\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.4 = [50 x i8] c"Message truncated; FIFO has no readers or is full\00"
define i8* @dm_text2fifo(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %54, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @strlen(i8* %5)
%9 = load i8, i8* %7
switch i8 %9, label %17 [
i8 47, label %54
i8 0, label %35
]
10: ; preds = %25
%11 = load i8, i8* %26
%12 = icmp ne i8 %11, 0
%13 = zext i1 %12 to i64
%14 = getelementptr i8, i8* %26, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %35, label %17
17: ; preds = %4, %10
%18 = phi i8* [ %14, %10 ], [ %7, %4 ]
%19 = tail call i8* @strchr(i8* %18, i8 signext 47)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = tail call i32 @strlen(i8* %7)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %7, i64 %23
br label %25
25: ; preds = %21, %17
%26 = phi i8* [ %24, %21 ], [ %19, %17 ]
%27 = ptrtoint i8* %26 to i64
%28 = ptrtoint i8* %18 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = tail call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = icmp eq i64 %31, 0
%33 = icmp sgt i64 %29, 1
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %54, label %10
35: ; preds = %10, %4
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %37, %36
%39 = tail call i32 @open(i8* %7, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %51, label %41
41: ; preds = %35
%42 = tail call i32 @write(i32 %39, i8* %5, i32 %8)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = tail call i32 @close(i32 %39)
br label %51
46: ; preds = %41
%47 = icmp eq i32 %42, %8
%48 = tail call i32 @close(i32 %39)
br i1 %47, label %49, label %54
49: ; preds = %46
%50 = icmp eq i32 %48, -1
br i1 %50, label %51, label %54
51: ; preds = %49, %35, %44
%52 = load i32, i32* @errno
%53 = tail call i8* @strerror(i32 %52)
br label %54
54: ; preds = %25, %51, %46, %4, %49, %2
%55 = phi i8* [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), %4 ], [ null, %49 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0), %46 ], [ %53, %51 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), %25 ]
ret i8* %55
}