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

/* Variables and functions */
int binfile_write(char const *, struct databuf *);
int /*<<< orphan*/ dbuf_dump(struct databuf *);
int get_filetype(char const *);
int hexfile_write(char const *, struct databuf *);
scalar_t__ strncmp(char const *, char *, int);
int file_write(const char *filename, struct databuf *dbuf) {
  if (strncmp(filename, "-", 1) == 0) {
    dbuf_dump(dbuf);
    return 0;
  }
  switch (get_filetype(filename)) {
  case 1:
    return binfile_write(filename, dbuf);
  case 2:
    return hexfile_write(filename, dbuf);
  default:
    return -1;
  }
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1194 <file_write+0x44>
mov    -0x18(%rbp),%rdi
call   1060 <dbuf_dump@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <file_write+0xa2>
mov    -0x10(%rbp),%rdi
call   1050 <get_filetype@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0x1,%eax
je     11c1 <file_write+0x71>
jmp    11b0 <file_write+0x60>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     11d6 <file_write+0x86>
jmp    11eb <file_write+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <binfile_write@plt>
mov    %eax,-0x4(%rbp)
jmp    11f2 <file_write+0xa2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <hexfile_write@plt>
mov    %eax,-0x4(%rbp)
jmp    11f2 <file_write+0xa2>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9f(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     119b <file_write+0x4b>
mov    %rbx,%rdi
call   1050 <get_filetype@plt>
cmp    $0x2,%eax
je     11ad <file_write+0x5d>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%ecx
jne    11a5 <file_write+0x55>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <binfile_write@plt>
mov    %r14,%rdi
call   1060 <dbuf_dump@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <hexfile_write@plt>

LLVM IR (-O0):
%struct.databuf = type { i32 }
@.str = [2 x i8] c"-\00"
define i32 @file_write(i8* %0, %struct.databuf* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.databuf*
store i8* %0, i8** %4
store %struct.databuf* %1, %struct.databuf** %5
%6 = load i8*, i8** %4
%7 = call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load %struct.databuf*, %struct.databuf** %5
%11 = call i32 @dbuf_dump(%struct.databuf* %10)
store i32 0, i32* %3
br label %24
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @get_filetype(i8* %13)
switch i32 %14, label %23 [
i32 1, label %15
i32 2, label %19
]
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load %struct.databuf*, %struct.databuf** %5
%18 = call i32 @binfile_write(i8* %16, %struct.databuf* %17)
store i32 %18, i32* %3
br label %24
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = load %struct.databuf*, %struct.databuf** %5
%22 = call i32 @hexfile_write(i8* %20, %struct.databuf* %21)
store i32 %22, i32* %3
br label %24
23: ; preds = %12
store i32 -1, i32* %3
br label %24
24: ; preds = %23, %19, %15, %9
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.databuf = type { i32 }
@.str = [2 x i8] c"-\00"
define i32 @file_write(i8* %0, %struct.databuf* %1) {
%3 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @dbuf_dump(%struct.databuf* %1)
br label %13
7: ; preds = %2
%8 = tail call i32 @get_filetype(i8* %0)
switch i32 %8, label %13 [
i32 1, label %9
i32 2, label %11
]
9: ; preds = %7
%10 = tail call i32 @binfile_write(i8* %0, %struct.databuf* %1)
br label %13
11: ; preds = %7
%12 = tail call i32 @hexfile_write(i8* %0, %struct.databuf* %1)
br label %13
13: ; preds = %7, %11, %9, %5
%14 = phi i32 [ 0, %5 ], [ %12, %11 ], [ %10, %9 ], [ -1, %7 ]
ret i32 %14
}