#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *os_createnewfile(char *);
char *os_idtodir(char *);
int /*<<< orphan*/ os_perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
FILE *startDescFile(char *fname) {
  char buf[1024];
  char *dir;
  FILE *descfile;
  dir = os_idtodir(fname);
  if (!dir)
    return 0;
  sprintf(buf, "%s0", dir);
  descfile = fopen(buf, "r");
  if (descfile) {
    fclose(descfile);
    return 0;
  }
  descfile = os_createnewfile(buf);
  if (!descfile) {
    os_perror(buf);
    return 0;
  }
  return descfile;
}


x86 assembly (clang, -O0):
<startDescFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <os_idtodir@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    119a <startDescFile+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    123a <startDescFile+0xda>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rdx
lea    0xe51(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     11f5 <startDescFile+0x95>
mov    -0x420(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <startDescFile+0xda>
lea    -0x410(%rbp),%rdi
call   1050 <os_createnewfile@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    122f <startDescFile+0xcf>
lea    -0x410(%rbp),%rdi
call   1040 <os_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <startDescFile+0xda>
mov    -0x420(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startDescFile>:
push   %rbx
sub    $0x400,%rsp
call   1060 <os_idtodir@plt>
test   %rax,%rax
je     11a3 <startDescFile+0x43>
lea    0xe87(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
call   1080 <sprintf@plt>
lea    0xe76(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ae <startDescFile+0x4e>
mov    %rax,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
ret
mov    %rsp,%rdi
call   1050 <os_createnewfile@plt>
test   %rax,%rax
jne    11a5 <startDescFile+0x45>
mov    %rsp,%rdi
call   1040 <os_perror@plt>
jmp    11a3 <startDescFile+0x43>

LLVM IR (-O0):
@.str = [4 x i8] c"%s0\00"
@.str.1 = [2 x i8] c"r\00"
define i32* @startDescFile(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @os_idtodir(i8* %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i32* null, i32** %2
br label %33
12: ; preds = %1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
store i32* null, i32** %2
br label %33
23: ; preds = %12
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%25 = call i32* @os_createnewfile(i8* %24)
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%30 = call i32 @os_perror(i8* %29)
store i32* null, i32** %2
br label %33
31: ; preds = %23
%32 = load i32*, i32** %6
store i32* %32, i32** %2
br label %33
33: ; preds = %31, %28, %20, %11
%34 = load i32*, i32** %2
ret i32* %34
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s0\00"
@.str.1 = [2 x i8] c"r\00"
define i32* @startDescFile(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @os_idtodir(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %1
%7 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %4)
%8 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @fclose(i32* %8)
br label %17
12: ; preds = %6
%13 = call i32* @os_createnewfile(i8* %3)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @os_perror(i8* %3)
br label %17
17: ; preds = %12, %1, %15, %10
%18 = phi i32* [ null, %10 ], [ null, %15 ], [ null, %1 ], [ %13, %12 ]
ret i32* %18
}