#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 */
struct sigaction {
  int /*<<< orphan*/ sa_handler;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SYS_rt_sigaction;
scalar_t__ chdir(char *);
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ fail(char *);
int /*<<< orphan*/ install_segv_handler();
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
char *mkdtemp(char *);
int /*<<< orphan*/ syscall(int /*<<< orphan*/, int, struct sigaction *,
                           int /*<<< orphan*/ *, int);
void setup_main_process() {
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_IGN;
  syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
  syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
  install_segv_handler();
  char tmpdir_template[] = "./syzkaller.XXXXXX";
  char *tmpdir = mkdtemp(tmpdir_template);
  if (!tmpdir)
    fail("failed to mkdtemp");
  if (chmod(tmpdir, 0777))
    fail("failed to chmod");
  if (chdir(tmpdir))
    fail("failed to chdir");
}


x86 assembly (clang, -O0):
<setup_main_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    $0x20,%esi
lea    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x8,%r8d
call   1070 <syscall@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    $0x21,%esi
lea    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x8,%r8d
call   1070 <syscall@plt>
mov    $0x0,%al
call   1050 <install_segv_handler@plt>
mov    0xe1c(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe19(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe16(%rip),%ax
mov    %ax,-0x10(%rbp)
mov    0xe0e(%rip),%al
mov    %al,-0xe(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <mkdtemp@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122b <setup_main_process+0xbb>
lea    0xded(%rip),%rdi
call   1030 <fail@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1ff,%esi
call   1090 <chmod@plt>
cmp    $0x0,%rax
je     124f <setup_main_process+0xdf>
lea    0xddb(%rip),%rdi
call   1030 <fail@plt>
mov    -0x28(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     126e <setup_main_process+0xfe>
lea    0xdcc(%rip),%rdi
call   1030 <fail@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_main_process>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e49(%rip),%rbx
mov    (%rbx),%edi
mov    $0x20,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    $0x8,%r8d
call   1070 <syscall@plt>
mov    (%rbx),%edi
mov    $0x21,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    $0x8,%r8d
call   1070 <syscall@plt>
xor    %eax,%eax
call   1050 <install_segv_handler@plt>
movaps 0xe25(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x585858,0x1f(%rsp)
lea    0x10(%rsp),%rdi
call   1080 <mkdtemp@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1206 <setup_main_process+0x96>
lea    0xe12(%rip),%rdi
call   1030 <fail@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1090 <chmod@plt>
test   %rax,%rax
je     1224 <setup_main_process+0xb4>
lea    0xe06(%rip),%rdi
call   1030 <fail@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     123d <setup_main_process+0xcd>
lea    0xdfd(%rip),%rdi
call   1030 <fail@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sigaction = type { i32 }
@SIG_IGN = global i32 0
@SYS_rt_sigaction = global i32 0
@__const.setup_main_process.tmpdir_template = [19 x i8] c"./syzkaller.XXXXXX\00"
@.str = [18 x i8] c"failed to mkdtemp\00"
@.str.1 = [16 x i8] c"failed to chmod\00"
@.str.2 = [16 x i8] c"failed to chdir\00"
define void @setup_main_process() {
%1 = alloca %struct.sigaction
%2 = alloca [19 x i8]
%3 = alloca i8*
%4 = call i32 @memset(%struct.sigaction* %1, i32 0, i32 4)
%5 = load i32, i32* @SIG_IGN
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i32 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* @SYS_rt_sigaction
%8 = call i32 @syscall(i32 %7, i32 32, %struct.sigaction* %1, i32* null, i32 8)
%9 = load i32, i32* @SYS_rt_sigaction
%10 = call i32 @syscall(i32 %9, i32 33, %struct.sigaction* %1, i32* null, i32 8)
%11 = call i32 (...) @install_segv_handler()
%12 = bitcast [19 x i8]* %2 to i8*
%13 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%14 = call i8* @mkdtemp(i8* %13)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %0
%18 = call i32 @fail(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = load i8*, i8** %3
%21 = call i64 @chmod(i8* %20, i32 511)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = load i8*, i8** %3
%27 = call i64 @chdir(i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
ret void
}

LLVM IR (-O2):
%struct.sigaction = type { i32 }
@SIG_IGN = global i32 0
@SYS_rt_sigaction = global i32 0
@__const.setup_main_process.tmpdir_template = [19 x i8] c"./syzkaller.XXXXXX\00"
@.str = [18 x i8] c"failed to mkdtemp\00"
@.str.1 = [16 x i8] c"failed to chmod\00"
@.str.2 = [16 x i8] c"failed to chdir\00"
define void @setup_main_process() {
%1 = alloca %struct.sigaction
%2 = alloca [19 x i8]
%3 = bitcast %struct.sigaction* %1 to i8*
%4 = call i32 @memset(%struct.sigaction* %1, i32 0, i32 4)
%5 = load i32, i32* @SIG_IGN
%6 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* @SYS_rt_sigaction
%8 = call i32 @syscall(i32 %7, i32 32, %struct.sigaction* %1, i32* null, i32 8)
%9 = load i32, i32* @SYS_rt_sigaction
%10 = call i32 @syscall(i32 %9, i32 33, %struct.sigaction* %1, i32* null, i32 8)
%11 = call i32 (...) @install_segv_handler()
%12 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%13 = call i8* @mkdtemp(i8* %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @fail(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = call i64 @chmod(i8* %13, i32 511)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = call i64 @chdir(i8* %13)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @fail(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
ret void
}