#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*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/,
                           char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
char *xml_encode(char *);
int update_adisk(int file_fmt, char *share_name) {
  static int cnt = 0;
  FILE *adisk_conf_fp = NULL;
  char *encoded_share_name = NULL;
  if ((adisk_conf_fp = fopen("/etc/avahi/services/adisk.service", "a")) ==
      NULL) {
    ;
    return -1;
  }
  fseek(adisk_conf_fp, 0, SEEK_END);
  encoded_share_name = xml_encode(share_name);
  if (file_fmt == 1) {
    fprintf(adisk_conf_fp,
            "    <txt-record>dk%d=adVF=0x1003,adVN=%s,adVU=</txt-record>\n",
            cnt++, (encoded_share_name ? encoded_share_name : share_name));
  } else {
    fprintf(adisk_conf_fp,
            "    <txt-record>dk%d=adVF=0x1002,adVN=%s,adVU=</txt-record>\n",
            cnt++, (encoded_share_name ? encoded_share_name : share_name));
  }
  if (encoded_share_name)
    free(encoded_share_name);
  fclose(adisk_conf_fp);
  return 0;
}


x86 assembly (clang, -O0):
<update_adisk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ac <update_adisk+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <update_adisk+0x142>
mov    -0x18(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <xml_encode@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    122d <update_adisk+0xcd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e6f(%rip),%eax
mov    %eax,-0x24(%rbp)
add    $0x1,%eax
mov    %eax,0x2e63(%rip)
cmpq   $0x0,-0x20(%rbp)
je     1209 <update_adisk+0xa9>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1211 <update_adisk+0xb1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rcx
lea    0xe01(%rip),%rsi
call   1050 <fprintf@plt>
jmp    127e <update_adisk+0x11e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e19(%rip),%eax
mov    %eax,-0x3c(%rbp)
add    $0x1,%eax
mov    %eax,0x2e0d(%rip)
cmpq   $0x0,-0x20(%rbp)
je     125f <update_adisk+0xff>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1267 <update_adisk+0x107>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x3c(%rbp),%edx
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rcx
lea    0xde8(%rip),%rsi
call   1050 <fprintf@plt>
cmpq   $0x0,-0x20(%rbp)
je     1292 <update_adisk+0x132>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_adisk>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%r15d
lea    0xe8b(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f8 <update_adisk+0x98>
mov    %rax,%rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %r12,%rdi
call   1080 <xml_encode@plt>
mov    %rax,%rbp
mov    0x2ea4(%rip),%edx
lea    0x1(%rdx),%eax
test   %rbp,%rbp
cmovne %rbp,%r12
cmp    $0x1,%r15d
mov    %eax,0x2e90(%rip)
lea    0xe59(%rip),%rax
lea    0xe8f(%rip),%rsi
cmove  %rax,%rsi
mov    %rbx,%rdi
mov    %r12,%rcx
call   1050 <fprintf@plt>
test   %rbp,%rbp
je     11ee <update_adisk+0x8e>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11fe <update_adisk+0x9e>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@update_adisk.cnt = internal global i32 0
@.str = [34 x i8] c"/etc/avahi/services/adisk.service\00"
@.str.1 = [2 x i8] c"a\00"
@SEEK_END = global i32 0
@.str.2 = [61 x i8] c" <txt-record>dk%d=adVF=0x1003,adVN=%s,adVU=</txt-record>\0A\00"
@.str.3 = [61 x i8] c" <txt-record>dk%d=adVF=0x1002,adVN=%s,adVU=</txt-record>\0A\00"
define i32 @update_adisk(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* null, i32** %6
store i8* null, i8** %7
%8 = call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %6
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %54
11: ; preds = %2
%12 = load i32*, i32** %6
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = load i8*, i8** %5
%16 = call i8* @xml_encode(i8* %15)
store i8* %16, i8** %7
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %32
19: ; preds = %11
%20 = load i32*, i32** %6
%21 = load i32, i32* @update_adisk.cnt
%22 = add nsw i32 %21, 1
store i32 %22, i32* @update_adisk.cnt
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i8*, i8** %7
br label %29
27: ; preds = %19
%28 = load i8*, i8** %5
br label %29
29: ; preds = %27, %25
%30 = phi i8* [ %26, %25 ], [ %28, %27 ]
%31 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %21, i8* %30)
br label %45
32: ; preds = %11
%33 = load i32*, i32** %6
%34 = load i32, i32* @update_adisk.cnt
%35 = add nsw i32 %34, 1
store i32 %35, i32* @update_adisk.cnt
%36 = load i8*, i8** %7
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = load i8*, i8** %7
br label %42
40: ; preds = %32
%41 = load i8*, i8** %5
br label %42
42: ; preds = %40, %38
%43 = phi i8* [ %39, %38 ], [ %41, %40 ]
%44 = call i32 @fprintf(i32* %33, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %43)
br label %45
45: ; preds = %42, %29
%46 = load i8*, i8** %7
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i8*, i8** %7
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %48, %45
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
store i32 0, i32* %3
br label %54
54: ; preds = %51, %10
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@update_adisk.cnt = internal unnamed_addr global i32 0
@.str = [34 x i8] c"/etc/avahi/services/adisk.service\00"
@.str.1 = [2 x i8] c"a\00"
@SEEK_END = global i32 0
@.str.2 = [61 x i8] c" <txt-record>dk%d=adVF=0x1003,adVN=%s,adVU=</txt-record>\0A\00"
@.str.3 = [61 x i8] c" <txt-record>dk%d=adVF=0x1002,adVN=%s,adVU=</txt-record>\0A\00"
define i32 @update_adisk(i32 %0, i8* %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i8* @xml_encode(i8* %1)
%9 = icmp eq i32 %0, 1
%10 = load i32, i32* @update_adisk.cnt
%11 = add nsw i32 %10, 1
store i32 %11, i32* @update_adisk.cnt
%12 = icmp eq i8* %8, null
%13 = select i1 %12, i8* %1, i8* %8
%14 = select i1 %9, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0)
%15 = tail call i32 @fprintf(i32* %3, i8* %14, i32 %10, i8* %13)
%16 = icmp eq i8* %8, null
br i1 %16, label %19, label %17
17: ; preds = %5
%18 = tail call i32 @free(i8* %8)
br label %19
19: ; preds = %17, %5
%20 = tail call i32 @fclose(i32* %3)
br label %21
21: ; preds = %2, %19
%22 = phi i32 [ 0, %19 ], [ -1, %2 ]
ret i32 %22
}