#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(int);
int /*<<< orphan*/ free(char *);
scalar_t__ getegid();
char *getenv(char *);
scalar_t__ geteuid();
scalar_t__ getgid();
scalar_t__ getuid();
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int raw_tempfd(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int create_tempfd(char *tag) {
  char *tmpdir = NULL;
  char *pattern;
  int temp_fd;
  if ((getuid() == geteuid()) && (getgid() == getegid())) {
    if (!((tmpdir = getenv("TMPDIR")))) {
      tmpdir = getenv("TMP");
    }
  }
  if (!tmpdir) {
    tmpdir = "/tmp";
  }
  pattern = malloc(strlen(tmpdir) + strlen(tag) + 2);
  if (!pattern) {
    perror("Could not malloc tempfile pattern");
    exit(1);
  }
  strcpy(pattern, tmpdir);
  strcat(pattern, "/");
  strcat(pattern, tag);
  temp_fd = raw_tempfd(pattern);
  free(pattern);
  return temp_fd;
}


x86 assembly (clang, -O0):
<create_tempfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jne    1257 <create_tempfd+0x87>
mov    $0x0,%al
call   1090 <getgid@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10b0 <getegid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jne    1257 <create_tempfd+0x87>
lea    0xdd1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1252 <create_tempfd+0x82>
lea    0xdbe(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
jmp    1257 <create_tempfd+0x87>
cmpq   $0x0,-0x10(%rbp)
jne    126d <create_tempfd+0x9d>
lea    0xda2(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12b8 <create_tempfd+0xe8>
lea    0xd67(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xd62(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10e0 <strcat@plt>
mov    -0x18(%rbp),%rdi
call   10d0 <raw_tempfd@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_tempfd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <geteuid@plt>
cmp    %rax,%rbx
jne    1230 <create_tempfd+0x60>
xor    %r12d,%r12d
xor    %eax,%eax
call   1090 <getgid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10b0 <getegid@plt>
cmp    %rax,%rbx
jne    1230 <create_tempfd+0x60>
lea    0xdec(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1230 <create_tempfd+0x60>
lea    0xddf(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
test   %r12,%r12
lea    0xdd1(%rip),%r15
cmovne %r12,%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12a8 <create_tempfd+0xd8>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
lea    0xdba(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10e0 <strcat@plt>
mov    %rbx,%rdi
call   10d0 <raw_tempfd@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [4 x i8] c"TMP\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [34 x i8] c"Could not malloc tempfile pattern\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @create_tempfd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %3
%6 = call i64 (...) @getuid()
%7 = call i64 (...) @geteuid()
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %19
9: ; preds = %1
%10 = call i64 (...) @getgid()
%11 = call i64 (...) @getegid()
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %3
br label %18
18: ; preds = %16, %13
br label %19
19: ; preds = %18, %9, %1
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %19
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %3
br label %23
23: ; preds = %22, %19
%24 = load i8*, i8** %3
%25 = call i64 @strlen(i8* %24)
%26 = load i8*, i8** %2
%27 = call i64 @strlen(i8* %26)
%28 = add nsw i64 %25, %27
%29 = add nsw i64 %28, 2
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = icmp ne i8* %31, null
br i1 %32, label %36, label %33
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %23
%37 = load i8*, i8** %4
%38 = load i8*, i8** %3
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %4
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%42 = load i8*, i8** %4
%43 = load i8*, i8** %2
%44 = call i32 @strcat(i8* %42, i8* %43)
%45 = load i8*, i8** %4
%46 = call i32 @raw_tempfd(i8* %45)
store i32 %46, i32* %5
%47 = load i8*, i8** %4
%48 = call i32 @free(i8* %47)
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [4 x i8] c"TMP\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [34 x i8] c"Could not malloc tempfile pattern\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @create_tempfd(i8* %0) {
%2 = tail call i64 (...) @getuid()
%3 = tail call i64 (...) @geteuid()
%4 = icmp eq i64 %2, %3
br i1 %4, label %5, label %14
5: ; preds = %1
%6 = tail call i64 (...) @getgid()
%7 = tail call i64 (...) @getegid()
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %9, %12, %5, %1
%15 = phi i8* [ %10, %9 ], [ %13, %12 ], [ null, %5 ], [ null, %1 ]
%16 = icmp eq i8* %15, null
%17 = select i1 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %15
%18 = tail call i64 @strlen(i8* dereferenceable(1) %17)
%19 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%20 = add i64 %18, 2
%21 = add i64 %20, %19
%22 = tail call i8* @malloc(i64 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = tail call i32 @strcpy(i8* %22, i8* %17)
%29 = tail call i32 @strcat(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = tail call i32 @strcat(i8* %22, i8* %0)
%31 = tail call i32 @raw_tempfd(i8* %22)
%32 = tail call i32 @free(i8* %22)
ret i32 %31
}