#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 O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
char *getenv(char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int rd_open_file(char *filename) {
  char *home;
  char fn[256];
  int fd;
  home = getenv("HOME");
  if (home == NULL)
    return -1;
  sprintf(fn, "%s/.rdesktop/%s", home, filename);
  fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd == -1)
    perror(fn);
  return fd;
}


x86 assembly (clang, -O0):
<rd_open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xeaa(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1176 <rd_open_file+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <rd_open_file+0xa9>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe79(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    11e0 <rd_open_file+0xa0>
lea    -0x120(%rbp),%rdi
call   1050 <perror@plt>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rd_open_file>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rbx
lea    0xeac(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdx
mov    $0xffffffff,%eax
test   %rdx,%rdx
je     11ac <rd_open_file+0x6c>
lea    0xe98(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rcx
call   1060 <sprintf@plt>
mov    0x2e5e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e34(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11b7 <rd_open_file+0x77>
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret
mov    %rsp,%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ac <rd_open_file+0x6c>

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [16 x i8] c"%s/.rdesktop/%s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @rd_open_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %31
11: ; preds = %1
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%13 = load i8*, i8** %4
%14 = load i8*, i8** %3
%15 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %16, i32 %19, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %11
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%28 = call i32 @perror(i8* %27)
br label %29
29: ; preds = %26, %11
%30 = load i32, i32* %6
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %10
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [16 x i8] c"%s/.rdesktop/%s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @rd_open_file(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %0)
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %3, i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = call i32 @perror(i8* %3)
br label %18
18: ; preds = %6, %16, %1
%19 = phi i32 [ -1, %1 ], [ -1, %16 ], [ %14, %6 ]
ret i32 %19
}