#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 */
scalar_t__ chdir(char *);
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ dprintf(int, char *);
int /*<<< orphan*/ exit(int);
char *mkdtemp(char *);
int /*<<< orphan*/ perror(char *);
void use_temporary_dir(void) {
  char tmpdir_template[] = "/data/data/syzkaller/syzkaller.XXXXXX";
  char *tmpdir = mkdtemp(tmpdir_template);
  if (!tmpdir)
    do {
      dprintf(2, "failed to mkdtemp");
      dprintf(2, "\n");
      perror("errno");
      exit(1);
    } while (0);
  if (chmod(tmpdir, 0777))
    do {
      dprintf(2, "failed to chmod");
      dprintf(2, "\n");
      perror("errno");
      exit(1);
    } while (0);
  if (chdir(tmpdir))
    do {
      dprintf(2, "failed to chdir");
      dprintf(2, "\n");
      perror("errno");
      exit(1);
    } while (0);
}


x86 assembly (clang, -O0):
<use_temporary_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x30(%rbp),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x26,%edx
call   1050 <memcpy@plt>
lea    -0x30(%rbp),%rdi
call   1060 <mkdtemp@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11e2 <use_temporary_dir+0x72>
jmp    11aa <use_temporary_dir+0x3a>
mov    $0x2,%edi
lea    0xe70(%rip),%rsi
call   1040 <dprintf@plt>
mov    $0x2,%edi
lea    0xe71(%rip),%rsi
call   1040 <dprintf@plt>
lea    0xe67(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    $0x1ff,%esi
call   1070 <chmod@plt>
cmp    $0x0,%rax
je     1237 <use_temporary_dir+0xc7>
jmp    11ff <use_temporary_dir+0x8f>
mov    $0x2,%edi
lea    0xe35(%rip),%rsi
call   1040 <dprintf@plt>
mov    $0x2,%edi
lea    0xe1c(%rip),%rsi
call   1040 <dprintf@plt>
lea    0xe12(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     1287 <use_temporary_dir+0x117>
jmp    124f <use_temporary_dir+0xdf>
mov    $0x2,%edi
lea    0xdf5(%rip),%rsi
call   1040 <dprintf@plt>
mov    $0x2,%edi
lea    0xdcc(%rip),%rsi
call   1040 <dprintf@plt>
lea    0xdc2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<use_temporary_dir>:
push   %rbx
sub    $0x30,%rsp
movaps 0xea4(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe88(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x5858585858582e,%rax
mov    %rax,0x1e(%rsp)
mov    %rsp,%rdi
call   1050 <mkdtemp@plt>
test   %rax,%rax
je     11c0 <use_temporary_dir+0x60>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1ff,%esi
call   1060 <chmod@plt>
test   %rax,%rax
jne    11c9 <use_temporary_dir+0x69>
mov    %rbx,%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jne    11d2 <use_temporary_dir+0x72>
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe5f(%rip),%rsi
jmp    11d9 <use_temporary_dir+0x79>
lea    0xe70(%rip),%rsi
jmp    11d9 <use_temporary_dir+0x79>
lea    0xe77(%rip),%rsi
mov    $0x2,%edi
call   1040 <dprintf@plt>
lea    0xe4e(%rip),%rsi
mov    $0x2,%edi
call   1040 <dprintf@plt>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.use_temporary_dir.tmpdir_template = [38 x i8] c"/data/data/syzkaller/syzkaller.XXXXXX\00"
@.str = [18 x i8] c"failed to mkdtemp\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [6 x i8] c"errno\00"
@.str.3 = [16 x i8] c"failed to chmod\00"
@.str.4 = [16 x i8] c"failed to chdir\00"
define void @use_temporary_dir() {
%1 = alloca [38 x i8]
%2 = alloca i8*
%3 = bitcast [38 x i8]* %1 to i8*
%4 = getelementptr inbounds [38 x i8], [38 x i8]* %1, i64 0, i64 0
%5 = call i8* @mkdtemp(i8* %4)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %15, label %8
8: ; preds = %0
br label %9
9: ; preds = %8
%10 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %0
%16 = load i8*, i8** %2
%17 = call i64 @chmod(i8* %16, i32 511)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %15
br label %20
20: ; preds = %19
%21 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; No predecessors!
br label %26
26: ; preds = %25, %15
%27 = load i8*, i8** %2
%28 = call i64 @chdir(i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %26
br label %31
31: ; preds = %30
%32 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; No predecessors!
br label %37
37: ; preds = %36, %26
ret void
}

LLVM IR (-O2):
@__const.use_temporary_dir.tmpdir_template = [38 x i8] c"/data/data/syzkaller/syzkaller.XXXXXX\00"
@.str = [18 x i8] c"failed to mkdtemp\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [6 x i8] c"errno\00"
@.str.3 = [16 x i8] c"failed to chmod\00"
@.str.4 = [16 x i8] c"failed to chdir\00"
define void @use_temporary_dir() {
%1 = alloca [38 x i8]
%2 = getelementptr inbounds [38 x i8], [38 x i8]* %1, i64 0, i64 0
%3 = call i8* @mkdtemp(i8* %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i64 @chmod(i8* %3, i32 511)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = call i64 @chdir(i8* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
ret void
}