#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 stat {
  int dummy;
};
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ symlink(char *, char *);
scalar_t__ unlink(char *);
int main(int argc, char **argv) {
  int retval = 0;
  char *zone, *p;
  char path[PATH_MAX];
  FILE *f = NULL;
  struct stat st;
  if (argc != 2)
    return 1;
  zone = argv[1];
  snprintf(path, sizeof(path), "/usr/share/zoneinfo/%s", zone);
  if (stat(path, &st) < 0) {
    fprintf(stderr, "Failed to stat '%s': %s\n", zone, strerror(errno));
    retval = 2;
    goto finish;
  }
  if ((f = fopen("/etc/timezone", "w"))) {
    fputs(zone, f);
  } else {
    fprintf(stderr, "Failed to open /etc/timezone: %s", strerror(errno));
    retval = 3;
    goto finish;
  }
  if (unlink("/etc/localtime")) {
    fprintf(stderr, "Failed to unlink localtime: %s", strerror(errno));
    retval = 4;
    goto finish;
  }
  if (symlink(path, "/etc/localtime")) {
    fprintf(stderr, "Failed to symlink local time: %s", strerror(errno));
    retval = 5;
    goto finish;
  }
finish:
  if (f)
    fclose(f);
  return retval;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1205 <main+0x75>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    13bf <main+0x22f>
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    %rcx,-0x20(%rbp)
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
lea    0xdda(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x58(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    1288 <main+0xf8>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x2,-0x14(%rbp)
jmp    139e <main+0x20e>
lea    0xda1(%rip),%rdi
lea    0xda8(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     12bb <main+0x12b>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <fputs@plt>
jmp    12f5 <main+0x165>
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x3,-0x14(%rbp)
jmp    139e <main+0x20e>
lea    0xd65(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     1345 <main+0x1b5>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xd3e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x4,-0x14(%rbp)
jmp    139e <main+0x20e>
mov    -0x58(%rbp),%rdi
lea    0xd11(%rip),%rsi
call   1050 <symlink@plt>
cmp    $0x0,%rax
je     1399 <main+0x209>
mov    0x2c72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x78(%rbp),%edi
mov    %rax,%rdx
lea    0xd09(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x5,-0x14(%rbp)
jmp    139e <main+0x20e>
jmp    139e <main+0x20e>
cmpq   $0x0,-0x40(%rbp)
je     13b2 <main+0x222>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    $0x1,%r15d
cmp    $0x2,%edi
jne    12f4 <main+0x164>
mov    0x8(%rax),%r15
lea    0xe2a(%rip),%rdx
mov    %rbx,%rdi
mov    %r15,%rcx
call   1060 <snprintf@plt>
lea    -0x28(%rbp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
test   %rax,%rax
js     123b <main+0xab>
lea    0xe37(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1273 <main+0xe3>
mov    %rax,%r14
mov    %r15,%rdi
mov    %rax,%rsi
call   1070 <fputs@plt>
lea    0xe42(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     12a5 <main+0x115>
mov    $0x4,%r15d
lea    0xe3a(%rip),%r12
jmp    12c6 <main+0x136>
mov    0x2d96(%rip),%rax
mov    (%rax),%ebx
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdbe(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%r15d
jmp    12f4 <main+0x164>
mov    0x2d5e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdaf(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x3,%r15d
jmp    12f4 <main+0x164>
lea    0xdb5(%rip),%rsi
mov    %rbx,%rdi
call   1050 <symlink@plt>
test   %rax,%rax
je     1304 <main+0x174>
mov    $0x5,%r15d
lea    0xdc9(%rip),%r12
mov    0x2d0b(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
jmp    12ec <main+0x15c>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [23 x i8] c"/usr/share/zoneinfo/%s\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to stat '%s': %s\0A\00"
@errno = global i32 0
@.str.2 = [14 x i8] c"/etc/timezone\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [33 x i8] c"Failed to open /etc/timezone: %s\00"
@.str.5 = [15 x i8] c"/etc/localtime\00"
@.str.6 = [31 x i8] c"Failed to unlink localtime: %s\00"
@.str.7 = [33 x i8] c"Failed to symlink local time: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca %struct.stat
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
store i32* null, i32** %11
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 1, i32* %3
store i32 1, i32* %13
br label %73
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %7
%25 = trunc i64 %15 to i32
%26 = load i8*, i8** %7
%27 = call i32 @snprintf(i8* %17, i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = call i64 @stat(i8* %17, %struct.stat* %12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %7
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %34)
store i32 2, i32* %6
br label %65
36: ; preds = %21
%37 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %37, i32** %11
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i8*, i8** %7
%41 = load i32*, i32** %11
%42 = call i32 @fputs(i8* %40, i32* %41)
br label %48
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %46)
store i32 3, i32* %6
br label %65
48: ; preds = %39
%49 = call i64 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %54)
store i32 4, i32* %6
br label %65
56: ; preds = %48
%57 = call i64 @symlink(i8* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i8* %62)
store i32 5, i32* %6
br label %65
64: ; preds = %56
br label %65
65: ; preds = %64, %59, %51, %43, %30
%66 = load i32*, i32** %11
%67 = icmp ne i32* %66, null
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32*, i32** %11
%70 = call i32 @fclose(i32* %69)
br label %71
71: ; preds = %68, %65
%72 = load i32, i32* %6
store i32 %72, i32* %3
store i32 1, i32* %13
br label %73
73: ; preds = %71, %20
%74 = load i8*, i8** %9
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [23 x i8] c"/usr/share/zoneinfo/%s\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to stat '%s': %s\0A\00"
@errno = global i32 0
@.str.2 = [14 x i8] c"/etc/timezone\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [33 x i8] c"Failed to open /etc/timezone: %s\00"
@.str.5 = [15 x i8] c"/etc/localtime\00"
@.str.6 = [31 x i8] c"Failed to unlink localtime: %s\00"
@.str.7 = [33 x i8] c"Failed to symlink local time: %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %3 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %9, label %45
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = call i64 @stat(i8* %6, %struct.stat* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %18)
br label %45
20: ; preds = %9
%21 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = call i32 @fputs(i8* %11, i32* %21)
%25 = call i64 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %32, label %35
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %30)
br label %45
32: ; preds = %23
%33 = call i64 @symlink(i8* %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %42, label %35
35: ; preds = %32, %23
%36 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), %23 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), %32 ]
%37 = phi i32 [ 4, %23 ], [ 5, %32 ]
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* %36, i8* %40)
br label %42
42: ; preds = %35, %32
%43 = phi i32 [ 0, %32 ], [ %37, %35 ]
%44 = call i32 @fclose(i32* %21)
br label %45
45: ; preds = %15, %27, %42, %2
%46 = phi i32 [ 1, %2 ], [ %43, %42 ], [ 2, %15 ], [ 3, %27 ]
ret i32 %46
}