#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*/ close(int);
int creat(char const *, int);
int errno;
int /*<<< orphan*/ printf(char *, ...);
int unlink(char const *);
int main(int argc, char *argv[]) {
  char const *path = "/dev/fd/1";
  int ret = unlink(path);
  if (ret == -1)
    printf("unlink failed, errno = %d\n", errno);
  else
    printf("unlink OK\n");
  int fd = creat(path, 0777);
  if (fd < 0)
    printf("creat failed, errno = %d\n", errno);
  else {
    printf("creat OK\n");
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1193 <main+0x53>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a1 <main+0x61>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1ff,%esi
call   1060 <creat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11d8 <main+0x98>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ee <main+0xae>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     1170 <main+0x20>
lea    0xed6(%rip),%rdi
call   1040 <puts@plt>
jmp    1187 <main+0x37>
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe72(%rip),%rdi
mov    $0x1ff,%esi
call   1070 <creat@plt>
test   %eax,%eax
js     11b3 <main+0x63>
mov    %eax,%ebx
lea    0xea4(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11ca <main+0x7a>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/fd/1\00"
@.str.1 = [27 x i8] c"unlink failed, errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [11 x i8] c"unlink OK\0A\00"
@.str.3 = [26 x i8] c"creat failed, errno = %d\0A\00"
@.str.4 = [10 x i8] c"creat OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
%9 = load i8*, i8** %6
%10 = call i32 @unlink(i8* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i8*, i8** %6
%20 = call i32 @creat(i8* %19, i32 511)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %30
26: ; preds = %18
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %26, %23
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/fd/1\00"
@.str.1 = [27 x i8] c"unlink failed, errno = %d\0A\00"
@errno = global i32 0
@.str.3 = [26 x i8] c"creat failed, errno = %d\0A\00"
@str = [10 x i8] c"unlink OK\00"
@str.5 = [9 x i8] c"creat OK\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = tail call i32 @creat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 511)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %19
16: ; preds = %10
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.5, i64 0, i64 0))
%18 = tail call i32 @close(i32 %11)
br label %19
19: ; preds = %16, %13
ret i32 0
}