#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, void *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ unlink(char *);
int create_and_write_pipe(char *pipename) {
  int retval, created, pipefd, nread, nwritten;
  char input[80];
  char errstring[180];
  int create_pipe(char *pipename);
  int write_to_pipe(int pipefd, char *input, int nchar);
  created = create_pipe(pipename);
  if (-1 == created) {
    sprintf(errstring, "unable to create pipe '%s'", pipename);
    perror(errstring);
    retval = -1;
  } else {
    pipefd = open(pipename, O_WRONLY);
    if (-1 == pipefd) {
      sprintf(errstring, "unable to open pipe '%s'", pipename);
      perror(errstring);
      retval = -1;
    } else {
      fprintf(stderr, "%s \n %s", "type PIN into SecurID card and",
              "enter resulting passcode:");
      nread = read(STDIN_FILENO, (void *)input, 80);
      if (0 >= nread) {
        perror("unable to read from stdin");
        retval = -1;
      } else {
        input[nread - 1] = 0;
        nread--;
        nwritten = write_to_pipe(pipefd, input, nread);
        retval = nwritten;
      }
      close(pipefd);
      if (unlink(pipename) < 0)
        perror("Warning: couldn't remove pipe");
    }
  }
  return (retval);
}


x86 assembly (clang, -O0):
<create_and_write_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <create_pipe@plt>
mov    %eax,-0x10(%rbp)
mov    $0xffffffff,%eax
cmp    -0x10(%rbp),%eax
jne    11e8 <create_and_write_pipe+0x58>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe35(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    12f9 <create_and_write_pipe+0x169>
mov    -0x8(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    123a <create_and_write_pipe+0xaa>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdfe(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    12f4 <create_and_write_pipe+0x164>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
lea    0xdeb(%rip),%rdx
lea    0xe03(%rip),%rcx
call   1080 <fprintf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jl     129a <create_and_write_pipe+0x10a>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    12c8 <create_and_write_pipe+0x138>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write_to_pipe@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
jge    12ef <create_and_write_pipe+0x15f>
lea    0xda5(%rip),%rdi
call   10a0 <perror@plt>
jmp    12f4 <create_and_write_pipe+0x164>
jmp    12f9 <create_and_write_pipe+0x169>
mov    -0xc(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_write_pipe>:
push   %rbp
push   %r14
push   %rbx
sub    $0xc0,%rsp
mov    %rdi,%rbx
call   1070 <create_pipe@plt>
cmp    $0xffffffff,%eax
je     123e <create_and_write_pipe+0xae>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1247 <create_and_write_pipe+0xb7>
mov    %eax,%ebp
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
lea    0xe5d(%rip),%rdx
lea    0xe75(%rip),%rcx
call   1080 <fprintf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    $0x50,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    126c <create_and_write_pipe+0xdc>
add    $0xffffffff,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write_to_pipe@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1040 <unlink@plt>
test   %rax,%rax
js     1280 <create_and_write_pipe+0xf0>
mov    %r14d,%eax
add    $0xc0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdbb(%rip),%rsi
jmp    124e <create_and_write_pipe+0xbe>
lea    0xdcd(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    122f <create_and_write_pipe+0x9f>
lea    0xe02(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    121b <create_and_write_pipe+0x8b>
lea    0xe08(%rip),%rdi
call   10a0 <perror@plt>
jmp    122f <create_and_write_pipe+0x9f>

LLVM IR (-O0):
@.str = [27 x i8] c"unable to create pipe '%s'\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"unable to open pipe '%s'\00"
@stderr = global i32 0
@.str.2 = [8 x i8] c"%s \0A %s\00"
@.str.3 = [31 x i8] c"type PIN into SecurID card and\00"
@.str.4 = [26 x i8] c"enter resulting passcode:\00"
@STDIN_FILENO = global i32 0
@.str.5 = [26 x i8] c"unable to read from stdin\00"
@.str.6 = [30 x i8] c"Warning: couldn't remove pipe\00"
define i32 @create_and_write_pipe(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [80 x i8]
%9 = alloca [180 x i8]
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 @create_pipe(i8* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = getelementptr inbounds [180 x i8], [180 x i8]* %9, i64 0, i64 0
%16 = load i8*, i8** %2
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [180 x i8], [180 x i8]* %9, i64 0, i64 0
%19 = call i32 @perror(i8* %18)
store i32 -1, i32* %3
br label %64
20: ; preds = %1
%21 = load i8*, i8** %2
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = icmp eq i32 -1, %24
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = getelementptr inbounds [180 x i8], [180 x i8]* %9, i64 0, i64 0
%28 = load i8*, i8** %2
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [180 x i8], [180 x i8]* %9, i64 0, i64 0
%31 = call i32 @perror(i8* %30)
store i32 -1, i32* %3
br label %63
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%35 = load i32, i32* @STDIN_FILENO
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 80)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp sge i32 0, %38
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
42: ; preds = %32
%43 = load i32, i32* %6
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 %45
store i8 0, i8* %46
%47 = load i32, i32* %6
%48 = add nsw i32 %47, -1
store i32 %48, i32* %6
%49 = load i32, i32* %5
%50 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%51 = load i32, i32* %6
%52 = call i32 @write_to_pipe(i32 %49, i8* %50, i32 %51)
store i32 %52, i32* %7
%53 = load i32, i32* %7
store i32 %53, i32* %3
br label %54
54: ; preds = %42, %40
%55 = load i32, i32* %5
%56 = call i32 @close(i32 %55)
%57 = load i8*, i8** %2
%58 = call i64 @unlink(i8* %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %60, %54
br label %63
63: ; preds = %62, %26
br label %64
64: ; preds = %63, %14
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@.str = [27 x i8] c"unable to create pipe '%s'\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"unable to open pipe '%s'\00"
@stderr = global i32 0
@.str.2 = [8 x i8] c"%s \0A %s\00"
@.str.3 = [31 x i8] c"type PIN into SecurID card and\00"
@.str.4 = [26 x i8] c"enter resulting passcode:\00"
@STDIN_FILENO = global i32 0
@.str.5 = [26 x i8] c"unable to read from stdin\00"
@.str.6 = [30 x i8] c"Warning: couldn't remove pipe\00"
define i32 @create_and_write_pipe(i8* %0) {
%2 = alloca [80 x i8]
%3 = alloca [180 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [180 x i8], [180 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @create_pipe(i8* %0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @perror(i8* %5)
br label %38
11: ; preds = %1
%12 = load i32, i32* @O_WRONLY
%13 = tail call i32 @open(i8* %0, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = call i32 @perror(i8* %5)
br label %38
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32, i32* @STDIN_FILENO
%22 = call i32 @read(i32 %21, i8* %4, i32 80)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %31
26: ; preds = %18
%27 = add nsw i32 %22, -1
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 @write_to_pipe(i32 %13, i8* %4, i32 %27)
br label %31
31: ; preds = %26, %24
%32 = phi i32 [ -1, %24 ], [ %30, %26 ]
%33 = call i32 @close(i32 %13)
%34 = call i64 @unlink(i8* %0)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %15, %36, %31, %8
%39 = phi i32 [ -1, %8 ], [ -1, %15 ], [ %32, %36 ], [ %32, %31 ]
ret i32 %39
}