#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 passwd {
  char *pw_name;
};

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchmod(int, int);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int open(char *, int, int);
int /*<<< orphan*/ puts(char const *);
int /*<<< orphan*/ seteuid(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int writeToLogFile(const char *msg) {
  struct passwd *pass = getpwuid(getuid());
  if (pass == NULL) {
    return 1;
  }
  char log_path[1032];
  snprintf(log_path, 1032, "/home/%s/.openvpn/output.log", pass->pw_name);
  seteuid(getuid());
  int fd = open(log_path, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
  seteuid(0);
  if (fd == -1) {
    puts("failed to open log file");
  } else {
    puts(msg);
    fchmod(fd, 444);
    write(fd, msg, strlen(msg));
    close(fd);
  }
  return 1;
}


x86 assembly (clang, -O0):
<writeToLogFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <writeToLogFile+0x38>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <writeToLogFile+0x128>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    $0x408,%esi
lea    0xe0e(%rip),%rdx
call   1080 <snprintf@plt>
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,%edi
call   10c0 <seteuid@plt>
lea    -0x420(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
or     (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
mov    0x2da1(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x424(%rbp)
xor    %edi,%edi
call   10c0 <seteuid@plt>
cmpl   $0xffffffff,-0x424(%rbp)
jne    1269 <writeToLogFile+0xc9>
lea    0xdbe(%rip),%rdi
call   1030 <puts@plt>
jmp    12c1 <writeToLogFile+0x121>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
mov    -0x424(%rbp),%edi
mov    $0x1bc,%esi
call   10a0 <fchmod@plt>
mov    -0x424(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x424(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeToLogFile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
test   %rax,%rax
je     126d <writeToLogFile+0xcd>
mov    (%rax),%rcx
lea    0xe31(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x408,%esi
call   1080 <snprintf@plt>
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%edi
call   10c0 <seteuid@plt>
mov    0x2e04(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db2(%rip),%rax
or     (%rax),%esi
mov    0x2db9(%rip),%rax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   10b0 <open@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   10c0 <seteuid@plt>
cmp    $0xffffffff,%ebp
je     1261 <writeToLogFile+0xc1>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x1bc,%esi
call   10a0 <fchmod@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    126d <writeToLogFile+0xcd>
lea    0xdb5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [29 x i8] c"/home/%s/.openvpn/output.log\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"failed to open log file\00"
define i32 @writeToLogFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = alloca [1032 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i32 (...) @getuid()
%8 = call %struct.passwd* @getpwuid(i32 %7)
store %struct.passwd* %8, %struct.passwd** %4
%9 = load %struct.passwd*, %struct.passwd** %4
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 1, i32* %2
br label %48
12: ; preds = %1
%13 = getelementptr inbounds [1032 x i8], [1032 x i8]* %5, i64 0, i64 0
%14 = load %struct.passwd*, %struct.passwd** %4
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @snprintf(i8* %13, i32 1032, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 (...) @getuid()
%19 = call i32 @seteuid(i32 %18)
%20 = getelementptr inbounds [1032 x i8], [1032 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @O_WRONLY
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %20, i32 %25, i32 %28)
store i32 %29, i32* %6
%30 = call i32 @seteuid(i32 0)
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %12
%34 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %47
35: ; preds = %12
%36 = load i8*, i8** %3
%37 = call i32 @puts(i8* %36)
%38 = load i32, i32* %6
%39 = call i32 @fchmod(i32 %38, i32 444)
%40 = load i32, i32* %6
%41 = load i8*, i8** %3
%42 = load i8*, i8** %3
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @write(i32 %40, i8* %41, i32 %43)
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %35, %33
store i32 1, i32* %2
br label %48
48: ; preds = %47, %11
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [29 x i8] c"/home/%s/.openvpn/output.log\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"failed to open log file\00"
define i32 @writeToLogFile(i8* %0) {
%2 = alloca [1032 x i8]
%3 = tail call i32 (...) @getuid()
%4 = tail call %struct.passwd* @getpwuid(i32 %3)
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %33, label %6
6: ; preds = %1
%7 = getelementptr inbounds [1032 x i8], [1032 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = call i32 @snprintf(i8* %7, i32 1032, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = call i32 (...) @getuid()
%12 = call i32 @seteuid(i32 %11)
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %14, %13
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = call i32 @open(i8* %7, i32 %17, i32 %20)
%22 = call i32 @seteuid(i32 0)
%23 = icmp eq i32 %21, -1
br i1 %23, label %24, label %26
24: ; preds = %6
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %32
26: ; preds = %6
%27 = call i32 @puts(i8* dereferenceable(1) %0)
%28 = call i32 @fchmod(i32 %21, i32 444)
%29 = call i32 @strlen(i8* %0)
%30 = call i32 @write(i32 %21, i8* %0, i32 %29)
%31 = call i32 @close(i32 %21)
br label %32
32: ; preds = %26, %24
br label %33
33: ; preds = %1, %32
ret i32 1
}