#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_CLOEXEC;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mkostemp(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strncmp(char *, char *, int);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ vfork();
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int fd, status;
  char buff[5];
  char template[] = "/tmp/test-mkostemp.XXXXXX";
  fd = mkostemp(template, O_CLOEXEC);
  unlink(template);
  snprintf(buff, 5, "%d", fd);
  if (!vfork())
    if (execl("./test-mkostemp-child", "test-mkostemp-child", buff, NULL) == -1)
      exit(EXIT_FAILURE);
  wait(&status);
  memset(buff, 0, 5);
  lseek(fd, 0, SEEK_SET);
  errno = 0;
  if (read(fd, buff, 5) == -1)
    exit(EXIT_FAILURE);
  if (!strncmp(buff, "test", 5))
    exit(EXIT_FAILURE);
  else
    exit(EXIT_SUCCESS);
  close(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe33(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe30(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe2d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe2a(%rip),%ax
mov    %ax,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1060 <mkostemp@plt>
mov    %eax,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x1d(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x5,%esi
lea    0xdf7(%rip),%rdx
call   1050 <snprintf@plt>
call   10a0 <vfork@plt>
cmp    $0x0,%eax
jne    126d <main+0xbd>
lea    -0x1d(%rbp),%rdx
lea    0xddc(%rip),%rdi
lea    0xdd7(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   10c0 <execl@plt>
cmp    $0xffffffff,%eax
jne    1268 <main+0xb8>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    126d <main+0xbd>
lea    -0x18(%rbp),%rdi
call   10d0 <wait@plt>
lea    -0x1d(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1080 <memset@plt>
mov    -0x14(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    0x2d40(%rip),%rax
movq   $0x0,(%rax)
mov    -0x14(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x5,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    12cf <main+0x11f>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x1d(%rbp),%rdi
lea    0xd59(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    12fb <main+0x14b>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
movups 0xe49(%rip),%xmm0
movups %xmm0,0x1a(%rsp)
movaps 0xe33(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <mkostemp@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
lea    0xe21(%rip),%rdx
lea    0x7(%rsp),%rdi
mov    $0x5,%esi
mov    %ebp,%ecx
call   1050 <snprintf@plt>
call   10a0 <vfork@plt>
test   %eax,%eax
jne    1234 <main+0x84>
lea    0xe03(%rip),%rdi
lea    0xdfe(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    0x7(%rsp),%rdx
call   10c0 <execl@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0xf4>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
lea    0x7(%rsp),%r14
xor    %ebx,%ebx
mov    $0x5,%r15d
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
call   1080 <memset@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1070 <lseek@plt>
mov    0x2d6f(%rip),%rax
movq   $0x0,(%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0xf4>
lea    0xda2(%rip),%rsi
lea    0x7(%rsp),%rdi
mov    $0x5,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    12b2 <main+0x102>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@__const.main.template = [26 x i8] c"/tmp/test-mkostemp.XXXXXX\00"
@O_CLOEXEC = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [22 x i8] c"./test-mkostemp-child\00"
@.str.2 = [20 x i8] c"test-mkostemp-child\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@.str.3 = [5 x i8] c"test\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [5 x i8]
%9 = alloca [26 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [26 x i8]* %9 to i8*
%11 = getelementptr inbounds [26 x i8], [26 x i8]* %9, i64 0, i64 0
%12 = load i32, i32* @O_CLOEXEC
%13 = call i32 @mkostemp(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = getelementptr inbounds [26 x i8], [26 x i8]* %9, i64 0, i64 0
%15 = call i32 @unlink(i8* %14)
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%17 = load i32, i32* %6
%18 = call i32 @snprintf(i8* %16, i32 5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @vfork()
%20 = icmp ne i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %2
%22 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%23 = call i32 @execl(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %22, i32* null)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %21
br label %29
29: ; preds = %28, %2
%30 = call i32 @wait(i32* %7)
%31 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i32 0, i32 5)
%33 = load i32, i32* %6
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @lseek(i32 %33, i32 0, i32 %34)
store i64 0, i64* @errno
%36 = load i32, i32* %6
%37 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 5)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %29
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %29
%44 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%45 = call i32 @strncmp(i8* %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 5)
%46 = icmp ne i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %43
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %43
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
}

LLVM IR (-O2):
@__const.main.template = [26 x i8] c"/tmp/test-mkostemp.XXXXXX\00"
@O_CLOEXEC = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [22 x i8] c"./test-mkostemp-child\00"
@.str.2 = [20 x i8] c"test-mkostemp-child\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@.str.3 = [5 x i8] c"test\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = alloca [26 x i8]
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [26 x i8], [26 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @O_CLOEXEC
%10 = call i32 @mkostemp(i8* %8, i32 %9)
%11 = call i32 @unlink(i8* %8)
%12 = call i32 @snprintf(i8* %7, i32 5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %10)
%13 = call i32 @vfork()
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = call i32 @execl(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %7, i32* null)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15, %2
%22 = call i32 @wait(i32* %3)
%23 = call i32 @memset(i8* %7, i32 0, i32 5)
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %10, i32 0, i32 %24)
store i64 0, i64* @errno
%26 = call i32 @read(i32 %10, i8* %7, i32 5)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = call i32 @strncmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 5)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %31
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
}