#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_APPEND;
int O_CREAT;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ log_error(char *, char *);
int open(char *, int, int);
int /*<<< orphan*/ remove(char *);
int system(char const *);
int exec_command(char *logfile, const char *command) {
  remove(logfile);
  close(1);
  if (open(logfile, O_WRONLY | O_CREAT | O_APPEND,
           S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
    log_error("fail to write output on %s", logfile);
  } else {
    close(2);
    dup2(1, 2);
  }
  return system(command) / 256;
}


x86 assembly (clang, -O0):
<exec_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <remove@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2e61(%rip),%rax
or     (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    0x2e2e(%rip),%rax
or     (%rax),%edx
mov    0x2e15(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11e9 <exec_command+0x89>
mov    -0x8(%rbp),%rsi
lea    0xe21(%rip),%rdi
call   1040 <log_error@plt>
jmp    1202 <exec_command+0xa2>
mov    $0x2,%edi
call   1070 <close@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1050 <system@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_command>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <remove@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    0x2e30(%rip),%rax
mov    0x2e71(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e16(%rip),%rax
or     (%rax),%esi
mov    0x2e25(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e23(%rip),%rax
or     (%rax),%edx
mov    0x2e12(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11e0 <exec_command+0x80>
mov    $0x2,%edi
call   1070 <close@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
jmp    11ef <exec_command+0x8f>
lea    0xe19(%rip),%rdi
mov    %rbx,%rsi
call   1040 <log_error@plt>
mov    %r14,%rdi
call   1050 <system@plt>
lea    0xff(%rax),%ecx
test   %eax,%eax
cmovs  %ecx,%eax
sar    $0x8,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [27 x i8] c"fail to write output on %s\00"
define i32 @exec_command(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i8*, i8** %3
%6 = call i32 @remove(i8* %5)
%7 = call i32 @close(i32 1)
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_APPEND
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRGRP
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IROTH
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %8, i32 %13, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = load i8*, i8** %3
%25 = call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %29
26: ; preds = %2
%27 = call i32 @close(i32 2)
%28 = call i32 @dup2(i32 1, i32 2)
br label %29
29: ; preds = %26, %23
%30 = load i8*, i8** %4
%31 = call i32 @system(i8* %30)
%32 = sdiv i32 %31, 256
ret i32 %32
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [27 x i8] c"fail to write output on %s\00"
define i32 @exec_command(i8* %0, i8* nocapture %1) {
%3 = tail call i32 @remove(i8* %0)
%4 = tail call i32 @close(i32 1)
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %0, i32 %9, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = tail call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %24
21: ; preds = %2
%22 = tail call i32 @close(i32 2)
%23 = tail call i32 @dup2(i32 1, i32 2)
br label %24
24: ; preds = %21, %19
%25 = tail call i32 @system(i8* %1)
%26 = sdiv i32 %25, 256
ret i32 %26
}