#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 ip_usbph {
  int dummy;
};
typedef int /*<<< orphan*/ rcfile_new;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
char *getenv(char *);
int /*<<< orphan*/ ip_usbph_state_save(struct ip_usbph *, int);
int open(char *, int, int);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
void rc_save(struct ip_usbph *ph) {
  char rcfile[PATH_MAX];
  char rcfile_new[PATH_MAX];
  int fd;
  snprintf(rcfile, sizeof(rcfile_new), "%s/.ip-usbphrc", getenv("HOME"));
  snprintf(rcfile_new, sizeof(rcfile_new), "%s/.ip-usbphrc~", getenv("HOME"));
  rename(rcfile, rcfile_new);
  fd = open(rcfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  if (fd < 0) {
    rename(rcfile_new, rcfile);
  } else {
    ip_usbph_state_save(ph, fd);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<rc_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,-0x4c(%rbp)
lea    0xe4b(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x4c(%rbp),%esi
mov    -0x30(%rbp),%rdi
mov    %rax,%rcx
lea    0xe26(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rax
mov    %eax,-0x3c(%rbp)
lea    0xe22(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdi
mov    %rax,%rcx
lea    0xe11(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdi
call   1070 <rename@plt>
mov    -0x30(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    125d <rc_save+0xfd>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
call   1070 <rename@plt>
jmp    1271 <rc_save+0x111>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <ip_usbph_state_save@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rc_save>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e68(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r13
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe70(%rip),%rbx
mov    %rbx,%rdi
call   1030 <getenv@plt>
lea    0xe52(%rip),%rdx
mov    %r13,%rdi
mov    %r12d,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <getenv@plt>
lea    0xe49(%rip),%rdx
mov    %r15,%rdi
mov    %r12d,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <rename@plt>
mov    0x2dd5(%rip),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
mov    %r13,%rdi
mov    $0x180,%edx
call   1060 <open@plt>
test   %eax,%eax
js     1225 <rc_save+0xc5>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %eax,%esi
call   1080 <ip_usbph_state_save@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1230 <rc_save+0xd0>
mov    %r15,%rdi
mov    %r13,%rsi
call   1070 <rename@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ip_usbph = type { i32 }
@PATH_MAX = global i32 0
@.str = [15 x i8] c"%s/.ip-usbphrc\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [16 x i8] c"%s/.ip-usbphrc~\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define void @rc_save(%struct.ip_usbph* %0) {
%2 = alloca %struct.ip_usbph*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store %struct.ip_usbph* %0, %struct.ip_usbph** %2
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
store i64 %12, i64* %5
%14 = trunc i64 %12 to i32
%15 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @snprintf(i8* %10, i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = trunc i64 %12 to i32
%18 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @snprintf(i8* %13, i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = call i32 @rename(i8* %10, i8* %13)
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %10, i32 %25, i32 384)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %1
%30 = call i32 @rename(i8* %13, i8* %10)
br label %37
31: ; preds = %1
%32 = load %struct.ip_usbph*, %struct.ip_usbph** %2
%33 = load i32, i32* %6
%34 = call i32 @ip_usbph_state_save(%struct.ip_usbph* %32, i32 %33)
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %31, %29
%38 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
%struct.ip_usbph = type { i32 }
@PATH_MAX = global i32 0
@.str = [15 x i8] c"%s/.ip-usbphrc\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [16 x i8] c"%s/.ip-usbphrc~\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define void @rc_save(%struct.ip_usbph* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @snprintf(i8* %5, i32 %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = call i32 @rename(i8* %4, i8* %5)
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %4, i32 %15, i32 384)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @rename(i8* %5, i8* %4)
br label %23
20: ; preds = %1
%21 = call i32 @ip_usbph_state_save(%struct.ip_usbph* %0, i32 %16)
%22 = call i32 @close(i32 %16)
br label %23
23: ; preds = %20, %18
ret void
}