#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 stat {
  int dummy;
};

/* Variables and functions */
int PATH_MAX;
int creat(char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int new_outfd(char *fn) {
  struct stat sbuf;
  char new[PATH_MAX];
  int i;
  strcpy(new, fn);
  for (i = 1; stat(new, &sbuf) == 0; i++) {
    memset(new, 0, PATH_MAX);
    sprintf(new, "(%d)%s", i, fn);
  }
  return creat(new, 0666);
}


x86 assembly (clang, -O0):
<new_outfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11e8 <new_outfd+0x98>
mov    -0x30(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x8(%rbp),%rcx
lea    0xe2b(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1198 <new_outfd+0x48>
mov    -0x30(%rbp),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_outfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    %rsp,-0x38(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rsi
mov    %r15,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    11e6 <new_outfd+0x96>
mov    $0x1,%ebx
lea    0xe56(%rip),%r12
lea    -0x30(%rbp),%r13
xchg   %ax,%ax
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %r14,%rcx
call   1070 <sprintf@plt>
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r13,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     11b0 <new_outfd+0x60>
mov    %r15,%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"(%d)%s\00"
define i32 @new_outfd(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = load i8*, i8** %2
%12 = call i32 @strcpy(i8* %10, i8* %11)
store i32 1, i32* %6
br label %13
13: ; preds = %22, %1
%14 = call i64 @stat(i8* %10, %struct.stat* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = load i32, i32* @PATH_MAX
%18 = call i32 @memset(i8* %10, i32 0, i32 %17)
%19 = load i32, i32* %6
%20 = load i8*, i8** %2
%21 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %19, i8* %20)
br label %22
22: ; preds = %16
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %13
25: ; preds = %13
%26 = call i32 @creat(i8* %10, i32 438)
%27 = load i8*, i8** %4
ret i32 %26
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"(%d)%s\00"
define i32 @new_outfd(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = call i32 @strcpy(i8* %7, i8* %0)
%9 = call i64 @stat(i8* %7, %struct.stat* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %1, %11
%12 = phi i32 [ %16, %11 ], [ 1, %1 ]
%13 = load i32, i32* @PATH_MAX
%14 = call i32 @memset(i8* %7, i32 0, i32 %13)
%15 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %0)
%16 = add nuw nsw i32 %12, 1
%17 = call i64 @stat(i8* %7, %struct.stat* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %11, label %19
19: ; preds = %11, %1
%20 = call i32 @creat(i8* %7, i32 438)
ret i32 %20
}