#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ trm_get(char *);
int /*<<< orphan*/ trm_str(char *);
int write(int, char *, int);
int advsave(char *hdr, int hlen, char *save, int slen) {
  char fname[50];
  int fd;
  trm_str("File name? ");
  trm_get(fname);
  strcat(fname, ".sav");
  if ((fd = open(fname, O_WRONLY | O_TRUNC | O_CREAT, 0600)) == 0)
    return (0);
  if (write(fd, hdr, hlen) != hlen) {
    close(fd);
    return (0);
  }
  if (write(fd, save, slen) != slen) {
    close(fd);
    return (0);
  }
  close(fd);
  return (1);
}


x86 assembly (clang, -O0):
<advsave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    0xe83(%rip),%rdi
call   1080 <trm_str@plt>
lea    -0x60(%rbp),%rdi
call   1030 <trm_get@plt>
lea    -0x60(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1070 <strcat@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x64(%rbp)
cmp    $0x0,%eax
jne    11dc <advsave+0x7c>
movl   $0x0,-0x4(%rbp)
jmp    1243 <advsave+0xe3>
mov    -0x64(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     1208 <advsave+0xa8>
mov    -0x64(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1243 <advsave+0xe3>
mov    -0x64(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <write@plt>
cmp    -0x24(%rbp),%eax
je     1234 <advsave+0xd4>
mov    -0x64(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1243 <advsave+0xe3>
mov    -0x64(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<advsave>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%r13d
mov    %rdi,%r12
lea    0xe7f(%rip),%rdi
call   1080 <trm_str@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <trm_get@plt>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    0x2e21(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   1060 <open@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     11fe <advsave+0x9e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %r13d,%edx
call   1040 <write@plt>
cmp    %r13d,%eax
jne    11f7 <advsave+0x97>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
xor    %ebx,%ebx
cmp    %r15d,%eax
sete   %bl
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"File name? \00"
@.str.1 = [5 x i8] c".sav\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @advsave(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [50 x i8]
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = call i32 @trm_str(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%14 = call i32 @trm_get(i8* %13)
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%16 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %17, i32 %22, i32 384)
store i32 %23, i32* %11
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %4
store i32 0, i32* %5
br label %49
26: ; preds = %4
%27 = load i32, i32* %11
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = call i32 @write(i32 %27, i8* %28, i32 %29)
%31 = load i32, i32* %7
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32, i32* %11
%35 = call i32 @close(i32 %34)
store i32 0, i32* %5
br label %49
36: ; preds = %26
%37 = load i32, i32* %11
%38 = load i8*, i8** %8
%39 = load i32, i32* %9
%40 = call i32 @write(i32 %37, i8* %38, i32 %39)
%41 = load i32, i32* %9
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = load i32, i32* %11
%45 = call i32 @close(i32 %44)
store i32 0, i32* %5
br label %49
46: ; preds = %36
%47 = load i32, i32* %11
%48 = call i32 @close(i32 %47)
store i32 1, i32* %5
br label %49
49: ; preds = %46, %43, %33, %25
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@.str = [12 x i8] c"File name? \00"
@.str.1 = [5 x i8] c".sav\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @advsave(i8* nocapture %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = alloca [50 x i8]
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @trm_str(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @trm_get(i8* %6)
%9 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %11, %10
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %6, i32 %14, i32 384)
%16 = icmp eq i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %4
%18 = call i32 @write(i32 %15, i8* %0, i32 %1)
%19 = icmp eq i32 %18, %1
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @close(i32 %15)
br label %27
22: ; preds = %17
%23 = call i32 @write(i32 %15, i8* %2, i32 %3)
%24 = icmp eq i32 %23, %3
%25 = call i32 @close(i32 %15)
%26 = zext i1 %24 to i32
br label %27
27: ; preds = %22, %4, %20
%28 = phi i32 [ 0, %20 ], [ 0, %4 ], [ %26, %22 ]
ret i32 %28
}