#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 int /*<<< orphan*/ pathname;
typedef int mode_t;
typedef int /*<<< orphan*/ dev_t;

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ O_RDWR;
int S_IFBLK;
int S_IFCHR;
int S_IRWXU;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ error_reason(int /*<<< orphan*/);
int /*<<< orphan*/ error_text(char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *misc_make_path(char *, char *);
int mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int misc_temp_dev(dev_t dev, int blockdev, char **devno) {
  char *result;
  char *pathname[] = {"/dev", getenv("TMPDIR"), "/tmp", getenv("HOME"), ".",
                      "/"};
  char filename[] = "zipl0000";
  mode_t mode;
  unsigned int path;
  int retry;
  int rc;
  int fd;
  if (blockdev)
    mode = S_IFBLK | S_IRWXU;
  else
    mode = S_IFCHR | S_IRWXU;
  for (path = 0; path < sizeof(pathname) / sizeof(pathname[0]); path++) {
    if (pathname[path] == NULL)
      continue;
    for (retry = 0; retry < 1000; retry++) {
      sprintf(filename, "zipl%04d", retry);
      result = misc_make_path(pathname[path], filename);
      if (result == NULL)
        return -1;
      rc = mknod(result, mode, dev);
      if (rc == 0) {
        fd = open(result, O_RDWR);
        if (fd != -1) {
          close(fd);
          *devno = result;
          return 0;
        }
        remove(result);
        retry = 1000;
      } else if (errno != EEXIST)
        retry = 1000;
      free(result);
    }
  }
  error_text("Unable to create temporary device node");
  error_reason(strerror(errno));
  return -1;
}


x86 assembly (clang, -O0):
<misc_temp_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe37(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe31(%rip),%rdi
call   1040 <getenv@plt>
mov    %rax,-0x48(%rbp)
lea    0xe28(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe22(%rip),%rdi
call   1040 <getenv@plt>
mov    %rax,-0x38(%rbp)
lea    0xe17(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe0e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe05(%rip),%rax
mov    %rax,-0x59(%rbp)
mov    0xe03(%rip),%al
mov    %al,-0x51(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1246 <misc_temp_dev+0x96>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    0x2dac(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x60(%rbp)
jmp    125b <misc_temp_dev+0xab>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    0x2d92(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x60(%rbp)
movl   $0x0,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    $0x6,%rax
jae    1395 <misc_temp_dev+0x1e5>
mov    -0x64(%rbp),%eax
cmpq   $0x0,-0x50(%rbp,%rax,8)
jne    1283 <misc_temp_dev+0xd3>
jmp    1387 <misc_temp_dev+0x1d7>
movl   $0x0,-0x68(%rbp)
cmpl   $0x3e8,-0x68(%rbp)
jge    1382 <misc_temp_dev+0x1d2>
lea    -0x59(%rbp),%rdi
mov    -0x68(%rbp),%edx
lea    0xd7e(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x64(%rbp),%eax
mov    -0x50(%rbp,%rax,8),%rdi
lea    -0x59(%rbp),%rsi
call   10b0 <misc_make_path@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d6 <misc_temp_dev+0x126>
movl   $0xffffffff,-0x4(%rbp)
jmp    13be <misc_temp_dev+0x20e>
mov    -0x20(%rbp),%rdi
mov    -0x60(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1090 <mknod@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jne    1345 <misc_temp_dev+0x195>
mov    -0x20(%rbp),%rdi
mov    0x2ce3(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x70(%rbp)
cmpl   $0xffffffff,-0x70(%rbp)
je     1330 <misc_temp_dev+0x180>
mov    -0x70(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    13be <misc_temp_dev+0x20e>
mov    -0x20(%rbp),%rdi
call   1060 <remove@plt>
movl   $0x3e8,-0x68(%rbp)
jmp    136b <misc_temp_dev+0x1bb>
mov    0x2c6c(%rip),%rax
mov    (%rax),%rax
mov    0x2c72(%rip),%rcx
cmp    (%rcx),%rax
je     1366 <misc_temp_dev+0x1b6>
movl   $0x3e8,-0x68(%rbp)
jmp    136b <misc_temp_dev+0x1bb>
mov    -0x20(%rbp),%rdi
call   1050 <free@plt>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    128a <misc_temp_dev+0xda>
jmp    1387 <misc_temp_dev+0x1d7>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    1262 <misc_temp_dev+0xb2>
lea    0xc90(%rip),%rdi
call   1080 <error_text@plt>
mov    0x2c10(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    %eax,%edi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_temp_dev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdx,0x18(%rsp)
mov    %esi,%ebp
mov    %edi,0x4(%rsp)
lea    0xe30(%rip),%rbx
mov    %rbx,0x20(%rsp)
lea    0xe29(%rip),%rdi
call   1040 <getenv@plt>
mov    %rax,0x28(%rsp)
lea    0xe1f(%rip),%rax
mov    %rax,0x30(%rsp)
lea    0xe18(%rip),%rdi
call   1040 <getenv@plt>
mov    %rax,0x38(%rsp)
lea    0xe0c(%rip),%rax
mov    %rax,0x40(%rsp)
lea    0xe02(%rip),%rax
mov    %rax,0x48(%rsp)
movabs $0x303030306c70697a,%rax
30 30 30
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
test   %ebp,%ebp
jne    123c <misc_temp_dev+0x8c>
mov    0x2d86(%rip),%rax
jmp    1243 <misc_temp_dev+0x93>
mov    0x2d8d(%rip),%rax
mov    0x2d9e(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
xor    %r15d,%r15d
lea    0x8(%rsp),%r13
test   %rbx,%rbx
je     1306 <misc_temp_dev+0x156>
nop
mov    $0xffffffff,%r12d
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r12d
mov    %r13,%rdi
lea    0xda5(%rip),%rsi
mov    %r12d,%edx
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10b0 <misc_make_path@plt>
test   %rax,%rax
je     1342 <misc_temp_dev+0x192>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebp,%esi
mov    0x4(%rsp),%edx
call   1090 <mknod@plt>
test   %eax,%eax
je     12e0 <misc_temp_dev+0x130>
mov    0x2d02(%rip),%rax
mov    (%rax),%rax
mov    0x2d08(%rip),%rcx
cmp    (%rcx),%rax
jne    12fe <misc_temp_dev+0x14e>
mov    %r14,%rdi
call   1050 <free@plt>
cmp    $0x3e7,%r12d
jb     1270 <misc_temp_dev+0xc0>
jmp    1306 <misc_temp_dev+0x156>
nopl   0x0(%rax,%rax,1)
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    1356 <misc_temp_dev+0x1a6>
mov    %r14,%rdi
call   1060 <remove@plt>
mov    %r14,%rdi
call   1050 <free@plt>
add    $0x1,%r15
cmp    $0x6,%r15
je     1320 <misc_temp_dev+0x170>
mov    0x20(%rsp,%r15,8),%rbx
test   %rbx,%rbx
jne    1260 <misc_temp_dev+0xb0>
jmp    1306 <misc_temp_dev+0x156>
lea    0xd05(%rip),%rdi
call   1080 <error_text@plt>
mov    0x2c85(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    %eax,%edi
call   1030 <error_reason@plt>
mov    $0xffffffff,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%edi
call   1070 <close@plt>
mov    0x18(%rsp),%rax
mov    %r14,(%rax)
xor    %eax,%eax
jmp    1347 <misc_temp_dev+0x197>

LLVM IR (-O0):
@.str = [5 x i8] c"/dev\00"
@.str.1 = [7 x i8] c"TMPDIR\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [5 x i8] c"HOME\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [2 x i8] c"/\00"
@__const.misc_temp_dev.filename = [9 x i8] c"zipl0000\00"
@S_IFBLK = global i32 0
@S_IRWXU = global i32 0
@S_IFCHR = global i32 0
@.str.6 = [9 x i8] c"zipl%04d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.7 = [39 x i8] c"Unable to create temporary device node\00"
define i32 @misc_temp_dev(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca [6 x i8*]
%10 = alloca [9 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%16 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %16
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %17
%19 = getelementptr inbounds i8*, i8** %17, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %21, i8** %20
%22 = getelementptr inbounds i8*, i8** %20, i64 1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %22
%23 = getelementptr inbounds i8*, i8** %22, i64 1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8** %23
%24 = bitcast [9 x i8]* %10 to i8*
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %3
%28 = load i32, i32* @S_IFBLK
%29 = load i32, i32* @S_IRWXU
%30 = or i32 %28, %29
store i32 %30, i32* %11
br label %35
31: ; preds = %3
%32 = load i32, i32* @S_IFCHR
%33 = load i32, i32* @S_IRWXU
%34 = or i32 %32, %33
store i32 %34, i32* %11
br label %35
35: ; preds = %31, %27
store i32 0, i32* %12
br label %36
36: ; preds = %98, %35
%37 = load i32, i32* %12
%38 = zext i32 %37 to i64
%39 = icmp ult i64 %38, 6
br i1 %39, label %40, label %101
40: ; preds = %36
%41 = load i32, i32* %12
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i64 0, i64 %42
%44 = load i8*, i8** %43
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %40
br label %98
47: ; preds = %40
store i32 0, i32* %13
br label %48
48: ; preds = %94, %47
%49 = load i32, i32* %13
%50 = icmp slt i32 %49, 1000
br i1 %50, label %51, label %97
51: ; preds = %48
%52 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%53 = load i32, i32* %13
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %12
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i64 0, i64 %56
%58 = load i8*, i8** %57
%59 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%60 = call i8* @misc_make_path(i8* %58, i8* %59)
store i8* %60, i8** %8
%61 = load i8*, i8** %8
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %64
63: ; preds = %51
store i32 -1, i32* %4
br label %106
64: ; preds = %51
%65 = load i8*, i8** %8
%66 = load i32, i32* %11
%67 = load i32, i32* %5
%68 = call i32 @mknod(i8* %65, i32 %66, i32 %67)
store i32 %68, i32* %14
%69 = load i32, i32* %14
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %85
71: ; preds = %64
%72 = load i8*, i8** %8
%73 = load i32, i32* @O_RDWR
%74 = call i32 @open(i8* %72, i32 %73)
store i32 %74, i32* %15
%75 = load i32, i32* %15
%76 = icmp ne i32 %75, -1
br i1 %76, label %77, label %82
77: ; preds = %71
%78 = load i32, i32* %15
%79 = call i32 @close(i32 %78)
%80 = load i8*, i8** %8
%81 = load i8**, i8*** %7
store i8* %80, i8** %81
store i32 0, i32* %4
br label %106
82: ; preds = %71
%83 = load i8*, i8** %8
%84 = call i32 @remove(i8* %83)
store i32 1000, i32* %13
br label %91
85: ; preds = %64
%86 = load i64, i64* @errno
%87 = load i64, i64* @EEXIST
%88 = icmp ne i64 %86, %87
br i1 %88, label %89, label %90
89: ; preds = %85
store i32 1000, i32* %13
br label %90
90: ; preds = %89, %85
br label %91
91: ; preds = %90, %82
%92 = load i8*, i8** %8
%93 = call i32 @free(i8* %92)
br label %94
94: ; preds = %91
%95 = load i32, i32* %13
%96 = add nsw i32 %95, 1
store i32 %96, i32* %13
br label %48
97: ; preds = %48
br label %98
98: ; preds = %97, %46
%99 = load i32, i32* %12
%100 = add i32 %99, 1
store i32 %100, i32* %12
br label %36
101: ; preds = %36
%102 = call i32 @error_text(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%103 = load i64, i64* @errno
%104 = call i32 @strerror(i64 %103)
%105 = call i32 @error_reason(i32 %104)
store i32 -1, i32* %4
br label %106
106: ; preds = %101, %77, %63
%107 = load i32, i32* %4
ret i32 %107
}

LLVM IR (-O2):
@.str = [5 x i8] c"/dev\00"
@.str.1 = [7 x i8] c"TMPDIR\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [5 x i8] c"HOME\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [2 x i8] c"/\00"
@__const.misc_temp_dev.filename = [9 x i8] c"zipl0000\00"
@S_IFBLK = global i32 0
@S_IRWXU = global i32 0
@S_IFCHR = global i32 0
@.str.6 = [9 x i8] c"zipl%04d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.7 = [39 x i8] c"Unable to create temporary device node\00"
define i32 @misc_temp_dev(i32 %0, i32 %1, i8** nocapture writeonly %2) {
%4 = alloca [6 x i8*]
%5 = alloca [9 x i8]
%6 = bitcast [6 x i8*]* %4 to i8*
%7 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 1
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %9, i8** %8
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 3
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %12, i8** %11
%13 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%16 = icmp eq i32 %1, 0
%17 = load i32, i32* @S_IFCHR
%18 = load i32, i32* @S_IFBLK
%19 = select i1 %16, i32 %17, i32 %18
%20 = load i32, i32* @S_IRWXU
%21 = or i32 %20, %19
br label %22
22: ; preds = %55, %3
%23 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %3 ], [ %57, %55 ]
%24 = phi i64 [ 0, %3 ], [ %53, %55 ]
%25 = icmp eq i8* %23, null
br i1 %25, label %52, label %26
26: ; preds = %22, %48
%27 = phi i32 [ %50, %48 ], [ 0, %22 ]
%28 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %27)
%29 = call i8* @misc_make_path(i8* %23, i8* %15)
%30 = icmp eq i8* %29, null
br i1 %30, label %63, label %31
31: ; preds = %26
%32 = call i32 @mknod(i8* %29, i32 %21, i32 %0)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* %29, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @close(i32 %36)
store i8* %29, i8** %2
br label %63
40: ; preds = %34
%41 = call i32 @remove(i8* %29)
br label %46
42: ; preds = %31
%43 = load i64, i64* @errno
%44 = load i64, i64* @EEXIST
%45 = icmp eq i64 %43, %44
br i1 %45, label %48, label %46
46: ; preds = %42, %40
%47 = call i32 @free(i8* %29)
br label %52
48: ; preds = %42
%49 = call i32 @free(i8* %29)
%50 = add nuw nsw i32 %27, 1
%51 = icmp ult i32 %27, 999
br i1 %51, label %26, label %52
52: ; preds = %48, %46, %22
%53 = add nuw nsw i64 %24, 1
%54 = icmp eq i64 %53, 6
br i1 %54, label %58, label %55
55: ; preds = %52
%56 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %53
%57 = load i8*, i8** %56
br label %22
58: ; preds = %52
%59 = call i32 @error_text(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%60 = load i64, i64* @errno
%61 = call i32 @strerror(i64 %60)
%62 = call i32 @error_reason(i32 %61)
br label %63
63: ; preds = %26, %58, %38
%64 = phi i32 [ 0, %38 ], [ -1, %58 ], [ -1, %26 ]
ret i32 %64
}